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!!