SQL窗口函数错误 - 寻找代码修复/建议

时间:2017-06-07 17:31:20

标签: sql sql-server

更新了问题

我想获得LOT项目编号的COUNTS和SUMS。批次具有不同的BATCH编号,每个BATCH可以具有不同的REVISION编号。 REVISION编号必须是PROPOSALSTATUS的每个批号中的“授予”或“已关闭/取消”的MAX(修订)编号。如果提案状态为“获得ROR”或“OAA获奖”,那么我将计算所有值。

这就是我想要使用的东西。由于我的MAX(修订版)语句,我一直收到窗口函数错误。我不明白如何解决这个问题。如果有人可以采取其中一个计数和一个总和并告诉我如何解决它将是非常棒的。

SELECT 
COUNT(PB.lot)AS SubmittedCount, 

COUNT(DISTINCT MAX(pb.revision) OVER (PARTITION BY pb.lot,pb.batch,(CASE WHEN pb.ProposalStatus = 'Closed/Cancelled'   OR pb.proposalstatus = 'Awarded' THEN 1 ELSE NULL END))) +
COUNT(CASE WHEN pb.proposalstatus = 'ROR Awarded'  or pb.ProposalStatus = 'OAA Awarded' THEN 1 ELSE NULL END)AS DecidedCount,

COUNT(DISTINCT MAX(pb.revision) OVER (PARTITION BY pb.lot, pb.batch,(CASE WHEN pb.proposalstatus = 'OAA Partially Awarded' OR pb.ProposalStatus = 'ROR Partially Awarded'  OR pb.proposalstatus = 'OAA Proposal Submitted - Pending Award' THEN 1 ELSE NULL END))) +
COUNT(CASE WHEN pb.ProposalStatus = 'Awarded' OR pb.ProposalStatus = 'ROR Awarded' OR pb.ProposalStatus = 'OAA Awarded' THEN 1 ELSE NULL END) AS WonCount,

COUNT(CASE WHEN pb.ProposalStatus = 'Disapproved' THEN 1 ELSE NULL END) AS DisapprovedCount,

SUM(pb.materialvalue + pb.shippingValue)AS Total, 

SUM(DISTINCT MAX(pb.revision) OVER (PARTITION BY pb.lot,pb.batch,(CASE WHEN pb.ProposalStatus = 'Closed/Cancelled'   OR pb.proposalstatus = 'Awarded' THEN pb.MaterialValue + Pb.ShippingValue ELSE NULL END))) +
SUM(CASE WHEN pb.proposalstatus = 'ROR Awarded'  or pb.ProposalStatus = 'OAA Awarded' THEN pb.MaterialValue + Pb.ShippingValue ELSE NULL END)AS DecidedTotal,

SUM(DISTINCT MAX(pb.revision) OVER (PARTITION BY pb.lot,pb.batch,(CASE WHEN pb.proposalstatus = 'OAA Partially Awarded' OR pb.ProposalStatus = 'ROR Partially Awarded'  OR pb.proposalstatus = 'OAA Proposal Submitted - Pending Award' THEN pb.MaterialValue + Pb.ShippingValue ELSE NULL END))) +
SUM(CASE WHEN pb.ProposalStatus = 'Awarded' OR pb.ProposalStatus = 'ROR Awarded' OR pb.ProposalStatus = 'OAA Awarded' THEN pb.MaterialValue + Pb.ShippingValue ELSE NULL END) AS WonTotal,

SUM(CASE WHEN pb.proposalStatus = 'Disapproved' THEN pb.MaterialValue + Pb.ShippingValue ELSE NULL END) AS DisapprovedTotal


FROM  DB1 ps  
INNER JOIN DB2 pb
ON ps.Title = pb.Lot

WHERE  (pb.ProposalSubmitted IS NOT NULL)  AND BATCH <> 6 AND (ps.Contract LIKE 'CLS')
GROUP BY pb.lot, pb.batch, pb.revision, pb.proposalstatus
ORDER BY pb.lot

