분기를 기준으로 기간을 선택하려면 달력 표를 사용하여이를 용이하게 할 수 있습니다. 다음 예제에서는 Creating a date dimension or calendar table in SQL Server - Aaron Bertrand의 발췌 문장을 사용합니다.
-- if regional settings are interfering with interpretation of dates/literals:
/*
set datefirst 7;
set dateformat mdy;
set language us_english;
--*/
if object_id('dbo.Calendar_Example') is not null drop table dbo.Calendar_Example;
create table dbo.Calendar_Example (
[Date] date not null
, [Year] smallint not null
, [Day] tinyint not null
, [Month] tinyint not null
, [Quarter] tinyint not null
, [YearQuarter] char(7) not null /* yyyy-qq */
, constraint pk_Calendar_Quarters primary key clustered (date)
);
declare @FromDate date = '20160101';
declare @ThruDate date = '202';
with n as (select n from (values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9)) t(n))
, d as (
select DateValue=convert(date,dateadd(day
, row_number() over (order by (select 1)) -1, @fromdate))
from n as deka
cross join n as hecto
cross join n as kilo /* 2.73 years */
cross join n as [10k] /* 27.3 years */
--cross join n as [100k] /* 273 years */
--cross join n as mega
)
insert into dbo.Calendar_Example
([Date], [Year], [Day], [Month], [Quarter], [YearQuarter])
select top (datediff(day, @FromDate, @ThruDate)+1)
[Date] = DateValue
, [Year] = convert(smallint,datepart(year,DateValue))
, [Day] = convert(tinyint,datepart(day,DateValue))
, [Month] = convert(tinyint,datepart(month,DateValue))
, [Quarter] = convert(tinyint,datepart(quarter,DateValue))
, [YearQuarter] = convert(char(7) ,convert(char(4), datepart(year,DateValue))
+'-Q'+convert(char(1), datepart(quarter,DateValue)))
from d
order by DateValue;
일정 숫자 표 참조 :
여기에서, 당신은 선택에 포함 분기 세트를 전달하는 테이블 반환 매개 변수 (TVP)을 사용할 수 있습니다.예 :
create type dbo.DateRange_udt as table (
FromDate date not null
, ThruDate date not null
primary key (FromDate,ThruDate)
);
go
create procedure dbo.Select_Using_DateRange_udt (
@DateRange dbo.DateRange_udt readonly
) as
begin;
set nocount on;
set xact_abort on;
select *
from MM
inner join @DateRange dr on mm.CDate >= dr.FromDate
and mm.CDate <= dr.ThruDate
end;
go
테이블 반환 매개 변수 참조 :
TVPs에 대한 대안
how to use TVPs with Entity Framework 4.1 and CodeFirst , 당신은
@innerstring
변수를 분할
수 있습니다. 및 (코멘트 약간 제거 포맷) Jeff Moden's DelimitedSplitN4k를 이용한다 ' create procedure dbo.Select_Using_YearQuarter_Delimited (
@DelimitedString nvarchar(256)
) as
begin;
set nocount on;
set xact_abort on;
with c as (
select ce.[Date]
from dbo.Calendar_Example ce
inner join dbo.DelimitedSplitN4k(@DelimitedString,';') as d
on convert(char(7),d.Item) = ce.YearQuarter
)
select *
from MM
inner join c on mm.CDate = c.Date;
end;
go
상기 예제의 분리를 가정한다.
create function dbo.DelimitedSplitN4K (
@pString nvarchar(4000)
, @pDelimiter nchar(1)
)
returns table with schemabinding as
return
with e1(n) as (
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all
select 1 union all select 1 union all select 1 union all select 1
)
, e2(n) as (select 1 from e1 a, e1 b)
, e4(n) as (select 1 from e2 a, e2 b)
, cteTally(n) as (select top (isnull(datalength(@pString)/2,0))
row_number() over (order by (select null)) from e4)
, cteStart(n1) as (select 1 union all
select t.n+1 from cteTally t where substring(@pString,t.n,1) = @pDelimiter)
, cteLen(n1,l1) as(select s.n1
, isnull(nullif(charindex(@pDelimiter,@pString,s.n1),0)-s.n1,4000)
from cteStart s
)
select ItemNumber = row_number() over(order by l.n1)
, Item = substring(@pString, l.n1, l.l1)
from cteLen l;
go
분할 문자열 참조 :
왜 MySQL의 태그를? – jarlh
예제 데이터와 예상 결과를 추가해야합니다. 우리는 quaters 또는 날짜로 수행하고자하는 것을 완전히 이해하지 못합니다. – Deadsheep39