编写数据集中最新月份的脚本
Scripting the latest month in the data set
在下面的视图中,我希望 sql 服务器将 6 月和 5 月识别为最近的月份,并在视图获取时识别 (latest month-1),而不是在 where 条件中指定实际日期每月刷新一次,最近一个月和(最近一个月 -1)也是如此。
PS:- 最近的报告月份是 table
中的 6 月
SELECT (A.[First Name])
,A.[Last Name]
,A.[Report Month]
FROM (
(
SELECT DISTINCT [First Name]
,[Last Name]
,[Report Month]
,[Bill To Code]
,[Region]
,[Area]
FROM dbo.Data
WHERE (
[Report Month] BETWEEN '2015-06-01'
AND '2015-06-30'
AND [FTE Status] = 'Inactive'
)
) A INNER JOIN (
SELECT DISTINCT [First Name]
,[Last Name]
,[Report Month]
,[Bill To Code]
,[Region]
,[Area]
FROM dbo.Data
WHERE (
[Report Month] BETWEEN '2015-05-01'
AND '2015-05-31'
AND [FTE Status] = 'Active'
)
) B ON A.[First Name] = B.[First Name]
AND A.[Last Name] = B.[Last Name]
)
谢谢,刚刚从我的错误中学会了如何更好地格式化 SO.Thanks 到 Sean.Learning 伙计们..:)
怎么样:
SELECT MAX(MONTH(Report_Month))
FROM YourTable
GROUP BY YEAR(Report_Month)
HAVING YEAR(Report_Month) = MAX(YEAR(Report_Month))
尽管你的问题相当模糊,但我认为这就是你要找的。
这将 return YourTable 中最大日期的第一天和最后一天。
;with cte as (
select convert(date,left(convert(varchar,Max(Report_Month),112),6) + '01') startDate,
month(Max(Report_Month)) n
from YourTable
union all
select dateadd(month,n,convert(date,convert(varchar,year(startDate)) + '0101')) startDate,
(n+1) n
from cte
where n < month(startDate)
)
select startdate, dateadd(day,-1,dateadd(month,1,startdate)) enddate
from cte
Hence i should write some function so that sql server reads the start
and end date of the latest month in the data set.
您说您需要读取开始日期和结束日期,但如果您真正需要的只是获取 table 中最近一个月的所有数据,那么这样做就可以了:
select *
from dbo.Data
where DATEDIFF(month,[Report Month],(SELECT MAX([Report Month] FROM dbo.Data))=0
and [FTE Status]='Active'
在下面的视图中,我希望 sql 服务器将 6 月和 5 月识别为最近的月份,并在视图获取时识别 (latest month-1),而不是在 where 条件中指定实际日期每月刷新一次,最近一个月和(最近一个月 -1)也是如此。
PS:- 最近的报告月份是 table
中的 6 月SELECT (A.[First Name])
,A.[Last Name]
,A.[Report Month]
FROM (
(
SELECT DISTINCT [First Name]
,[Last Name]
,[Report Month]
,[Bill To Code]
,[Region]
,[Area]
FROM dbo.Data
WHERE (
[Report Month] BETWEEN '2015-06-01'
AND '2015-06-30'
AND [FTE Status] = 'Inactive'
)
) A INNER JOIN (
SELECT DISTINCT [First Name]
,[Last Name]
,[Report Month]
,[Bill To Code]
,[Region]
,[Area]
FROM dbo.Data
WHERE (
[Report Month] BETWEEN '2015-05-01'
AND '2015-05-31'
AND [FTE Status] = 'Active'
)
) B ON A.[First Name] = B.[First Name]
AND A.[Last Name] = B.[Last Name]
)
谢谢,刚刚从我的错误中学会了如何更好地格式化 SO.Thanks 到 Sean.Learning 伙计们..:)
怎么样:
SELECT MAX(MONTH(Report_Month))
FROM YourTable
GROUP BY YEAR(Report_Month)
HAVING YEAR(Report_Month) = MAX(YEAR(Report_Month))
尽管你的问题相当模糊,但我认为这就是你要找的。
这将 return YourTable 中最大日期的第一天和最后一天。
;with cte as (
select convert(date,left(convert(varchar,Max(Report_Month),112),6) + '01') startDate,
month(Max(Report_Month)) n
from YourTable
union all
select dateadd(month,n,convert(date,convert(varchar,year(startDate)) + '0101')) startDate,
(n+1) n
from cte
where n < month(startDate)
)
select startdate, dateadd(day,-1,dateadd(month,1,startdate)) enddate
from cte
Hence i should write some function so that sql server reads the start and end date of the latest month in the data set.
您说您需要读取开始日期和结束日期,但如果您真正需要的只是获取 table 中最近一个月的所有数据,那么这样做就可以了:
select *
from dbo.Data
where DATEDIFF(month,[Report Month],(SELECT MAX([Report Month] FROM dbo.Data))=0
and [FTE Status]='Active'