2016-12-30 26 views
1

因此,我有一個查詢可以選擇具有長連接和聚合的數據度量。一些結果會返回似乎是重複的,除了一列以外,這是一個交易號碼。原因是單獨的交易是針對完全相同的記錄進行的。我們不能只爲了記錄保存目的而刪除交易。我有下面的查詢:在select語句中複製記錄,複合連接中的一列除外

SELECT distinct (SELECT DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,MAX(GreatestDate)) + 1, 0)) 
       FROM 
       (VALUES (max(th.TransactionEffectiveDt)),(max(bp.EffectiveDt)),(max(th.TransactionDt))) AS Value(GreatestDate)) AS BookDate 
    ,th.Transactioncd TransactionType 
    ,bp.PolicyNumber PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) EffectiveDate 
    ,max(cast(th.TransactionEffectiveDt AS DATE)) TransactionEffectiveDate 
    ,NULL WINS_TransactionNumber 
    ,th.TransactionNumber TransactionNumber 
    ,cast(bp.ExpirationDt AS DATE) ExpirationDate 
    ,replace(UPPER(ni.IndexName), ',', '') InsuredName 
    ,replace(isnull(UPPER(ad.Addr1), '') + ' ' + isnull(UPPER(ad.Addr2), '') + ' ' + isnull(UPPER(ad.Addr3), '') + ' ' + isnull(UPPER(ad.Addr4), ''), ',', '') InsuredStreetAddress 
    ,UPPER(ad.City) InsuredCity 
    ,UPPER(ad.StateProvCd) InsuredState 
    ,ad.PostalCode InsuredZipCode 
    ,i.EntityTypeCd InsuredType 
    ,isnull(tr.ReasonCd, '') ReasonCode 
    ,cast(datediff(mm, th.transactioneffectivedt, bp.expirationdt)/cast(datediff(mm, bp.effectivedt, bp.expirationdt) AS DECIMAL(15, 4)) AS DECIMAL(15, 4)) ProrateFactor 
    ,isnull(cast(th.writtenpremiumamt as int), '0') APRP 
    ,isnull(cast(th.inforcepremiumamt as int), '0') AnnualPremium 
    ,CONVERT(INT,REPLACE(REPLACE(REPLACE(ln.DWELLLimit, '.00', ''), '$', ''),',','')) AggreLimit 
    ,'0' YrConstruction 
    ,ln.PROPDed +'%' [% loss ded EQ] 
    ,CASE 
     WHEN BD.bldgnumber > 0 
     THEN '0' END as Ind 
    ,'0' CovA 
    ,'0' CovB 
    ,'0' CovC 
    ,'0' CovD 
    ,'0' CovE 
    ,'0' CovF 
FROM java.basicpolicy bp 
INNER JOIN java.nameinfo ni ON ni.SystemId = bp.SystemId 
    AND ni.CMMContainer = bp.CMMContainer 
INNER JOIN java.line ln ON ln.CMMContainer = bp.CMMContainer 
    AND bp.SystemId = ln.SystemId 
INNER JOIN java.building bd ON bd.CMMContainer = bp.CMMContainer 
    AND bd.SystemId = bp.SystemId 
INNER JOIN java.addr ad ON ad.CMMContainer = ni.CMMContainer 
    AND bp.SystemId = ad.SystemId 
    AND ad.AddrTypeCd = 'InsuredMailingAddr' 
INNER JOIN java.transactionhistory th ON th.CMMContainer = bp.CMMContainer 
    AND th.SystemId = bp.SystemId 
LEFT JOIN java.transactionreason tr ON tr.CMMContainer = bp.CMMContainer 
    AND TR.SystemId = bp.SystemId 
    AND TR.ParentId = th.ID 
INNER JOIN java.insured i ON i.CMMContainer = bp.CMMContainer 
    AND i.SystemId = bp.SystemId 
WHERE bp.CMMContainer = 'policy' 
    AND ni.NameTypeCd = 'INSUREDNAME' 
    AND (
     th.TransactionCd IN (
      'new business' 
      ,'endorsement' 
      ,'cancellation' 
      ,'rewrite-new' 
      ) 
     OR (
      th.WrittenPremiumAmt IS NOT NULL 
      AND th.WrittenPremiumAmt <> 0 
      AND th.TransactionCd IN ('reinstatement') 
      ) 
     ) 
    AND bp.CarrierCd = 'ENIC' 
    AND bp.PolicyNumber = '84EQ000006' 