现实 - 我希望能够把它放到一个看起来像这样的小表中: Sample Input

Final Results

    declare @pb table
              (lot, proposalStatus, proposalSubmitted datetime,
               revision, materialValue, shippingValue, batch);

    insert into @pb
    values ('1', 'Awarded', '5/23/2016', '0', '200', '100', '2'),
           ('1', 'Awarded', '5/23/2016', '0', '200', '300', '4'),   
           ('2', 'Pending', '  ', '1', '100', '400', '2'),
           ('3', 'Cancelled', '6/12/2016', '4', '200', '100', '4'),
           ('4', 'Awarded', '5/12/2016', '2', '300', '100', '3')

    declare @ps table
                   (title, contract);
    insert into ps
values ('1', 'CLS'),
       ('1', 'AFC'),
       ('2', 'CLS'),
       ('3', 'AFC'),
       ('4', 'CLS')        

我的加入是在pb.lot = ps.title

2 个答案:

答案 0 :(得分:1)

在这一行:

(SELECT COUNT(DISTINCT MAX(pb.revision) OVER (PARTITION BY pb.lot,pb.batch(CASE WHEN pb.ProposalStatus = 'Closed/Cancelled' OR pb.proposalstatus = 'Awarded' THEN 1 ELSE NULL END)))

您在“pb.batch”之后缺少逗号。

答案 1 :(得分:1)

您可以使用此代码计算MaxRevision Count,如果您想要计算所有最大修订数(不仅仅是第一个),请将ROW_NUMBER替换为RANK

WITH Data AS (
    SELECT *,
        IsMaxRevision = IIF(ROW_NUMBER() OVER(PARTITION BY PB.lot,PB.batch ORDER BY Revision DESC) = 1, 1, 0)
    FROM @ps        PS
    INNER JOIN @pb  PB ON ps.Title = pb.Lot
)
SELECT 
    lot, batch,
    DecidedCount        = SUM(CASE WHEN proposalstatus IN ('ROR Awarded', 'OAA Awarded') THEN 1
                                   WHEN proposalstatus IN ('Closed/Cancelled', 'Awarded') THEN IsMaxRevision
                              END),

    SubmittedCount      = COUNT(lot),
    OutstandingCount    = COUNT(CASE WHEN proposalstatus IN ('Partially Awarded', 'OAA Partially Awarded', 'ROR Partially Awarded', 'Proposal Submitted - Pending Award', 'OAA Proposal Submitted - Pending Award') THEN 1 ELSE NULL END),
    WonCount            = COUNT(CASE WHEN ProposalStatus IN ('Awarded', 'ROR Awarded', 'OAA Awarded') THEN 1 ELSE NULL END),
    DisapprovedCount    = COUNT(CASE WHEN ProposalStatus = 'Disapproved' THEN 1 ELSE NULL END),

    Total               = SUM(materialvalue + shippingValue), 
    DecidedTotal        = SUM(CASE WHEN proposalstatus IN ('Closed/Cancelled', 'Awarded', 'ROR Awarded', 'OAA Awarded') THEN materialvalue + shippingValue ELSE NULL END),
    OutstandingTotal    = SUM(CASE WHEN proposalstatus IN ('Partially Awarded', 'OAA Partially Awarded', 'ROR Partially Awarded', 'Proposal Submitted - Pending Award', 'OAA Proposal Submitted - Pending Award') THEN materialvalue + shippingValue ELSE NULL END),
    WonTotal            = SUM(CASE WHEN proposalstatus IN ('Awarded', 'ROR Awarded', 'OAA Awarded') THEN materialvalue + shippingValue ELSE NULL END),
    DisapprovedTotal    = SUM(CASE WHEN proposalStatus = 'Disapproved' THEN MaterialValue + ShippingValue ELSE NULL END) 
FROM Data   D
GROUP BY lot, batch