除了最后一句话之外,我已经完成了所有的事情。
我希望查询能够进一步缩小我的搜索范围并发出一条声明,说明如果agencyprofilesetupcomplete中的一条记录为0,那么请查看agencyprofilecreateddate中的日期是否少于今天日期的90天,如果是我想要包括那些结果。
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 :(得分: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
答案 1 :(得分:0)
只需在WHERE
条款中比较您的约会对象...
您需要使用DATEADD function on your GETDATE() - 90.
... >= DATEADD(dd, -90, GETDATE())
答案 2 :(得分: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