我正在尝试编写一个T-SQL脚本,它将被安排为自动运行。它应该返回与最近的全天业务有关的记录,即昨天,或者,如果在周一运行,则返回上周五的记录。
我被要求提供最近一天工作开始时(即昨天或上周五)所有打开的案例数量的报告(即,昨天或上周五,下面的代码是我第一次尝试这样做,但在周一运行时不起作用),以及所有打开的案例、所有关闭的案例和一天结束时的所有打开的案例。这就是我到目前为止所拥有的,它提供了期初余额(但它只有在周二到周五运行时才有效……不是在周一):
DECLARE @Start DATE = (getdate()-1), @end DATE = getdate()
SELECT
tc_sk -- the ID
,[tc_open_date]
,[tc_closed_date]
FROM tbl_cases
WHERE tc_open_date<@start
AND
tc_closed_date>=@start
OR
tc_open_date<@start
AND
tc_closed_date is null同样,我试图解决的问题是获取在最后一天营业期间打开的所有记录的列表。如果脚本在星期一运行,它应该检索上周五输入的记录。如果脚本从星期二运行到星期五,它应该检索“昨天”输入的记录。
我是一个脚本新手,而且我是自学成才的。您能给我的任何帮助都非常感谢!
发布于 2017-06-24 08:55:51
试试这个:
Declare @Start Date = GetDate() ;
If DateName(DW , @Start) = 'Friday'
begin
Set @Start = (getdate()-3);
SELECT
tc_sk -- the ID
,[tc_open_date]
,[tc_closed_date]
FROM tbl_cases
WHERE tc_open_date< @start
AND
(tc_closed_date>=@start
OR
tc_closed_date is null);
end
else
begin
Set @Start = (getdate()-1);
SELECT
tc_sk -- the ID
,[tc_open_date]
,[tc_closed_date]
FROM tbl_cases
WHERE tc_open_date< @start
AND
(tc_closed_date>=@start
OR
tc_closed_date is null);
end;发布于 2017-06-24 09:07:48
你可以用这个。
DECLARE @Start DATE = (getdate()-1), @end DATE = getdate()-1
SELECT
tc_sk -- the ID
,MAX([tc_open_date]) AS tc_open_date
,MAX([tc_closed_date]) AS tc_closed_date
FROM tbl_cases
WHERE tc_open_date<@start
AND (tc_closed_date>=@start OR tc_closed_date is null)
GROUP BY tc_sk
HAVING MAX([tc_open_date])https://stackoverflow.com/questions/44731685
复制相似问题