2017-12-30 23 views
0

顧客は複数の契約を結ぶことができ、各契約には複数の商品(下の画像ではA〜H)があります。私は各契約の製品数を示す次のクエリを書いた。次にRowNumを使用して、今日に最も近いAgreementとAgreementEndDateを決定します。SQLクエリの概要行

しかし、私が返す必要があるのはRowNum = 1です。a)今日に最も近いAgreementEndDate(および関連するAgreementIDとAgreementEffectiveDate)と製品購入の概要。

したがって、ROWNUM 1 =

  • AgreementID = 73838545
  • AgreementEffectiveDate = 2015年5月28日
  • AgreementEndDate = 2018年5月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

誰もいません製品テーブル。私は実際に8つの他のテーブルを内部結合する必要があります。代わりに、LAの *、私が試みた:。 LA.CustomerID 、和([契約}) 、和([B契約}) 、和([C契約}) 、和([D契約}) ...など。 しかし、私は同じ結果を得ています。そして、はい、あなたは正しい、実際には終了日はないが、合意はもはや効力を失う。 – emie

+1

...見たいサンプルデータと結果データを取得できますか?このテーブルに既にサマリーデータがあり、他のテーブルにアクセスする必要はないと言っているなら、それは問題ありません。 –

答えて

0

私は解決策を考え出すことになりました。私はちょうど内部結合を追加しました。私がこれをしたら、すべてがうまくいった。

これは最善の方法ではありませんが、機能します。

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