sql query按日期从多个表中选择列的sum()

时间:2016-09-05 05:23:19

标签: sql sql-server

所以我有4个表,我只想得到每个表的每一列的总和,并按日期分组,这样我就可以得到合适的数量。例如:

Table 1
ID|Amount1|Amount2|  forDate |
1 |  12   |   14  |2016-09-05|
2 |  13   |   15  |2016-09-05|

Table 2
ID|Amount3|Amount4|Amount5|  forDate |
1 |  15   |   16  |  17   |2016-09-05|

Table 3
ID|Amount6|  forDate |
1 |  16   |2016-09-05|
2 |  17   |2016-09-05|


Table 4
ID|Amount7|  forDate |
1 |   18  |2016-09-05|

我无法弄清楚我应该如何输出这个。

|   Date   |Amount1|Amount2|Amount3|Amount4|Amount5|Amount6|Amount7|
|2016-09-05|   25  |   29  |   15  |   16  |   17  |   33  |  18   |

总数应按日期分组。求助。

补充:我也希望有一个Where子句。我试过@Unnikrishnan R的答案,但它给了我这个输出:

|   Date   |Amount1|Amount2|Amount3|Amount4|Amount5|Amount6|Amount7|
|2016-09-05|   25  |   29  |   30  |   32  |   34  |   33  |  36   |

4 个答案:

答案 0 :(得分:0)

如果你想在table1中根据'for value'得到SUM()结果,请使用下面的脚本。

 SELECT t1.ForDate, SUM(DISTINCT ISNULL(AMOUNT1,0)) AMOUNT1 ,SUM(DISTINCT ISNULL(Amount2,0)) AMOUNT2,SUM(DISTINCT ISNULL(AMOUNT3,0)) AMOUNT3
      ,SUM( DISTINCT ISNULL(Amount4,0)) AMOUNT4,SUM(DISTINCT ISNULL(AMOUNT5,0)) AMOUNT5,SUM(DISTINCT ISNULL(Amount6,0)) AMOUNT6,SUM(DISTINCT ISNULL(Amount7,0)) AMOUNT7
FROM table1 t1
LEFT JOIN table2 t2 on  t1.fordate=t2.fordate
LEFT JOIN table3 t3 on  t1.fordate=t3.fordate
LEFT JOIN table4 t4 on  t1.fordate=t4.fordate
GROUP BY t1.forDate

如果要从所有表中包含'fordate'并需要聚合SUM(),请使用以下脚本。

with cte_1
as
(SELECT ForDate, SUM(AMOUNT1) AMOUNT1 ,SUM(Amount2) AMOUNT2,0 AMOUNT3
      ,0 AMOUNT4,0 AMOUNT5,0 AMOUNT6,0 AMOUNT7
FROM table1 
GROUP BY forDate

UNION ALL

SELECT ForDate, 0 AMOUNT1 ,0 AMOUNT2,SUM(AMOUNT3) AMOUNT3
      ,SUM(Amount4) AMOUNT4,SUM(AMOUNT5) AMOUNT5,0 AMOUNT6,0 AMOUNT7
FROM table2
GROUP BY forDate


UNION ALL


SELECT ForDate, 0 AMOUNT1 ,0 AMOUNT2,0 AMOUNT3
      ,0 AMOUNT4,0 AMOUNT5,SUM(Amount6) AMOUNT6,0 AMOUNT7
FROM table3 
GROUP BY forDate

UNION ALL 

SELECT ForDate, 0 AMOUNT1 ,0 AMOUNT2,0 AMOUNT3
      ,0 AMOUNT4,0 AMOUNT5,0 AMOUNT6,SUM(Amount7) AMOUNT7
FROM table4
GROUP BY forDate)

SELECT forDate,SUM(AMOUNT1) AMOUNT1 ,SUM(Amount2) AMOUNT2,SUM(AMOUNT3) AMOUNT3
      ,SUM(Amount4) AMOUNT4,SUM(AMOUNT5) AMOUNT5,SUM(Amount6) AMOUNT6,SUM(Amount7) AMOUNT7
FROM cte_1
GROUP BY cte_1

答案 1 :(得分:0)

这适用于SQL Server:

CREATE TABLE T1 (ID int, Amount1 int, Amount2 int, forDate date);
CREATE TABLE T2 (ID int, Amount3 int, Amount4 int, Amount5 int, forDate date);
CREATE TABLE T3 (ID int, Amount6 int, forDate date);
CREATE TABLE T4 (ID int, Amount7 int, forDate date);

INSERT INTO T1 VALUES (1, 12, 14, '2016-09-05');
INSERT INTO T1 VALUES (2, 13, 15, '2016-09-05');
INSERT INTO T2 VALUES (1, 15, 16, 17, '2016-09-05');
INSERT INTO T3 VALUES (1, 16, '2016-09-05');
INSERT INTO T3 VALUES (2, 17, '2016-09-05');
INSERT INTO T4 VALUES (1, 18, '2016-09-05');

