0

i'm trying to make a query by joining some lines through joins, until only one is able to get results, as soon as i insert two and go sql it remains in processing without returning results.

I've to combine them considering the production lot (column DAT and equal like AR1ID) and group them by the average of each value (thickness, weight, width, length)

This is the complete query with all join:

SELECT p1.AR1ID as 'Article', p1.DAT as 'Lot', ROUND((SUM(p1.VAL))/(COUNT(p1.VAL)),2) as 'Weight', ROUND((SUM(p2.VAL))/(COUNT(p2.VAL)),2) as 'Width', ROUND((SUM(p3.VAL))/(COUNT(p3.VAL)),2) as 'Length', ROUND((SUM(p4.VAL))/(COUNT(p4.VAL)),2) as 'Thickness'
From( PesoMedioLotto  p1 JOIN PesoMedioLotto  p2 on p1.AR1ID = p2.AR1ID and p1.DAT = p2.DAT)
    join PesoMedioLotto p3 on p2.AR1ID = p3.AR1ID and p2.DAT = p3.DAT
    join PesoMedioLotto p4 on p3.AR1ID = p4.AR1ID and p3.DAT = p4.DAT
    Where p1.DES1 = 'Weight' and p2.des1 = 'Width' and p3.DES1='Length' and p4.DES1 = 'Thickness'
Group by p1.AR1ID, p1.DAT

As you can see I use AR1ID to make the join that is the same for everyone

... and sql remains loading

But if i use this query (joining only p1 and p2) i have the correct results:

Query:

SELECT p1.AR1ID as 'Article', p1.DAT as 'Lot', ROUND((SUM(p1.VAL))/(COUNT(p1.VAL)),2) as 'Weight', ROUND((SUM(p2.VAL))/(COUNT(p2.VAL)),2) as 'Width'
From( PesoMedioLotto  p1 JOIN PesoMedioLotto  p2 on p1.AR1ID = p2.AR1ID and p1.DAT = p2.DAT)
    Where p1.DES1 = 'Weight' and p2.des1 = 'Width'
Group by p1.AR1ID, p1.DAT 

And work perfectly after 20 second of loading..!

This is the results:

<table>
  <tr>
    <th>Article</th>
    <th>Lot</th>
    <th>Weight</th>
    <th>Width</th>
  </tr>
  <tr>
    <td>1010585</td>
    <td>20190910</td>
    <td>7,85</td>
    <td>43,54</td>
  </tr>
  <tr>
    <td>1010500</td>
    <td>20190718</td>
    <td>18,51</td>
    <td>67,4</td>
  </tr>
  <tr>
    <td>1010444</td>
    <td>20190502</td>
    <td>19,6</td>
    <td>68,17</td>
  </tr>
  <tr>
    <td>1010314</td>
    <td>20190427</td>
    <td>9,09</td>
    <td>42,96</td>
  </tr>
  <tr>
    <td>1010525</td>
    <td>20190505</td>
    <td>19,43</td>
    <td>66,92</td>
  </tr>
  <tr>
    <td>1010397</td>
    <td>20190729</td>
    <td>3,02</td>
    <td>30,38</td>
  </tr>
  <tr>
    <td>1010387</td>
    <td>20190806</td>
    <td>18,74</td>
    <td>66,78</td>
  </tr>
</table>

Any ideas? Thanks!!

1 Answer 1

2

So your query basically boils down to this (I think):

SELECT
    p1.AR1ID AS Article,
    p1.DAT AS Lot,
    ROUND((SUM(p1.VAL))/(COUNT(p1.VAL)),2) AS [Weight],
    ROUND((SUM(p2.VAL))/(COUNT(p2.VAL)),2) AS [Width],
    ROUND((SUM(p3.VAL))/(COUNT(p3.VAL)),2) AS [Length],
    ROUND((SUM(p4.VAL))/(COUNT(p4.VAL)),2) AS [Thickness]
FROM
    PesoMedioLotto p1
    INNER JOIN PesoMedioLotto p2 ON p2.AR1ID = p1.AR1ID AND p2.DAT = p1.DAT AND p2.DES1 = 'Width'
    INNER JOIN PesoMedioLotto p3 ON p3.AR1ID = p1.AR1ID AND p3.DAT = p1.DAT AND p3.DES1 = 'Length'
    INNER JOIN PesoMedioLotto p4 ON p4.AR1ID = p1.AR1ID AND p4.DAT = p1.DAT AND p4.DES1 = 'Thickness'
WHERE 
    p1.DES1 = 'Weight'
GROUP BY
    p1.AR1ID,
    p1.DAT;

