Amount of data from multiple tables

I have 3 tables T1, T2, T3. Each of them is related to the other.

enter image description here

enter image description here

enter image description here

SELECT     T1.T1_Serno, T2.T2_Serno, T3.T3_Serno, T1.T1,COUNT( T3.T3_Serno) as Count_1, T2.T2, T3.T3, T3.Quantity
FROM         T1 INNER JOIN
                      T2 ON T1.T1_Serno = T2.T1_Serno INNER JOIN
                      T3 ON T2.T2_Serno = T3.T2_Serno
GROUP BY T1.T1_Serno, T2.T2_Serno, T3.T3_Serno, T1.T1, T2.T2, T3.T3, T3.Quantity

and result

enter image description here

I want to get Count_1 to show me how many T3 there are, and Count_2 to have a relative count of the number of T3. I expect a result like

   T2    T3      Count_1 Count_2
    Apple Apple1    3    80
    Apple Apple2    3    80
    Apple Apple3    3    80
    -------
    Google Google1  1    40
    ------
    Nokia Nokia1    1    15

So, when I put this in the grid, I can use Counts as a Footer. Something like this, but with totals below each group.

enter image description here

+4
source share
1 answer
SELECT t2.t2,t3.t3,sum(t3.Quantity)
FROM T2 AS T2
INNER JOIN T3 AS T3 ON t3.t2_serno = t2.t2_serno
GROUP BY t2.t2,t3.t3

, 1 (t2) 1 (t3). , sql- group by, ( ). , . GROUP By t2.t2,t3.t3.

, , . t3. t3.t3 t2.t2, sql- t3.t3, t2.t2.

TL; DR: group by sql- , . , ( , sql) .

+7

Source: https://habr.com/ru/post/1537476/


All Articles