我有这个SQL查询根据用户可以选择的时间段提取数据,用户可以用他需要的日期手动更改日期,是否有可能像我们提取的那样修复这段时间将有最近三个月的数据来修复过去三个月的数据,如果有人可以帮助我请查询:
SELECT
ROW_NUMBER() OVER (partition by ptf_id ORDER BY sum(isnull([pnr_count],0)) desc) as id
,[coresitecode]
,[sitecode]
,ptf_id
,sum(isnull([pnr_count],0)) TOTAL_PNRs
,sum(isnull([child_pnr_count],0)) TOTAL_child_PNRs into #pnr_by_site_and_platform
FROM
[SWAT_V2_PLL].[rpt].[PNR_per_site_daily] pnr
inner join ctrl.PTF_Platform ptf on pnr.ptf_id = ptf.uid
where
DAY_id >= 20150701 -- time period you need to consider YYYYMMDD
and DAY_id <= 20150901 -- end of PERIOD
and ptf_label in ('AETMEU1EUR', 'AETMEU2EUR', 'AETMAPASIA', 'AETMUSAMER')
group by
[coresitecode]
,[sitecode]
,ptf_id
order by
TOTAL_PNRs desc
-- ordered PNRs ....
select ptf_id, id , coresitecode, sitecode, TOTAL_PNRs
from #pnr_by_site_and_platform order by ptf_id, TOTAL_PNRs desc
答案 0 :(得分:2)
继续我的快速评论
...DAY_id BETWEEN
CONVERT(INT,CONVERT(CHAR(8), DATEADD(MONTH, -3, GETDATE()), 112)) AND
CONVERT(INT,CONVERT(CHAR(8), GETDATE(), 112))
GETDATE()
今天,DATEADD
正在增加-3个月(换句话说,从今天开始减去三个月。CONVERT(CHAR(8),...,112) is converting to
YYYYMMDD and finally
CONVERT(INT,.... )`将字符串转换为整数。
转换文档:https://msdn.microsoft.com/en-AU/library/ms187928.aspx
这个问题有关于计算上周/月的第一天/最后一天等的更多细节。
How to get first and last day of previous month (with timestamp) in SQL Server