将特定月份拆分为数周和数天

时间:2013-11-25 07:16:46

标签: sql-server telerik

假设我参加11月份。我需要显示第一行,如下所示。其余行应该是数据库中的数据。行显示发布日期和数量,如果数据出现在这些日期,它应该显示。我还需要显示每周总数和每月总数。这显示特定年份的特定月份。 我计划编写一个数据查询,并通过交叉表 Telerik 中实现。请同样提出您的想法。 输入: -

POST DATE                Amount($)
2013-11-01 00:00:00.000  50.00
2013-11-04 09:30:12.000  10.00
2013-11-05 11:04:00.000  20.00
2013-11-06 00:00:00.000  30.00
2013-11-07 00:00:00.000  40.00
2013-11-08 00:00:00.000  10.00
2013-11-11 00:00:00.000  10.00
2013-11-12 00:00:00.000  10.00
2013-11-15 00:00:00.000  10.00
.
.
.

数据(11月份)应该如下所示: -

01-Nov  Weekly Total    04-Nov  05-Nov  06-Nov  07-Nov  08-Nov  Weekly Total    11-Nov  12-Nov  13-Nov  14-Nov  15-Nov  Weekly Total    18-Nov  19-Nov  20-Nov  21-Nov  22-Nov  Weekly Total    25-Nov  26-Nov  27-Nov  29-Nov  Weekly Total    MTD Total
50$       50$             10$    20$     30$     40$      10$     110$            10$     10$                    10$       30$

提前致谢

1 个答案:

答案 0 :(得分:0)

MSDN Date and Time Data Types and Functions (Transact-SQL)

DECLARE @TempTrans AS Table (
    ID INT NOT NULL IDENTITY PRIMARY KEY
    ,DateTimeTrans DATETIME NOT NULL
    ,Amount MONEY NOT NULL
)

INSERT INTO @TempTrans (DateTimeTrans, Amount)
VALUES ('20131101', 50)
    ,('20131104 09:30:12',10)
    ,('20131104 11:04:00',15)
    ,('20131105',20)
    ,('20131106',30)
    ,('20131111',10)
    ,('20131115',80)
    ,('20131119',70)
    ,('20131123',60)
    ,('20111129 10:08:04',25)

SELECT *
    ,CASE
        WHEN [weekNumber] IS NULL AND [Date] IS NULL THEN 'MTD Total'
        WHEN [Date] IS NULL THEN 'Weekly Total'
        ELSE CAST([Date] as varchar(30)) 
    END as [Description]
FROM (
    SELECT DATEPART(week,DateTimeTrans) as weekNumber,CAST(DateTimeTrans as date) as Date, SUM(Amount) as Amount
    FROM @TempTrans
    WHERE DATEPART(MONTH,DateTimeTrans) = 11
    GROUP BY DATEPART(week,DateTimeTrans) ,CAST(DateTimeTrans as date)
    WITH Rollup
) grp
GO