如何在SQL 2000中每周汇总数据

时间:2010-06-25 07:32:06

标签: sql sql-server tsql sql-server-2000

如何在MS-SQL 2000中每周汇总数据

我有一个表“Reports”,其中包含以下列。

TotalSubmissions,ZoneID,RptMonth,RptDay,RptYear
2,1,6,1,2010
1,1,6,2,2010
1,1,6,3,2010
1,2,6,1,2010
1,2,6,2,2010
2,2,6,3,2010
1,2,6,4,2010
1,4,6,1,2010
1,4,6,3,2010
1,4,6,4,2010

我希望每周为DateRange1到DateRange2的特定区域做一份报告

示例:我需要在2010年1月6日至2010年6月24日期间为zone2提交一周的提交总数。

请帮我完成上述任务。

此致

Mehboob Khan Afridi

3 个答案:

答案 0 :(得分:2)

我认为这会做你想要的。

我已经包含了一些用于测试的样本数据。

Create table #t
(
TotalSubmissions int,
ZoneId int,
RptMonth varchar(2),
RptDay varchar(2),
RptYear varchar(4)
)

Insert Into #t
Values (2,1,6,1,2010)
Insert Into #t
Values (1,1,6,2,2010)
Insert Into #t
Values (1,1,6,3,2010)
Insert Into #t
Values (1,2,6,1,2010)
Insert Into #t
Values (1,2,6,2,2010)
Insert Into #t
Values (2,2,6,3,2010)
Insert Into #t
Values (1,2,6,4,2010)
Insert Into #t
Values (1,4,6,1,2010)
Insert Into #t
Values (1,4,6,3,2010)
Insert Into #t
Values (1,4,6,4,2010)

declare @Date1 datetime,
        @Date2 datetime

Set @Date1 = '2010-06-01'
Set @Date2 = '2010-06-24'

Select  Sum(TotalSubmissions) as 'TotalSubmissions',
        ZoneId,
        DatePart(week, Cast((RptYear + '-' + RptMonth + '-' + RptDay) as datetime)) as 'WeekNumber',
        DatePart(year, Cast((RptYear + '-' + RptMonth + '-' + RptDay) as datetime)) as 'Year'

From #t
Where Cast((RptYear + '-' + RptMonth + '-' + RptDay) as datetime) >= @Date1
And Cast((RptYear + '-' + RptMonth + '-' + RptDay) as datetime) <= @Date2
Group By datepart(year,Cast((RptYear + '-' + RptMonth + '-' + RptDay) as datetime)),
        ZoneID, DatePart(week, Cast((RptYear + '-' + RptMonth + '-' + RptDay) as datetime))

drop table #t

答案 1 :(得分:1)

  

示例:我需要在2010年1月6日至2010年6月24日期间为zone2提交一周的提交总数。

这个T-SQL应该这样做:
(由于'yyyy-mm-dd'不是与语言无关的日期格式,因此我将日期保留在一个位置。)

DECLARE
    @StartDate  DATETIME,
    @EndDate    DATETIME,
    @TargetZone INT

SET @StartDate  = '2010-06-01'
SET @EndDate    = '2010-06-24'
SET @TargetZone = 2


SELECT
    SUM (dwd.TotalSubmissions)      AS TotalSubmissions,
    dwd.ZoneID,
    DATEPART (week, dwd.ReportDate) AS WeekOfTheYear
FROM
    (
        SELECT
            r.TotalSubmissions,
            r.ZoneID,
            CAST ( (CAST (r.RptYear AS varchar(4)) + '-' + CAST (r.RptMonth AS varchar(2)) + '-' + CAST (r.RptDay AS varchar(2))) AS DATETIME)  AS ReportDate
        FROM
            Reports r
        WHERE
            r.ZoneID    = @TargetZone
    )
    AS dwd
WHERE
    dwd.ReportDate      >= @StartDate
AND
    dwd.ReportDate      <= @EndDate
GROUP BY
    dwd.ZoneID,
    DATEPART (year, dwd.ReportDate),
    DATEPART (week, dwd.ReportDate)

样本数据:

CREATE TABLE Reports
(
    TotalSubmissions    INT,
    ZoneID              INT,
    RptMonth            INT,
    RptDay              INT,
    RptYear             INT
)

INSERT INTO
    Reports (TotalSubmissions, ZoneID, RptMonth, RptDay, RptYear)
SELECT
    2, 1, 6, 1, 2010      UNION ALL SELECT
    1, 1, 6, 2, 2010      UNION ALL SELECT
    1, 1, 6, 3, 2010      UNION ALL SELECT
    1, 2, 6, 1, 2010      UNION ALL SELECT
    1, 2, 6, 2, 2010      UNION ALL SELECT
    2, 2, 6, 3, 2010      UNION ALL SELECT
    1, 2, 6, 4, 2010      UNION ALL SELECT
    1, 4, 6, 1, 2010      UNION ALL SELECT
    1, 4, 6, 3, 2010      UNION ALL SELECT
    1, 4, 6, 4, 2010    


INSERT INTO Reports (TotalSubmissions, ZoneID, RptMonth, RptDay,    RptYear)
SELECT               TotalSubmissions, ZoneID, RptMonth, RptDay+10, RptYear
FROM        Reports

INSERT INTO Reports (TotalSubmissions, ZoneID, RptMonth, RptDay,    RptYear)
SELECT               TotalSubmissions, ZoneID, RptMonth, RptDay+20, RptYear
FROM        Reports

DELETE FROM Reports
WHERE       RptDay > 30

答案 2 :(得分:0)

一周的哪一天是'你'周的开始?

根据登录的区域设置以及会话选项DATEFIRST,您的查询结果每次都可能不同。

这是一个基于登录的一周第一天定义的解决方案。

SELECT SUM(TotalSubmissions) AS TotalSubmissions,
       ZoneId, 
       DATEADD(DAY, -DATEPART(WEEKDAY, Cast(RptYear + '-' + RptMonth + '-' + RptDay AS DATETIME)) + 1,
        Cast(RptYear + '-' + RptMonth + '-' + RptDay AS DATETIME)) AS WeekBeginning

FROM Reorts

WHERE CAST(RptYear + '-' + RptMonth + '-' + RptDay AS DATETIME) >= '2010-06-01'
  AND CAST(RptYear + '-' + RptMonth + '-' + RptDay AS DATETIME) <= '2010-06-24'

GROUP BY ZoneId,
  DATEADD(DAY, -DATEPART(WEEKDAY, Cast(RptYear + '-' + RptMonth + '-' + RptDay AS DATETIME)) + 1,
    Cast(RptYear + '-' + RptMonth + '-' + RptDay AS DATETIME))