如何从三个单独的表中获取月度数据

时间:2014-04-30 17:39:43

标签: sql sql-server date

我有三个单独的表来存储数据。

表1(每周执行和存储。使用上周的数据(例如4/27到5/3))

Date         Value1     Value2
5/3/2014      56          13
5/10/2014     12          25
5/17/2014     90          52
5/24/2014     82          36
5/31/2014     76          98
6/7/2014      34          25
6/14/2014     13          63
6/21/2014     45          98
...

表2(每周执行和存储。使用上周的数据(例如4/27到5/3))

Date         Value3      Value4
5/3/2014       54          62
5/10/2014      43          36
5/17/2014      90          43
5/24/2014      54          35
5/31/2014      76          45
6/7/2014       34          43
6/14/2014      23          37
6/21/2014      34          56
...

表3(每天从4/27开始执行和存储等等)

Date         Value5      Value6
4/27/2014      56          45
4/28/2014      34          34
4/29/2014      23          34
4/30/2014      15          90
5/1/2014       34          23
5/2/2014       45          12
5/3/2014       46          35
5/4/2014       67          38
5/5/2014       34          23
...

如何编写一个按MONTH计算每列的查询?

我的表格如下:

Month     Value1     Value2     Value3     Value4     Value5     Value6
April      NULL       NULL       NULL       NULL       128         203
May        316        224        317        221        226         131

我的查询获得当月的第一个:

CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)

下个月的第一天:

CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE)

我在想我的查询可能是这样的:

SELECT
     DATENAME(MONTH, @DATE) as [Date],
     (SELECT [Value1] From [database].[dbo].[Table1]
          WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
          AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE)
     ) AS [VAL1],
     (SELECT [Value2] From [database].[dbo].[Table1]
          WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
          AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE)
     ) AS [VAL2],
     (SELECT [Value3] From [database].[dbo].[Table2]
          WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
          AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE)
     ) AS [VAL3],
     (SELECT [Value4] From [database].[dbo].[Table2]
          WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
          AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE)
     ) AS [VAL4],
     HOW TO DO VALUE5,
     HOW TO DO VALUE6
     INTO [database].[dbo].[tablemonthly]

我正在请求完成上述查询的帮助。

另外,如何在上面的查询中使用IIF语句,因此它只添加一次行,只更新Value5和Value6,因为它是DAILY并保持其他完整,因为它将是更大的一部分每天运行的存储过程?

我的三张桌子是:

enter image description here

4 个答案:

答案 0 :(得分:1)

此查询列出了VALUE#s的年份,月份和总计。

即使查询可能很长,它也会很快运行,因为连接操作在Table1,Table2和Table3的较小子集上(因为它们首先被过滤)。然后,使用Date列的Month和Year部分连接它们。最后,数据按月和年排序。

SELECT
COALESCE(DATENAME(YEAR, Table1.[Date]), DATENAME(YEAR, Table2.[Date]), DATENAME(YEAR, Table3.[Date])) AS Year
, COALESCE(DATENAME(MONTH, Table1.[Date]), DATENAME(MONTH, Table2.[Date]), DATENAME(MONTH, Table3.[Date])) AS Month 
, COALESCE(SUM(Table1.Value1), 0) Value1 
, COALESCE(SUM(Table1.Value2), 0) Value2 
, COALESCE(SUM(Table2.Value3), 0) Value3 
, COALESCE(SUM(Table2.Value4), 0) Value4 
, COALESCE(SUM(Table3.Value5), 0) Value5 
, COALESCE(SUM(Table3.Value6), 0) Value6 
FROM
(
  SELECT *
FROM Table1
WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 9, @DATE)) as DATE)
) Table1
FULL JOIN
(
  SELECT *
FROM Table2
WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 9, @DATE)) as DATE)
) Table2
ON DATENAME(MONTH, Table1.[Date]) = DATENAME(MONTH, Table2.[Date])
AND DATENAME(YEAR, Table1.[Date]) = DATENAME(YEAR, Table2.[Date])
FULL JOIN
(
  SELECT *
FROM Table3
WHERE [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE)
AND [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 9, @DATE)) as DATE)
) Table3
ON DATENAME(MONTH, Table1.[Date]) = DATENAME(MONTH, Table3.[Date])
AND DATENAME(YEAR, Table1.[Date]) = DATENAME(YEAR, Table3.[Date])
GROUP BY 
COALESCE(DATENAME(YEAR, Table1.[Date]), DATENAME(YEAR, Table2.[Date]), DATENAME(YEAR, Table3.[Date]))
, COALESCE(DATENAME(MONTH, Table1.[Date]), DATENAME(MONTH, Table2.[Date]), DATENAME(MONTH, Table3.[Date]))
ORDER BY 
COALESCE(DATENAME(YEAR, Table1.[Date]), DATENAME(YEAR, Table2.[Date]), DATENAME(YEAR, Table3.[Date]))
, MONTH(CAST(COALESCE(DATENAME(MONTH, Table1.[Date]), DATENAME(MONTH, Table2.[Date]), DATENAME(MONTH, Table3.[Date])) + ' 1 2014' AS DATETIME));

