在SQL中选择空周

时间:2014-01-08 16:44:14

标签: sql sql-server

我在几周内使用DATEPART在SQL中进行分组。但是,我想要包括空周,不仅仅包括数周。我有一个脚本可以完美地收集数周的数据,但不是空的周。我一直试图使用左外连接,但我的SQL技能并不是那么好。我决定问你们。

这是我的剧本:

select 
    DatePart(Year, MYTABLE.CREATION_DATE) as DateYear,
    DatePart(Month, MYTABLE.CREATION_DATE) as DateMonth,
    DatePart(Week, MYTABLE.CREATION_DATE) as DateWeek,
    sum(cast(MYTABLE.BASE_TRANSACTION_VALUE as int)) as Value,
    min(MYTABLE.CREATION_DATE) as DateT
from MYTABLE
where MYTABLE.SEGMENT2 like '00601'
    and MYTABLE.SEGMENT4 like '%'
    and MYTABLE.CREATION_DATE > '2012-12-30 12:00:00'
    and MYTABLE.CREATION_DATE < '2013-12-30 11:00:00'
group by 
    DatePart(Year, MYTABLE.CREATION_DATE), 
    DatePart(Month, MYTABLE.CREATION_DATE), 
    DatePart(Week, MYTABLE.CREATION_DATE)
order by DateYear, DateMonth, DateWeek

这是该脚本的结果。

enter image description here

在这种情况下,你们可以给我一个加入条款的例子吗?

2 个答案:

答案 0 :(得分:6)

[更新2014-01-09 08:06 UTC]

你可以动态创建日期(DateRange),然后将它们加入你的选择中,如下所示:

declare @startdate date;
set @startdate = '2012-12-30';
declare @enddate date;
set @enddate = '2013-12-30'

;with DateRange AS
(
    SELECT 
        DatePart(Year, @startdate) as DateYear,
        DatePart(Month, @startdate) as DateMonth,
        DatePart(iso_Week, @startdate) as DateWeek,
        @startdate as DateValue
    UNION ALL
    SELECT 
        DatePart(Year, dateadd(dd,7,DateValue)) as DateYear,
        DatePart(Month, dateadd(dd,7,DateValue)) as DateMonth,
        DatePart(iso_Week, dateadd(dd,7,DateValue)) as DateWeek,
        dateadd(dd,7,DateValue)
    FROM   DateRange
    WHERE  dateadd(dd,7,DateValue) <= @enddate
), data as (
    select 
        DatePart(Year, MYTABLE.CREATION_DATE) as DateYear,
        DatePart(Month, MYTABLE.CREATION_DATE) as DateMonth,
        DatePart(iso_Week, MYTABLE.CREATION_DATE) as DateWeek,
        sum(cast(MYTABLE.BASE_TRANSACTION_VALUE as int)) as Value,
        min(MYTABLE.CREATION_DATE) as DateT
    from MYTABLE
    where MYTABLE.SEGMENT2 like '00601'
        and MYTABLE.SEGMENT4 like '%'
        and MYTABLE.CREATION_DATE > '2012-12-30 12:00:00'
        and MYTABLE.CREATION_DATE < '2013-12-30 11:00:00'
    group by 
        DatePart(Year, MYTABLE.CREATION_DATE),
        DatePart(Month, MYTABLE.CREATION_DATE),
        DatePart(iso_Week, MYTABLE.CREATION_DATE)
)
select 
    DateRange.DateYear,
    DateRange.DateMonth,
    DateRange.DateWeek,
    data.Value,
    data.DateT
from data
right outer join
DateRange 
on 
    data.DateYear = DateRange.DateYear
and data.DateWeek = DateRange.DateWeek
order by 
    DateRange.DateYear, 
    DateRange.DateMonth, 
    DateRange.DateWeek

答案 1 :(得分:4)

这应该这样做。我的SQLFiddle是http://www.sqlfiddle.com/#!6/b777f/1/0

declare @WeekTable table(
    [WeekDate] datetime
)
declare @startDate datetime,
@EndDate datetime
select @startDate = '1/1/2013',@EndDate = '12/31/2013'
while @startDate<=@EndDate begin
    insert into @WeekTable values(@startDate)
    set @startDate = @startDate+7
end

select 
    DatePart(Year, [WeekDate]) as DateYear,
    DatePart(Month, [WeekDate]) as DateMonth,
    DatePart(Week, [WeekDate]) as DateWeek,
    sum(cast(MYTABLE.BASE_TRANSACTION_VALUE as int)) as Value,
    min(isnull(MYTABLE.CREATION_DATE,[WeekDate])) as DateT
from
    @WeekTable
    left outer join
    MYTABLE
    on 
    DatePart(Week,MYTABLE.CREATION_DATE) =DatePart(Week, [WeekDate])
    and
    DatePart(Year,MYTABLE.CREATION_DATE) =DatePart(Year, [WeekDate])
    and
    MYTABLE.SEGMENT2 like '00601'
    and
    MYTABLE.SEGMENT4 like '%'
group by 
    DatePart(Year, [WeekDate]) , 
    DatePart(Month, [WeekDate]), 
    DatePart(Week, [WeekDate])
order by
    DateYear, DateMonth, DateWeek