SELECT * FROM (
Select level='1', A='A1', B='B1', real amount=amount='1', B 'UNION ALL
Select level='2', A='B1', B='C1', real amount=amount='2', B 'UNION ALL
Select level='3', A='C1', B=' 'D1, real amount=amount=' 3 ', B 'UNION ALL
Select level='3', A='C1', B=' 'D2, real amount=amount=' 2 ', B 'UNION ALL
Select level='3', A='C1', B='D3', real amount=amount='2', B 'UNION ALL
Select level='3', A='C1', B='D4', real amount=amount='5', B 'UNION ALL
The select level='4', A=' 'D1, B=' E1 ', real consumption amount='9', B='UNION ALL
Select level='4', A=' 'D2, B=' E2 ', real amount=amount='5', B ') # t
For example:
There is A1 - B1 - C1 - D1 - E1 results A1 - B1 - C1 - D1 - E1
1, 2, 3, 9 2 1 6 54
For example:
There is A1 - B1 - C1 - D2 - E2 results A1 - B1 - C1 - D2 - E2
5 1 1 2 2 2 4 20
When A high level of line field B is equal to the low level of field, A multiply dosage of two lines together into A low level B field corresponding B in real usage, if in A process level than the need to scroll to multiply, such as the example, the smaller the level digital level is higher)
The results are as follows:
CodePudding user response:
To top once againCodePudding user response:
WITH CTE
AS
(SELECT *, QTY AS NEW_QTY FROM # T A
WHERE NOT the EXISTS (SELECT 1 FROM # T WHERE LEVELUNION ALL
SELECT A. *, B.N A.Q EW_QTY * TY
The FROM # T A
The JOIN CTE ON A.A B=B.B
WHERE a. EVEL> B.L EVEL)
SELECT * FROM CTE
The ORDER BY LEVEL, A