如何逐月从sql获取数据结果并分为几周

时间:2015-04-16 07:16:02

标签: sql sql-server sql-server-2012

我创建了一个脚本,可以在用户声明的月份内获得总销售额,并在每周进行分组。但问题是总数超出了我宣布的日期范围。因为宣布的月份不是7的分红(这是一周内的天数)。现在我想仅根据我的日期范围得到结果。

这是我的示例查询:

Set @starttime = '2014-10-01'
Set @endtime = '2014-10-31'

while @endtime > @starttime
begin
    print @starttime
    set @xendtime = DATEADD(DAY, 7,@starttime)

    print @xendtime
    set @starttime = DATEADD(DAY, 7,@starttime) 
end

这是结果

Oct  1 2014 12:00AM ---@StartDate
Oct  7 2014 12:00AM ---@xEndDate
Oct  8 2014 12:00AM ---@StartDate
Oct 14 2014 12:00AM ---@xEndDate
Oct 15 2014 12:00AM ---@StartDate
Oct 21 2014 12:00AM ---@xEndDate
Oct 22 2014 12:00AM ---@StartDate
Oct 28 2014 12:00AM ---@xEndDate
Oct 29 2014 12:00AM ---@StartDate
Nov  4 2014 12:00AM ---@xEndDate <----- I want this to end in Oct 31.
你能帮我解决这个问题吗?请

5 个答案:

答案 0 :(得分:0)

尝试:

if(Month(@xendtime) = month(@endtime))
    begin
    print @xendtime
    end
    else
    begin
    print @endtime
    end

完成查询:

declare @starttime date
declare @endtime date
declare @xendtime date

Set @starttime = '2014-10-01'
Set @endtime = '2014-10-31'

while @endtime>@starttime
begin
    print @starttime
    set @xendtime = DATEADD(DAY, 7,@starttime)
   set @starttime = DATEADD(DAY, 7,@starttime)
if(Month(@xendtime) = month(@endtime))
    begin
    print @xendtime
    end
    else
    begin
    print @endtime
    end
end

答案 1 :(得分:0)

您应该使用DATEPART之类的:

DATEPART(WEEK, DATEADD(DAY, 7,@starttime))

答案 2 :(得分:0)

这是您想要的结果,您的查询几乎没有修改。

declare @starttime datetime

declare @endtime datetime declare @time datetime Set @starttime = '2014-10-01' Set @endtime = '2014-10-31'

while @endtime > @starttime begin print @starttime set @time = DATEADD(DAY, 6,@starttime) If @time<@endtime Begin print @time set @starttime = DATEADD(DAY, 1,@time) End Else begin print @endtime set @endtime = @starttime End end

希望这会对你有所帮助。

答案 3 :(得分:0)

可能是这个作品

  DECLARE @startDate DATETIME, @endDate DATETIME
    SELECT @startDate = '2014-10-01', @endDate = '2014-10-31'
    ;WITH DateCalendar AS (
        SELECT @startDate AS dt
        UNION ALL
        SELECT dt + 1 FROM DateCalendar
        WHERE dt + 1 <= @endDate
    )
    SELECT 
     NameMonth = DATENAME(Month, dt)
    ,NameDay = convert(varchar(20),dt,100)
    FROM DateCalendar
    WHERE DATENAME (Weekday,dt) IN ('Sunday','Saturday')
    Option(MaxRecursion 0)

答案 4 :(得分:0)

DECLARE @starttime date
,@endtime date
,@xendtime date

Set @starttime = '2014-10-01'
Set @endtime = '2014-10-31'


while @endtime > @starttime
begin
    set @xendtime = CASE WHEN DATEADD(DAY, 6,@starttime) >= @endtime THEN @endtime ELSE DATEADD(DAY, 6,@starttime) END
    SELECT @starttime, @xendtime

    set @starttime = DATEADD(DAY, 1,@xendtime)
end
GO

使用CTE:

DECLARE @starttime date
,@endtime date
,@xendtime date

Set @starttime = '2014-10-01'
Set @endtime = '2014-10-31'

;WITH dt as (
SELECT @starttime as StartDate , CASE WHEN DATEADD(DAY, 6,@starttime) >= @endtime THEN @endtime ELSE DATEADD(DAY, 6,@starttime) END EndDate
UNION ALL
SELECT DATEADD(DAY, 1,dt.EndDate), CASE WHEN DATEADD(DAY, 7,dt.EndDate) >= @endtime THEN @endtime ELSE DATEADD(DAY, 7,dt.EndDate) END
FROM dt
WHERE dt.EndDate < @endtime
)

SELECT *
FROM dt
OPTION (MAXRECURSION 0);