GROUP BY 
    th.Transactioncd 
    ,bp.PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) 
    ,cast(th.TransactionEffectiveDt AS DATE) 
    ,th.TransactionNumber 
    ,cast(bp.ExpirationDt AS DATE) 
    ,UPPER(ni.IndexName) 
    ,(isnull(UPPER(ad.Addr1), '') + ' ' + isnull(UPPER(ad.Addr2), '') + ' ' + isnull(UPPER(ad.Addr3), '') + ' ' + isnull(UPPER(ad.Addr4), '')) 
    ,UPPER(ad.City) 
    ,UPPER(ad.StateProvCd) 
    ,ad.PostalCode 
    ,i.EntityTypeCd 
    ,isnull(tr.ReasonCd, '') 
    ,cast(datediff(mm, th.transactioneffectivedt, bp.expirationdt)/cast(datediff(mm, bp.effectivedt, bp.expirationdt) AS DECIMAL(15, 4)) AS DECIMAL(15, 4)) 
    ,isnull(cast(th.writtenpremiumamt as int), '0') 
    ,isnull(cast(th.inforcepremiumamt as int), '0') 
    ,CONVERT(INT,REPLACE(REPLACE(REPLACE(ln.DWELLLimit, '.00', ''), '$', ''),',','')) 
    ,ln.PROPDed +'%' 
    ,CASE 
     WHEN BD.bldgnumber > 0 
     THEN '0' END 
ORDER BY PolicyNumber 
     ,transactionnumber 
     ,Ind 
     ,BOOKDATE 

結果集是這樣的:

enter image description here

交易公告編號列有交易4和5?其他一切都是一樣的,但交易號碼。我不能只刪除一條記錄。所以對於這個實例,因爲有重複的記錄,客戶端只想看到其中的一個。我嘗試使用row_number()和按功能分組,但我認爲由於許多聯接,它將我扔掉。我需要一個select語句,只顯示其中的一個,因爲這只是一個非常大的存儲過程的一部分。我已經在使用臨時表作其他事情了,所以我不認爲我可以使用它,原因很多。

回答

0

可以使用max()(或min() - 取決於你)這樣的交易編號:

SELECT -- DISTINCT --<-- Removed distinct 
    (SELECT DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,MAX(GreatestDate)) + 1, 0)) 
       FROM 
       (VALUES (max(th.TransactionEffectiveDt)),(max(bp.EffectiveDt)),(max(th.TransactionDt))) AS Value(GreatestDate)) AS BookDate 
    ,th.Transactioncd TransactionType 
    ,bp.PolicyNumber PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) EffectiveDate 
    ,max(cast(th.TransactionEffectiveDt AS DATE)) TransactionEffectiveDate 
    ,NULL WINS_TransactionNumber 
    ,max(th.TransactionNumber) TransactionNumber --<-- Added max() here 
    ,cast(bp.ExpirationDt AS DATE) ExpirationDate 
    ,replace(UPPER(ni.IndexName), ',', '') InsuredName 
    ,replace(isnull(UPPER(ad.Addr1), '') + ' ' + isnull(UPPER(ad.Addr2), '') + ' ' + isnull(UPPER(ad.Addr3), '') + ' ' + isnull(UPPER(ad.Addr4), ''), ',', '') InsuredStreetAddress 
    ,UPPER(ad.City) InsuredCity 
    ,UPPER(ad.StateProvCd) InsuredState 
    ,ad.PostalCode InsuredZipCode 
    ,i.EntityTypeCd InsuredType 
    ,isnull(tr.ReasonCd, '') ReasonCode 
    ,cast(datediff(mm, th.transactioneffectivedt, bp.expirationdt)/cast(datediff(mm, bp.effectivedt, bp.expirationdt) AS DECIMAL(15, 4)) AS DECIMAL(15, 4)) ProrateFactor 
    ,isnull(cast(th.writtenpremiumamt as int), '0') APRP 
    ,isnull(cast(th.inforcepremiumamt as int), '0') AnnualPremium 
    ,CONVERT(INT,REPLACE(REPLACE(REPLACE(ln.DWELLLimit, '.00', ''), '$', ''),',','')) AggreLimit 
    ,'0' YrConstruction 
    ,ln.PROPDed +'%' [% loss ded EQ] 
    ,CASE 
     WHEN BD.bldgnumber > 0 
     THEN '0' END as Ind 
    ,'0' CovA 
    ,'0' CovB 
    ,'0' CovC 
    ,'0' CovD 
    ,'0' CovE 
    ,'0' CovF 
FROM java.basicpolicy bp 
INNER JOIN java.nameinfo ni ON ni.SystemId = bp.SystemId 
    AND ni.CMMContainer = bp.CMMContainer 
INNER JOIN java.line ln ON ln.CMMContainer = bp.CMMContainer 
    AND bp.SystemId = ln.SystemId 
INNER JOIN java.building bd ON bd.CMMContainer = bp.CMMContainer 
    AND bd.SystemId = bp.SystemId 
