2016-12-29 31 views
0

所以我有以下選擇將輸出datametrics,我有一個聯盟以及取得成果,只有當它在聯合TSQL存在

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和三個2,但只有兩個3?這是因爲在我的工會中,3不存在。只有我的頂級查詢確實如此。我想要做的是選擇只有它存在於我的工會中的所有東西。所以對於這個例子,因爲我的工會中不存在3,我想從我的結果中省略它。我嘗試做EXISTS和IN,但仍然沒有得到我想要的。一直卡在這。

回答

0

試試這個辦法

;WITH cte 
    AS (SELECT 1 AS fst_indicator, -- To identify the records from first query 
       NULL as scd_indicator, 
       TransactionNumber,.. 
     FROM First_Query 
     UNION ALL -- Change it to UNION if you really want to remove duplicates 
     SELECT NULL AS fst_indicator, 
       1 as scd_indicator,-- To identify the records from second query 
       TransactionNumber,.. 
     FROM Second_Query), 
    cte1 
    AS (SELECT TransactionNumber 
     FROM cte 
     GROUP BY TransactionNumber 
     HAVING Sum(fst_indicator) >= 1 -- to make sure TransactionNumber is present in first query 
       AND Sum(scd_indicator) >= 1 -- to make sure TransactionNumber is present in second query 
     ) 
SELECT * 
FROM cte c 
WHERE EXISTS (SELECT 1 
       FROM cte1 c1 
       WHERE c.TransactionNumber = c1.TransactionNumber) 
+0

嗯......沒有工作了,我希望它的方式。搞亂了代碼後,仍然有相同的結果。我現在正在做的是把它們放在臨時表中並加入它們。看起來像我正在得到我想現在的結果,但我想弄清楚如何在不同的行中顯示連接的結果。我在想也許是光標? – Lisbon

+0

merp nevermind我實際上並沒有從臨時表中獲得我想要的結果。但我認爲我在正確的軌道上把它放在臨時表上,儘管它使編寫腳本變得更容易 – Lisbon

+0

我實際上修改了這個查詢,它有效!我把所有東西都放在臨時表中,這樣我的選擇會更容易。然後我只是修改了上面的查詢。謝謝! – Lisbon