Home > database > The sum of the first working procedure summary of various kinds of products
The sum of the first working procedure summary of various kinds of products
Time:09-15
The sum of a process with only a summary of various kinds of products the first
The name of the product process quantity AAA process 1 100 AAA process 2 50 3 AAA process 100 BBB process 2 100 CCC process 1 100 CCC process 3 100
Summary of the first step of each product totaled 300, the first working procedure of procedure 1 AAA, BBB first process for process 2, the first step CCC for step 1
CodePudding user response:
SELECT SUM (quantity) FROM the TABLE A WHERE NOT the EXISTS (SELECT 1 FROM the TABLE WHERE the product name=a. product name AND the name of the process & lt; A. process name)
CodePudding user response:
Grouping sorting
If object_id (' tempdb for.. # # TAB ') is not null drop table TAB The create table # TAB (name nvarchar (500), the class nvarchar (500), the quantity a decimal (18, 2)) Insert into # TAB (name, class, quantity) Select 'AAA', '1', 100 union all Select 'AAA', '2', 50 union all Select 'AAA', '3', 100 union all Select 'BBB', '2', 100 union all Select 'CCC', '1', 100 union all Select 'CCC', '1', 100
Select sum (quantity) from ( The select row_number () over (partition by the name the order by class) as num, * from # TAB ) t where num='1'