INNER JOIN java.addr ad ON ad.CMMContainer = ni.CMMContainer 
    AND bp.SystemId = ad.SystemId 
    AND ad.AddrTypeCd = 'InsuredMailingAddr' 
INNER JOIN java.transactionhistory th ON th.CMMContainer = bp.CMMContainer 
    AND th.SystemId = bp.SystemId 
LEFT JOIN java.transactionreason tr ON tr.CMMContainer = bp.CMMContainer 
    AND TR.SystemId = bp.SystemId 
    AND TR.ParentId = th.ID 
INNER JOIN java.insured i ON i.CMMContainer = bp.CMMContainer 
    AND i.SystemId = bp.SystemId 
WHERE bp.CMMContainer = 'policy' 
    AND ni.NameTypeCd = 'INSUREDNAME' 
    AND (
     th.TransactionCd IN (
      'new business' 
      ,'endorsement' 
      ,'cancellation' 
      ,'rewrite-new' 
      ) 
     OR (
      th.WrittenPremiumAmt IS NOT NULL 
      AND th.WrittenPremiumAmt <> 0 
      AND th.TransactionCd IN ('reinstatement') 
      ) 
     ) 
    AND bp.CarrierCd = 'ENIC' 
    AND bp.PolicyNumber = '84EQ000006' 
GROUP BY 
    th.Transactioncd 
    ,bp.PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) 
    ,cast(th.TransactionEffectiveDt AS DATE) 
    --,th.TransactionNumber  --<-- Removed this 
    ,cast(bp.ExpirationDt AS DATE) 
    ,UPPER(ni.IndexName) 
    ,(isnull(UPPER(ad.Addr1), '') + ' ' + isnull(UPPER(ad.Addr2), '') + ' ' + isnull(UPPER(ad.Addr3), '') + ' ' + isnull(UPPER(ad.Addr4), '')) 
    ,UPPER(ad.City) 
    ,UPPER(ad.StateProvCd) 
    ,ad.PostalCode 
    ,i.EntityTypeCd 
    ,isnull(tr.ReasonCd, '') 
    ,cast(datediff(mm, th.transactioneffectivedt, bp.expirationdt)/cast(datediff(mm, bp.effectivedt, bp.expirationdt) AS DECIMAL(15, 4)) AS DECIMAL(15, 4)) 
    ,isnull(cast(th.writtenpremiumamt as int), '0') 
    ,isnull(cast(th.inforcepremiumamt as int), '0') 
    ,CONVERT(INT,REPLACE(REPLACE(REPLACE(ln.DWELLLimit, '.00', ''), '$', ''),',','')) 
    ,ln.PROPDed +'%' 
    ,CASE 
     WHEN BD.bldgnumber > 0 
     THEN '0' END 
ORDER BY PolicyNumber 
     ,transactionnumber 
     ,Ind 
     ,BOOKDATE 