这是更新的 SQL Fiddle demo ,其中包含各种组合的数据。

答案 1 :(得分:1)

我认为最简单的方法是将表格合并在一起然后提取你想要的东西:

select datename(month, @date) as [date],
       max(value1) as value1, max(value2) as value2,
       max(value3) as value3, max(value4) as value4,
       sum(value5) as value5, sum(value6) as value6
into [database].[dbo].[tablemonthly]
from ((select [date], value1, value2, NULL as value3, NULL as value4, NULL as value5, NULL as value6
       from table1
      ) union all
      (select [date], NULL, NULL, value3, value4, NULL, NULL
       from table2
      ) union all
      (select [date], NULL, NULL, NULL, NULL, value5, value6
       from table3
      )
     ) t
where [Date] >= CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, @DATE) as DATE) and
      [Date] < CAST(DATEADD(d, - DATEPART(d, @DATE) + 1, DATEADD(m, 1, @DATE)) as DATE);

您可以使用group by同时执行多个月。

编辑:

获得多个月的查询是:

select year([date]) as yr, month([date]) as mon,
       max(value1) as value1, max(value2) as value2,
       max(value3) as value3, max(value4) as value4,
       sum(value5) as value5, sum(value6) as value6
into [database].[dbo].[tablemonthly]
from ((select [date], value1, value2, NULL as value3, NULL as value4, NULL as value5, NULL as value6
       from table1
      ) union all
      (select [date], NULL, NULL, value3, value4, NULL, NULL
       from table2
      ) union all
      (select [date], NULL, NULL, NULL, NULL, value5, value6
       from table3
      )
     ) t
group by year([date]), month([date])

(我也把这一年放进去避免含糊不清。)

这会将where替换为group by,并将相关更改替换为select

答案 2 :(得分:1)

为确保您获得所需的所有月份,您需要有一个参考表 - 在这里我使用CTE,但您可以制作表格。你也可以使用递归CTE来构建基于输入的表,而不是像我在这里那样对值进行硬编码。

注意我使用Gordon提到的技术将所有数据连接在一起。

WITH Ranges AS
(
   SELECT 'April' AS M, '4/1/2014' AS [START], '4/30/2014' AS [END]
  UNION ALL
   SELECT 'May', '5/1/2014', '5/31/2014'
-- etc
), Data AS
(
   select [date], value1, value2, NULL as value3, NULL as value4, NULL as value5, NULL as value6 from table1
 union all
   select [date], NULL, NULL, value3, value4, NULL, NULL from table2
 union all
   select [date], NULL, NULL, NULL, NULL, value5, value6 from table3
)
, Joined AS
(
   SELECT M as [Month], Value1, Value2, Value3, Value4, Value5, Value6
   FROM Ranges R
   JOIN Data D ON D.[DATE] >= R.[START] AND D.[Date] <= R.[END]
)
SELECT [Month], SUM(Value1), SUM(Value2), SUM(Value3), SUM(Value4), SUM(Value5), SUM(Value6)
FROM Joined
GROUP BY [Month]

答案 3 :(得分:1)

with MonthDataSummed(theMonth,value1,value2,value3,value4,value5,value6) as 
(
    select dateName(Month,date),Sum(value1),Sum(value2),null,null,null,null from table11
     group by dateName(Month,date)
     UNION all
     select dateName(Month,date),NULL,NULL,Sum(value3),Sum(value4),null,null 
     from table22
     group by dateName(Month,date)
     UNION all
     select dateName(Month,date),NULL,NULL,NULL,NULL,Sum(value5),Sum(value6) 
     from table33
     group by dateName(Month,date)
 )
,SummedFlattened(theMonth,value1,value2,value3,value4,value5,value6) as 
(
    select
    theMonth,Max(value1),Max(value2),Max(value3),Max(value4),Max(value5),Max(value6)
    from MonthDataSummed
    group by theMonth
)
SELECT * FROM SummedFlattened