SQL查询摘要行

时间:2017-12-30 16:31:23

标签: sql sql-server

客户可以拥有多个协议,每个协议都有多个产品(即下图中的A - H)。我写了以下查询,它给出了每个协议的产品计数。然后我使用RowNum来确定最接近今天的协议和AgreementEndDate。

但是,我需要返回的是RowNum = 1是:a)最接近今天的AgreementEndDate(以及相关的AgreementID和AgreementEffectiveDate)以及产品购买摘要。

所以,RowNum 1 =

  • AgreementID = 73838545
  • AgreementEffectiveDate = 2015-05-28
  • AgreementEndDate = 2018-05-31
  • A = 1
  • B = 1
  • C = 1
  • D = 1
  • E = 1
  • F = 1
  • G = 1
  • H = 0

enter image description here

Select 
    LA.*
from
    (select 
        CustomerID
        ,AgreementID
        ,min(CONVERT(char(10), AgreementEffectiveDate,126)) as 'AgreementEffectiveDate'
        ,min(CONVERT(char(10), AgreementEndDate,126)) as 'AgreementEndDate'
        ,(CASE WHEN sum(cast([A Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'A Agreement'
        ,(CASE WHEN sum(cast([B Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'B Agreement'
        ,(CASE WHEN sum(cast([C Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'C Agreement'
        ,(CASE WHEN sum(cast([D Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'D Agreement'
        ,(CASE WHEN sum(cast([E Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'E Agreement'
        ,(CASE WHEN sum(cast([F Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'F Agreement'
        ,(CASE WHEN sum(cast([G Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'G Agreement'
        ,(CASE WHEN sum(cast([H Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'H Agreement'
        ,ROW_NUMBER() OVER(PARTITION BY TPID ORDER BY AgreementEndDate ASC) AS RowNum
        from LicenseAgreement
        Group by CustomerID, AgreementID, AgreementEndDate
        ) LA
    where TPID = '1789' --and RowNum = 1
order by RowNum, CustomerID, AgreementID, AgreementEndDate

1 个答案:

答案 0 :(得分:1)

我最终找到了解决方案。我刚添加了一个内连接。一旦我这样做,一切都有效。

这不是最好的方法,但它有效。

Select 
    B.ID 
    ,B.AgreementID 
    ,B.AgreementEffectiveDate 
    ,B.AgreementEndDate
    ,(CASE WHEN sum(cast(LA.[A Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'A Agreement'
    ,(CASE WHEN sum(cast(LA.[B Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'BAgreement'
    ,(CASE WHEN sum(cast(LA.[C Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'C Agreement'
    ,(CASE WHEN sum(cast(LA.[D Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'D Agreement'
    ,(CASE WHEN sum(cast(LA.[E Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'E Agreement'
    ,(CASE WHEN sum(cast(LA.[F Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'F Agreement'
    ,(CASE WHEN sum(cast(LA.[G Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'G Agreement'
    ,(CASE WHEN sum(cast(LA.[H Agreement] as float)) >= 1   THEN 1 ELSE 0 END) as 'H Agreement'
from
    (select 
        TPID 
        ,(CASE WHEN sum(cast([A Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'A Agreement'
        ,(CASE WHEN sum(cast([B Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'B Agreement'
        ,(CASE WHEN sum(cast([C Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'C Agreement'
        ,(CASE WHEN sum(cast([D Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'D Agreement'
        ,(CASE WHEN sum(cast([E Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'E Agreement'
        ,(CASE WHEN sum(cast([F Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'F Agreement'
        ,(CASE WHEN sum(cast([G Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'G Agreement'
        ,(CASE WHEN sum(cast([H Agreement] as float)) >= 1  THEN 1 ELSE 0 END) as 'H Agreement'
        from LicenseAgreement
        Group by ID, AgreementID, AgreementEndDate, AgreementEffectiveDate
        ) LA
    inner join (select 
        TPID 
        ,AgreementID
        ,min(CONVERT(char(10), AgreementEffectiveDate,126)) as 'AgreementEffectiveDate'
        ,min(CONVERT(char(10), AgreementEndDate,126)) as 'AgreementEndDate'
        ,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY AgreementEndDate ASC) AS RowNum
        from LicenseAgreement
        Group by ID, AgreementID, AgreementEndDate, AgreementEffectiveDate
    ) B ON b.TPID = LA.TPID 
    where RowNum = 1 
group by B.TPID,b.RowNum,b.AgreementID,b.AgreementEffectiveDate,b.AgreementEndDate
order by ID, AgreementID, AgreementEndDate, RowNum