只有在union tsql中存在时才能获得结果

时间:2016-12-29 00:44:44

标签: sql sql-server sql-server-2008 tsql

所以我有以下选择将输出datametrics,我也有一个Union

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
    ,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
    ,'0'
    ,'0'
    ,'0'
    ,bd.YearBuilt YrConstruction
    ,ln.PROPDed + '%' [% loss ded EQ]
    ,CAST(bd.BldgNumber AS VARCHAR(255)) BldgNumber
    ,CONVERT(INT,REPLACE(bd.BuildingValue,',','')) BuildingValue
    ,CONVERT(INT,REPLACE(bd.ContentsBLimit,',','')) ContentsBLimit
    ,CONVERT(INT,REPLACE(bd.ContentsCLimit,',','')) ContentsCLimit
    ,CONVERT(INT,REPLACE(bd.TIBLimit,',','')) TIBLimit
    ,CONVERT(INT,REPLACE(bd.BILimit,',','')) BILimit
    ,CONVERT(INT,REPLACE(bd.EDPLimit,',','')) EDPLimit
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.risk r on r.SystemId=bp.SystemId
    AND r.CMMContainer=bp.CMMContainer
    AND r.ParentId=ln.Id
INNER JOIN java.building bd ON bd.CMMContainer = bp.CMMContainer
    AND bd.SystemId = bp.SystemId
    AND bd.ParentId=r.id
    AND bd.[Status] = 'ACTIVE'
INNER JOIN java.addr ad ON ad.CMMContainer = ni.CMMContainer
    AND bp.SystemId = ad.SystemId
    AND ad.AddrTypeCd in ('RiskAddr')
    AND ad.ParentId = bd.id
INNER JOIN java.transactioninfo 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 = 'Application'
    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 th.TransactionNumber in (select distinct th.TransactionNumber
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')
GROUP BY
     th.Transactioncd
    ,bp.PolicyNumber
    ,cast(bp.EffectiveDt 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)) 
    ,bd.YearBuilt
    ,ln.PROPDed + '%' 
    ,CAST(bd.BldgNumber AS VARCHAR(255)) 
    ,CONVERT(INT,REPLACE(bd.BuildingValue,',','')) 
    ,CONVERT(INT,REPLACE(bd.ContentsBLimit,',','')) 
    ,CONVERT(INT,REPLACE(bd.ContentsCLimit,',','')) 
    ,CONVERT(INT,REPLACE(bd.TIBLimit,',','')) 
    ,CONVERT(INT,REPLACE(bd.BILimit,',','')) 
    ,CONVERT(INT,REPLACE(bd.EDPLimit,',',''))

UNION

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
    ,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'
    ,ln.PROPDed +'%' [% loss ded EQ]
    ,CASE 
        WHEN BD.bldgnumber > 0
        THEN '0' END as bldgnumber
    ,'0'
    ,'0'
    ,'0'
    ,'0'
    ,'0'
    ,'0'
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'
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
        ,bldgnumber
        ,BOOKDATE

我得到以下结果: enter image description here

请注意,有三个1和3个2,但只有两个3&#39;那是因为在我的工会中,3并不存在。只在我的顶级查询上。我想要做的就是选择我工会中只存在的所有东西。所以对于这个例子,由于3在我的联盟中不存在,我想从我的结果中省略它。我尝试过EXISTS和IN但仍然没有得到我想要的东西。一直坚持这一点。

1 个答案:

答案 0 :(得分:0)

尝试这种方法

option