2017-12-06 5 views
0

私は最後のステートメントとは別に、すべてのものを順番に取得しています。ここでは2つの基準を持つステートメントSQL

私の検索条件をさらに絞り込んで、agencyprofilesetupcomplete内のレコードの1つが0の場合は、agencyprofilecreateddateの日付が今日の日付よりも90日少ないかどうかを調べます。ifそれらの結果も含めたいと思っています。

select 
    distinct agencyprofilename, 
    (select 
     top 1 agencypayperiodfinalisestate 
    from 
     agencypayperiod as app 
    where 
     app.agencyprofileid = ap.agencyprofileid 
    order by 
     agencypayperiodpaydate)as 'Pay Period Finalise State', 
    agencyprofilecreateddate, agencyprofilesetupcomplete, 
    agencyemployertaxoffice 

from 
    Agencyprofile as ap 

left join 
    agencypayperiod as app on ap.agencyprofileid = app.agencyprofileid 
left outer join 
    agencyemployer as ae on ap.agencyemployerid = ae.agencyemployerid 
where agencypayperiodfinalisestate <>0 
    and agencyemployertaxoffice is not null 
    and agencyemployertaxoffice <>'' 
    or (agencyprofilesetupcomplete = 0 and agencyprofilecreateddate >= 
      (GetDate()-90)) 
order by agencyprofilename 

知りたい場合は、Microsoft SQL Serverも使用しています。

+0

このクエリでエラーが発生しましたか。 – Shammas

+0

あなたはあなたの基準を正しく読んでいるならば、あなたは 'agencyprofilecreateddate <(GetDate() - 90')を望んでいると思うでしょう –

+0

あなたの質問は何ですか?あなたが望まないデータを取得していますか、あるいはデータを取得していませんか? – Caleth

答えて

0

ちょうどあなたの日の微調整がWHERE句で比較...

あなたは私はあなたが条件のあなたの最初のグループをグループ化することで試すことができると思いDATEADD function on your GETDATE() - 90.

... >= DATEADD(dd, -90, GETDATE()) 
1

を使用する必要があります。たとえば、

SELECT DISTINCT agencyprofilename 
    ,(
     SELECT TOP 1 agencypayperiodfinalisestate 
     FROM agencypayperiod AS app 
     WHERE app.agencyprofileid = ap.agencyprofileid 
     ORDER BY agencypayperiodpaydate 
     ) AS 'Pay Period Finalise State' 
    ,agencyprofilecreateddate 
    ,agencyprofilesetupcomplete 
    ,agencyemployertaxoffice 
FROM Agencyprofile AS ap 
LEFT JOIN agencypayperiod AS app ON ap.agencyprofileid = app.agencyprofileid 
LEFT OUTER JOIN agencyemployer AS ae ON ap.agencyemployerid = ae.agencyemployerid 
WHERE (
     agencypayperiodfinalisestate <> 0 
     AND agencyemployertaxoffice IS NOT NULL 
     AND agencyemployertaxoffice <> '' 
     ) 
    OR (
     agencyprofilesetupcomplete = 0 
     AND agencyprofilecreateddate < DATEADD(dd,1,(GetDate() - 90)) 
     ) 
ORDER BY agencyprofilename 
0

次のクエリを試してください。

私はこれをテストする機会がありませんでした。あなたが何か問題に直面したら教えてください。

select 
    distinct agencyprofilename, 
    (select 
     top 1 agencypayperiodfinalisestate 
    from 
     agencypayperiod as app 
    where 
     app.agencyprofileid = ap.agencyprofileid 
    order by 
     agencypayperiodpaydate)as 'Pay Period Finalise State', 
    agencyprofilecreateddate, agencyprofilesetupcomplete, 
    agencyemployertaxoffice 

from 
    Agencyprofile as ap 

left join 
    agencypayperiod as app on ap.agencyprofileid = app.agencyprofileid 
left outer join 
    agencyemployer as ae on ap.agencyemployerid = ae.agencyemployerid 
where agencypayperiodfinalisestate <>0 
    and IsNull(agencyemployertaxoffice,'') != '' 
    or (agencyprofilesetupcomplete = 0 and agencyprofilecreateddate < DATEADD(dd,1,(GetDate() - 90)) 
order by agencyprofilename 
関連する問題