从同一个表

时间:2017-11-09 06:41:25

标签: sql sql-server tsql

例如,我有下表,

ID    DetailedID    ReferenceID    ItemID    Qty
01           101             80      A101    100
01           102             90      A102    200
01           103              0      A101    050
01           104              0      A109    100
02           105             81      1010    100
02           106             82      1010    100
03           107              0      1111    020
03           108             81      1010    100
03           109              0      X200    010
03           110              0      1010    020

现在我想要SumID数量,其中ReferenceID = 0且ItemID未根据ID明智匹配,并且我将显示数量总和以添加一列和该ID的最小详细ID。

我想输出像,

ID    DetailedID    ReferenceID    ItemID    Qty    AddQty
01           101             80      A101    100    055   
01           102             90      A102    200    NULL
01           103              0      A101    050    NULL
01           104              0      A109    055    NULL
02           105             81      1010    100    NULL
02           106             82      1010    100    NULL
03           107              0      1111    020    030
03           108             81      1010    100    NULL
03           109              0      X200    010    NULL
03           110              0      1010    020    NULL

2 个答案:

答案 0 :(得分:1)

你可以试试这个:

DECLARE @DataSource TABLE
(
    [ID] TINYINT
   ,[DetailedID] TINYINT
   ,[ReferenceID] TINYINT
   ,[ItemID] VARCHAR(12)
   ,[Qty] TINYINT
);

INSERT INTO @DataSource ([ID], [DetailedID], [ReferenceID], [ItemID], [Qty])
VALUES (01, 101, 80, 'A101', 100)
      ,(01, 102, 90, 'A102', 200)
      ,(01, 103, 0, 'A101', 50)
      ,(01, 104, 0, 'A109', 55)
      ,(02, 105, 81, '1010', 100)
      ,(02, 106, 82, '1010', 100)
      ,(03, 107, 0, '1111', 20)
      ,(03, 108, 81, '1010', 100)
      ,(03, 109, 0, 'X200', 10)
      ,(03, 110, 0, '1010', 20);

WITH DataSource AS
(
    SELECT DS.[ID]
          ,SUM(DS.[Qty]) AS [AddQty]
    FROM @DataSource DS
    WHERE DS.[ReferenceID] = 0
        AND NOT EXISTS(SELECT 1 FROM @DataSource I WHERE I.[ItemID] = DS.[ItemID] AND I.[ReferenceID] <> 0) 
    GROUP BY DS.[ID]
), DataSourceWithMinDetailedID AS
(
    SELECT [ID]     
          ,MIN([DetailedID]) AS [MinDetailedID]
    FROM @DataSource 
    GROUP BY [ID]
)
SELECT A.*
      ,C.[AddQty]
FROM @DataSource A
LEFT JOIN  DataSourceWithMinDetailedID B
    ON A.[ID] = B.[ID]
LEFT JOIN DataSource C
    ON B.[ID] = C.[ID]
    AND A.[DetailedID] = B.[MinDetailedID];

enter image description here

答案 1 :(得分:0)

请检查以下SQL CTE Select statement 您将意识到除了使用CASE语句之外,我还使用了Row_Number()和SUM()聚合函数以及Partition By子句 我希望很清楚

/*
create table ItemQuantity (
ID varchar(2),
DetailedID int,
ReferenceID int,
ItemID varchar(5),
Qty int)
insert into ItemQuantity values 
('01',           101,             80      ,'A101',    100),
('01',           102,             90      ,'A102',    200),
('01',           103,              0      ,'A101',    050),
('01',           104,              0      ,'A109',    100),
('02',           105,             81      ,'1010',    100),
('02',           106 ,            82      ,'1010',    100),
('03',           107  ,            0      ,'1111',    020),
('03',           108   ,          81      ,'1010',    100),
('03',           109    ,          0      ,'X200',    010),
('03',           110     ,         0      ,'1010',    020)
*/
;with cte as (
select 
    * ,
    QtyTemp = case when ReferenceID = 0 then Qty else 0 end,
    Flag = case when exists(select * from ItemQuantity k where i.ItemID = k.ItemID and k.ReferenceID <> 0) then 1 else 0 end,
    rn = row_number() over (partition by ID order by DetailedID)
from ItemQuantity i
)
select ID, DetailedID, ReferenceID, ItemID, Qty,
AddQty = case when rn = 1 then SUM(case when ReferenceID = 0 and Flag = 0 then QtyTemp else 0 end) OVER (Partition By ID) else null end
from cte

关于您的样本的一件事,您在所需输出中的一行中有55作为数量。但是该行的样本数据为100。请根据

检查数据和输出

我的脚本会产生以下输出 enter image description here

我希望它有所帮助,