sql - 数据等于数据中月份的末尾
sql - the data equals to the end of the months within the data
我需要等于相同 table 月末的数据。
到目前为止我一直在努力的是:
SELECT * FROM LG_006_01_EMFLINE H
where h.DATE_ in (
declare @start datetime
declare @end datetime
select @Start = (select MIN(mm.date_) as minimum FROM LG_006_01_EMFLINE mm where mm.accountcode like '335%' and mm.DATE_ > '2016-04-01')
select @End = (select MAX(nn.date_) FROM LG_006_01_EMFLINE nn where nn.accountcode like '335%' and nn.DATE_ > '2016-04-01')
;With CTE as
(
Select @Start as Date,Case When DatePart(mm,@Start)<>DatePart(mm,@Start+1) then 1 else 0 end as [Last]
UNION ALL
Select Date+1,Case When DatePart(mm,Date+1)<>DatePart(mm,Date+2) then 1 else 0 end from CTE
Where Date<@End
)
Select date from CTE
where [Last]=1 OPTION ( MAXRECURSION 0 ) )
我得到的错误是:
Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword
'declare'. Msg 102, Level 15, State 1, Line 26 Incorrect syntax near
')'.
提前致谢...
您说您的 in
语句中的代码本身可以正常工作,但正如 a_horse_with_no_name 正确指出的那样,您不能有 declare
语句或 cte
s 在子 select.
中
因此您需要稍微重新安排您的查询。由于您尚未提供源模式或数据,因此我无法对此进行测试:
declare @start datetime
declare @end datetime
select @Start = (select MIN(mm.date_) as minimum
FROM LG_006_01_EMFLINE mm
where mm.accountcode like '335%'
and mm.DATE_ > '20160401' -- Remove the hyphens to ensure SQL knows exactly what your date is, avoiding localisation issues.
)
select @End = (select MAX(nn.date_)
from LG_006_01_EMFLINE nn
where nn.accountcode like '335%'
and nn.DATE_ > '20160401' -- Remove the hyphens to ensure SQL knows exactly what your date is, avoiding localisation issues.
)
;With CTE as
(
Select @Start as Date
,Case When DatePart(mm,@Start) <> DatePart(mm,@Start+1) then 1 else 0 end as [Last]
union all
Select Date+1
,Case When DatePart(mm,Date+1) <> DatePart(mm,Date+2) then 1 else 0 end
from CTE
Where Date < @End
)
select *
from LG_006_01_EMFLINE H
where h.DATE_ in (
Select date
from CTE
where [Last]=1
)
option ( maxrecursion 0 )
我需要等于相同 table 月末的数据。
到目前为止我一直在努力的是:
SELECT * FROM LG_006_01_EMFLINE H
where h.DATE_ in (
declare @start datetime
declare @end datetime
select @Start = (select MIN(mm.date_) as minimum FROM LG_006_01_EMFLINE mm where mm.accountcode like '335%' and mm.DATE_ > '2016-04-01')
select @End = (select MAX(nn.date_) FROM LG_006_01_EMFLINE nn where nn.accountcode like '335%' and nn.DATE_ > '2016-04-01')
;With CTE as
(
Select @Start as Date,Case When DatePart(mm,@Start)<>DatePart(mm,@Start+1) then 1 else 0 end as [Last]
UNION ALL
Select Date+1,Case When DatePart(mm,Date+1)<>DatePart(mm,Date+2) then 1 else 0 end from CTE
Where Date<@End
)
Select date from CTE
where [Last]=1 OPTION ( MAXRECURSION 0 ) )
我得到的错误是:
Msg 156, Level 15, State 1, Line 7 Incorrect syntax near the keyword 'declare'. Msg 102, Level 15, State 1, Line 26 Incorrect syntax near ')'.
提前致谢...
您说您的 in
语句中的代码本身可以正常工作,但正如 a_horse_with_no_name 正确指出的那样,您不能有 declare
语句或 cte
s 在子 select.
因此您需要稍微重新安排您的查询。由于您尚未提供源模式或数据,因此我无法对此进行测试:
declare @start datetime
declare @end datetime
select @Start = (select MIN(mm.date_) as minimum
FROM LG_006_01_EMFLINE mm
where mm.accountcode like '335%'
and mm.DATE_ > '20160401' -- Remove the hyphens to ensure SQL knows exactly what your date is, avoiding localisation issues.
)
select @End = (select MAX(nn.date_)
from LG_006_01_EMFLINE nn
where nn.accountcode like '335%'
and nn.DATE_ > '20160401' -- Remove the hyphens to ensure SQL knows exactly what your date is, avoiding localisation issues.
)
;With CTE as
(
Select @Start as Date
,Case When DatePart(mm,@Start) <> DatePart(mm,@Start+1) then 1 else 0 end as [Last]
union all
Select Date+1
,Case When DatePart(mm,Date+1) <> DatePart(mm,Date+2) then 1 else 0 end
from CTE
Where Date < @End
)
select *
from LG_006_01_EMFLINE H
where h.DATE_ in (
Select date
from CTE
where [Last]=1
)
option ( maxrecursion 0 )