0
SELECT * FROM (
SELECT *,ROW_NUMBER()OVER(PARTITION BY BookDate 
    ,TransactionType 
    ,PolicyNumber 
    ,EffectiveDate 
    , TransactionEffectiveDate 
    , WINS_TransactionNumber 
    , TransactionNumber 
    , ExpirationDate 
    , InsuredName 
    , InsuredStreetAddress 
    ,InsuredCity 
    ,InsuredState 
    , InsuredZipCode 
    ,InsuredType 
    , ReasonCode 
    , ProrateFactor 
    ,APRP 
    , AnnualPremium 
    , AggreLimit 
    ,YrConstruction 
    , [% loss ded EQ] 
    , Ind 
    ,CovA 
    , CovB 
    , CovC 
    ,CovD 
    , CovE 
    , Cov ORDER BY TransactionNumber) as RowNumber 
FROM(
SELECT distinct (SELECT DATEADD(DAY,-1,DATEADD(MONTH,DATEDIFF(MONTH,0,MAX(GreatestDate)) + 1, 0)) 
       FROM 
       (VALUES (max(th.TransactionEffectiveDt)),(max(bp.EffectiveDt)),(max(th.TransactionDt))) AS Value(GreatestDate)) AS BookDate 
    ,th.Transactioncd TransactionType 
    ,bp.PolicyNumber PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) EffectiveDate 
    ,max(cast(th.TransactionEffectiveDt AS DATE)) TransactionEffectiveDate 
    ,NULL WINS_TransactionNumber 
    ,th.TransactionNumber TransactionNumber 
    ,cast(bp.ExpirationDt AS DATE) ExpirationDate 
    ,replace(UPPER(ni.IndexName), ',', '') InsuredName 
    ,replace(isnull(UPPER(ad.Addr1), '') + ' ' + isnull(UPPER(ad.Addr2), '') + ' ' + isnull(UPPER(ad.Addr3), '') + ' ' + isnull(UPPER(ad.Addr4), ''), ',', '') InsuredStreetAddress 
    ,UPPER(ad.City) InsuredCity 
    ,UPPER(ad.StateProvCd) InsuredState 
    ,ad.PostalCode InsuredZipCode 
    ,i.EntityTypeCd InsuredType 
    ,isnull(tr.ReasonCd, '') ReasonCode 
    ,cast(datediff(mm, th.transactioneffectivedt, bp.expirationdt)/cast(datediff(mm, bp.effectivedt, bp.expirationdt) AS DECIMAL(15, 4)) AS DECIMAL(15, 4)) ProrateFactor 
    ,isnull(cast(th.writtenpremiumamt as int), '0') APRP 
    ,isnull(cast(th.inforcepremiumamt as int), '0') AnnualPremium 
    ,CONVERT(INT,REPLACE(REPLACE(REPLACE(ln.DWELLLimit, '.00', ''), '$', ''),',','')) AggreLimit 
    ,'0' YrConstruction 
    ,ln.PROPDed +'%' [% loss ded EQ] 
    ,CASE 
     WHEN BD.bldgnumber > 0 
     THEN '0' END as Ind 
    ,'0' CovA 
    ,'0' CovB 
    ,'0' CovC 
    ,'0' CovD 
    ,'0' CovE 
    ,'0' CovF 
FROM java.basicpolicy bp 
INNER JOIN java.nameinfo ni ON ni.SystemId = bp.SystemId 
    AND ni.CMMContainer = bp.CMMContainer 
INNER JOIN java.line ln ON ln.CMMContainer = bp.CMMContainer 
    AND bp.SystemId = ln.SystemId 
INNER JOIN java.building bd ON bd.CMMContainer = bp.CMMContainer 
    AND bd.SystemId = bp.SystemId 
INNER JOIN java.addr ad ON ad.CMMContainer = ni.CMMContainer 
    AND bp.SystemId = ad.SystemId 
    AND ad.AddrTypeCd = 'InsuredMailingAddr' 
INNER JOIN java.transactionhistory th ON th.CMMContainer = bp.CMMContainer 
    AND th.SystemId = bp.SystemId 
LEFT JOIN java.transactionreason tr ON tr.CMMContainer = bp.CMMContainer 
    AND TR.SystemId = bp.SystemId 
    AND TR.ParentId = th.ID 
INNER JOIN java.insured i ON i.CMMContainer = bp.CMMContainer 
    AND i.SystemId = bp.SystemId 
WHERE bp.CMMContainer = 'policy' 
    AND ni.NameTypeCd = 'INSUREDNAME' 
    AND (
     th.TransactionCd IN (
      'new business' 
      ,'endorsement' 
      ,'cancellation' 
      ,'rewrite-new' 
      ) 
     OR (
      th.WrittenPremiumAmt IS NOT NULL 
      AND th.WrittenPremiumAmt <> 0 
      AND th.TransactionCd IN ('reinstatement') 
      ) 
     ) 
    AND bp.CarrierCd = 'ENIC' 
    AND bp.PolicyNumber = '84EQ000006' 
GROUP BY 
    th.Transactioncd 
    ,bp.PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) 
    ,cast(th.TransactionEffectiveDt AS DATE) 
    ,th.TransactionNumber 
    ,cast(bp.ExpirationDt AS DATE) 
    ,UPPER(ni.IndexName) 
    ,(isnull(UPPER(ad.Addr1), '') + ' ' + isnull(UPPER(ad.Addr2), '') + ' ' + isnull(UPPER(ad.Addr3), '') + ' ' + isnull(UPPER(ad.Addr4), '')) 
    ,UPPER(ad.City) 
    ,UPPER(ad.StateProvCd) 
    ,ad.PostalCode 
    ,i.EntityTypeCd 
    ,isnull(tr.ReasonCd, '') 
    ,cast(datediff(mm, th.transactioneffectivedt, bp.expirationdt)/cast(datediff(mm, bp.effectivedt, bp.expirationdt) AS DECIMAL(15, 4)) AS DECIMAL(15, 4)) 
    ,isnull(cast(th.writtenpremiumamt as int), '0') 
    ,isnull(cast(th.inforcepremiumamt as int), '0') 
    ,CONVERT(INT,REPLACE(REPLACE(REPLACE(ln.DWELLLimit, '.00', ''), '$', ''),',','')) 
    ,ln.PROPDed +'%' 
    ,CASE 
     WHEN BD.bldgnumber > 0 
     THEN '0' END 
     ) AS ABC 
     ) AS DEF 
    WHERE DEF.RowNumber =1 
+0

試試這個ROW_NUMBER,只是幾個子查詢 – LONG