除了复杂连接中的一列

时间:2016-12-30 17:16:51

标签: sql sql-server tsql join duplicates

所以我有一个查询,可以选择具有长连接和聚合的数据指标。一些结果将返回看似重复的内容,除了一列,这是一个交易号。原因是对完全相同的记录进行了单独的交易。我们不能仅仅为了保存记录而删除交易。我有以下查询:

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语句,因为这只是一个非常大的存储过程的一部分。我已经在使用临时表了解其他内容,所以我认为我不能因为各种原因而使用它。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分: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