I would be interested to see what happens when you run this:

SELECT DISTINCT
    p1.AR1ID AS Article,
    p1.DAT AS Lot
FROM
    PesoMedioLotto p1
    INNER JOIN PesoMedioLotto p2 ON p2.AR1ID = p1.AR1ID AND p2.DAT = p1.DAT AND p2.DES1 = 'Width'
    INNER JOIN PesoMedioLotto p3 ON p3.AR1ID = p1.AR1ID AND p3.DAT = p1.DAT AND p3.DES1 = 'Length'
    INNER JOIN PesoMedioLotto p4 ON p4.AR1ID = p1.AR1ID AND p4.DAT = p1.DAT AND p4.DES1 = 'Thickness'
WHERE 
    p1.DES1 = 'Weight';

If that doesn't run then could you comment out the JOIN to p4, then the JOIN to p3, etc. Or you could try removing the DISTINCT, to see if you get a partial results set even if the query never completes in a timely manner.

My assumption is that you have a lot more data for lengths and/ or thicknesses in your database than you do for weights or widths?

This might also be useful to know:

SELECT DES1, COUNT(*) AS freq FROM PesoMedioLotto GROUP BY DES1;

Then my next questions would be:

  • why is all this data in one table, when it might make more sense to have different tables for different metrics?
  • does the data hold rows for other DES1 values, and if so how many?
  • are there any orphans, e.g. a thickness for a AR1ID/ DAT that doesn't also have a weight?
  • do you have good indexes on this table?

If the table is configured in a way that makes it impossible to query, then the best option might be to run your query into parts:

  • load only the data you need into a temporary table;
  • apply indexes to the temporary table;
  • run your query from the temporary table.

Maybe something like this:

SELECT AR1ID, DAT, DES1, VAL INTO #temp FROM PesoMedioLotto WHERE DES1 IN ('Weight', 'Width', 'Length', 'Thickness');
CREATE INDEX ix$lotto1 ON #temp (DES1);
CREATE INDEX ix$lotto2 ON #temp (AR1ID, DAT);

SELECT
    p1.AR1ID AS Article,
    p1.DAT AS Lot,
    ROUND((SUM(p1.VAL))/(COUNT(p1.VAL)),2) AS [Weight],
    ROUND((SUM(p2.VAL))/(COUNT(p2.VAL)),2) AS [Width],
    ROUND((SUM(p3.VAL))/(COUNT(p3.VAL)),2) AS [Length],
    ROUND((SUM(p4.VAL))/(COUNT(p4.VAL)),2) AS [Thickness]
FROM
    #temp p1
    INNER JOIN #temp p2 ON p2.AR1ID = p1.AR1ID AND p2.DAT = p1.DAT AND p2.DES1 = 'Width'
    INNER JOIN #temp p3 ON p3.AR1ID = p1.AR1ID AND p3.DAT = p1.DAT AND p3.DES1 = 'Length'
    INNER JOIN #temp p4 ON p4.AR1ID = p1.AR1ID AND p4.DAT = p1.DAT AND p4.DES1 = 'Thickness'
WHERE 
    p1.DES1 = 'Weight'
GROUP BY
    p1.AR1ID,
    p1.DAT;
Sign up to request clarification or add additional context in comments.

4 Comments

Unfortunately I did not create this table ... it makes no sense to write all these lines on one instead of creating more tables .. Damn! I tried your queries, removing also DISTINCT but unfortunately sql dies in the loading .. Yes the data contains other lines for DES1ID, they have mixed everything .. I don't understand how they manage to work on it! deprecated The other values of DES1ID are 20 excluding the 4 with which I should work Each value refers to other topics .. I checked now, you are right... this is the count of DES Thickness 48641 Width 24736 Weight 48602 Length 24727
Update: They always use weight and thickness! (48602 and 48641) so we have: rectangular: 24727 width and 24736 length rounds: 20139 outlet diameter and diameter perpendicular to outlet 20121 squares: 3806 exit length and 3806 length perpendicular to the exit Therefore it can be deduced that thickness and weight always use it, while the others depend on the shape of the object, in fact: 24727 + 20139 + 3806 = 48672 so we have 4 new values to use knowing that if someone is full the other is NULL
Those numbers are small enough that the query should be taking seconds to complete if the table is indexed sensibly. So my assumption is that there's either an awful lot of "other" data, or there's no indexes that can be used.
I decided to filter the search by ar1id via another table with master data (round, rectangular or square) so that based on the filter I will average and join only certain fields. This is the only way. Thanks for your help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.