2016-12-29 9 views
0

だから私、次の選択という意志出力datametricsを持っているし、私も連合を持って結果を取得します

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

お知らせどのように3つの1と3つの2がありますが、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