2016-12-22 3 views
0

これに固執しました。私は、クエリを持っており、その結果は、以下の設定:あなたが写真で見ることができるようSQL Serverのselectでさまざまなグループを使用して最大日付を取得

enter image description here

私は何をしたいことはあるが、重複トランザクション番号2を削除するには:

DECLARE @bookdate date = cast(DATEADD(MONTH, DATEDIFF(MONTH, 0, GETDATE()), - 1) AS DATE) 
DECLARE @offset int = 1 

SELECT distinct @bookdate BookDate 
    ,th.Transactioncd TransactionType 
    ,bp.PolicyNumber PolicyNumber 
    ,cast(bp.EffectiveDt AS DATE) EffectiveDate 
    ,cast(th.TransactionEffectiveDt AS DATE) TransactionEffectiveDate 
    ,NULL 
    ,th.TransactionNumber TransactionNumber 
    ,cast(bp.ExpirationDt AS DATE) ExpirationDate 
    ,UPPER(ni.IndexName) InsuredName 
    ,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.partyinfo pti on pti.Id=ni.ParentId 
    AND pti.CMMContainer=bp.CMMContainer 
    and pti.SystemId=bp.SystemId 
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.TransactionEffectiveDt not like '2016-12%' 
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)) 
    ,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,',','')) 
ORDER BY PolicyNumber, transactionnumber, bldgnumber 

私はこの結果セットを取得します。私は日付を使ってmaxとgroup by関数を実行しようとしましたが動作しませんでした。私はトランザクション番号でそれを試しました、それは動作しますが、他の問題を導入しました。とにかく1つの結果セットを得ることができますか?それは実際の重複ではなく、異なる日付を持っていますが、私は最大の機能が働くかもしれないと思ったのですが、最新の日付を取得するだけですが、すべてでグループ化しなければならず、私は外の適用を見てきましたが、私はそれを一度も使用していないし、私はこの特定の問題に関してこれをどのように使用するのかも知らない。

+0

私はあなたが上のMAX関数を使用して、正しい軌道に乗っていると思います取引有効日付フィールド。はい、他のすべてのフィールドにグループを使用する必要があります。それをして、あなたはすばらしいはずです。 – tlemaster

+0

私はmaxとgroup byがうまくいくと思ったが、私は日付の最大値を除いてすべてでグループ化しているので、私は信じる日付が異なるので、2つの値を返すだろう。 – Lisbon

+0

トランザクションの有効期限を最大にして、1行だけ戻すようにしてください(2行の入力から)。 – tlemaster

答えて

0

first_valueを試しましたか?

FIRST_VALUE ([scalar_expression ]) 
    OVER ([ partition_by_clause ] order_by_clause [ rows_range_clause ]) 

それはこのように動作します:あなたはドキュメントがここで見つけることができDISTINCT

select **distinct** id, 
     first_value(transaction_effective_date) over (partition by number (,sup) order by date DESC (latest, ASC for oldest)) as last_trans_date 
from table; 

を記述する必要があります。https://msdn.microsoft.com/en-us/library/hh213018.aspx

+0

ああ、SQL Server 2008を使用していますが、これは2012以降にのみ適用されます。 – Lisbon

関連する問題