WITH 
TA AS
    (SELECT forDate, SUM(Amount1) AS Amount1, SUM(Amount2) AS Amount2
     FROM T1
     GROUP BY forDate
    ),
TB AS
    (SELECT forDate, SUM(Amount3) AS Amount3, SUM(Amount4) AS Amount4, SUM(Amount5) AS Amount5
     FROM T2
     GROUP BY forDate 
    ),
TC AS
    (SELECT forDate, SUM(Amount6) AS Amount6 
     FROM T3
     GROUP BY forDate
    ),
TD AS
    (SELECT forDate, SUM(Amount7) AS Amount7
     FROM T4
     GROUP BY forDate
    )
SELECT TB.forDate, TA.Amount1, TA.Amount2, TB.Amount3, TB.Amount4, TB.Amount5, TC.Amount6, TD.Amount7
FROM TA, TB, TC, TD
WHERE TA.forDate = TB.forDate
  AND TB.forDate = TC.forDate
  AND TC.forDate = TD.forDate

答案 2 :(得分:0)

使用 UNION ALL ,然后 GROUP BY

CREATE TABLE #tbl1 (ID int, Amount1 int, Amount2 int, forDate date);
CREATE TABLE #tbl2 (ID int, Amount3 int, Amount4 int, Amount5 int, forDate date);
CREATE TABLE #tbl3 (ID int, Amount6 int, forDate date);
CREATE TABLE #tbl4 (ID int, Amount7 int, forDate date);

INSERT INTO #tbl1 VALUES (1, 12, 14, '2016-09-05');
INSERT INTO #tbl1 VALUES (2, 13, 15, '2016-09-05');
INSERT INTO #tbl2 VALUES (1, 15, 16, 17, '2016-09-05');
INSERT INTO #tbl3 VALUES (1, 16, '2016-09-05');
INSERT INTO #tbl3 VALUES (2, 17, '2016-09-05');
INSERT INTO #tbl4 VALUES (1, 18, '2016-09-05');

SELECT 
    X.forDate,
    SUM(Amount1) AS Amount1,
    SUM(Amount2) AS Amount2,
    SUM(Amount3) AS Amount3,
    SUM(Amount4) AS Amount4,
    SUM(Amount5) AS Amount5,
    SUM(Amount6) AS Amount6,
    SUM(Amount7) AS Amount7
FROM
(
    SELECT 
        forDate,
        ISNULL(Amount1,0) AS Amount1,
        ISNULL(Amount2,0) AS Amount2,
        0 AS Amount3,
        0 AS Amount4,
        0 AS Amount5,
        0 AS Amount6,
        0 AS Amount7
    FROM #tbl1
    UNION ALL
    SELECT 
        forDate,
        0 AS Amount1,
        0 AS Amount2,
        ISNULL(Amount3,0) AS Amount3,
        ISNULL(Amount4,0) AS Amount4,
        ISNULL(Amount5,0) AS Amount5,
        0 AS Amount6,
        0 AS Amount7
    FROM #tbl2
    UNION ALL
    SELECT 
        forDate,
        0 AS Amount1,
        0 AS Amount2,
        0 AS Amount3,
        0 AS Amount4,
        0 AS Amount5,
        ISNULL(Amount6,0) AS Amount6,
        0 AS Amount7
    FROM #tbl3
    UNION ALL
    SELECT 
        forDate,
        0 AS Amount1,
        0 AS Amount2,
        0 AS Amount3,
        0 AS Amount4,
        0 AS Amount5,
        0 AS Amount6,
        ISNULL(Amount7,0) AS Amount7
    FROM #tbl4
)X
GROUP By X.forDate

Drop table #tbl1,#tbl2,#tbl3,#tbl4

答案 3 :(得分:0)

我的解决方案如下

select
    forDate,
    SUM(Amount1) Amount1, SUM(Amount2) Amount2, SUM(Amount3) Amount3, SUM(Amount4) Amount4, SUM(Amount5) Amount5, SUM(Amount6) Amount6, SUM(Amount7) Amount7
from (
select forDate, SUM(Amount1) Amount1, SUM(Amount2) Amount2, null Amount3, null Amount4, null Amount5, null Amount6, null Amount7 from Table1 Group By forDate
union all
select forDate, null, null, SUM(Amount3) Amount3, SUM(Amount4) Amount4, SUM(Amount5) Amount5, null, null from Table2 Group By forDate
union all
select forDate, null, null, null, null, null, SUM(Amount6) Amount6, null from Table3 Group By forDate
union all
select forDate, null, null, null, null, null, null, SUM(Amount7) Amount7 from Table4 Group By forDate
) alldata
group by forDate

输出将为

enter image description here