使用sum()函数和关系表正常工作

时间:2018-05-25 08:27:29

标签: sql sqlite

我创建了以下4个表:

CREATE TABLE `Name` ( `id` INTEGER PRIMARY KEY AUTOINCREMENT, `name` INTEGER );
insert into `Name` VALUES(1,'test1');
insert into `Name` VALUES(2,'test2');


CREATE TABLE "Table1" ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `nameID` INTEGER, `amnt1` INTEGER, `amnt2` INTEGER, `amnt3` INTEGER, `amnt4` INTEGER, `amnt5` INTEGER, `amnt6` INTEGER, `entryDate` TEXT );
INSERT INTO Table1 VALUES(NULL,1,1,1,1,1,1,1,'2018-04-01');
INSERT INTO Table1 VALUES(NULL,2,1,1,1,1,1,1,'2018-05-01');
INSERT INTO Table1 VALUES(NULL,1,1,1,1,1,1,1,'2018-05-06');

CREATE TABLE "Table2" ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `nameID` INTEGER, `amnt1` INTEGER, `amnt2` INTEGER, `amnt3` INTEGER, `amnt4` INTEGER, `amnt5` INTEGER, `amnt6` INTEGER, `entryDate` TEXT );
INSERT INTO Table2 VALUES(NULL,1,1,1,1,1,1,1,'2018-04-02');
INSERT INTO Table2 VALUES(NULL,1,1,1,1,1,1,1,'2018-05-05');
INSERT INTO Table2 VALUES(NULL,2,2,2,2,2,2,1,'2018-05-06');


CREATE TABLE "Table3" ( `id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, `nameID` INTEGER, `amnt1` INTEGER, `amnt2` INTEGER, `amnt3` INTEGER, `amnt4` INTEGER, `amnt5` INTEGER, `amnt6` INTEGER, `entryDate` TEXT );
INSERT INTO Table3 VALUES(NULL,1,1,1,1,1,1,1,'2018-04-02');
INSERT INTO Table3 VALUES(NULL,1,1,1,1,1,1,1,'2018-05-05');

我正在尝试添加与Table2匹配的Table1列数据(例如Table1.amnt1 + Table2.amnt1)列。然后添加Table3进行求和。

我不是专家数据库管理员,我尝试过:

SELECT sum(amnt1),sum(amnt2),sum(amnt3),sum(amnt4),sum(amnt5),sum(amnt6) 
FROM (select 
      Table1.amnt1+Table2.amnt1 as amnt1,Table1.amnt2+Table2.amnt2 as amnt2,Table1.amnt3+Table2.amnt3 as amnt3,
      Table1.amnt4+Table2.amnt4 as amnt4,Table1.amnt5+Table2.amnt5 as amnt5,Table1.amnt6+Table2.amnt6 as amnt6
      FROM Table1 JOIN Table2 ON Table1.nameID=Table2.nameID
      where Table1.entryDate between '2018-05-01' and '2018-05-20'

      UNION ALL 
      select amnt1,amnt2,amnt3,amnt4,amnt5,amnt6 
      FROM Table3 
      where entryDate between '2018-05-01' and '2018-05-20' )

它给出了错误的计算。我在关系子查询中看到我没有使用entryDate。似乎我的查询不正确。

SQL FIDDLE DEMO

1 个答案:

答案 0 :(得分:0)

我只想扩展UNION ALL部分:

SELECT sum(amnt1),sum(amnt2),sum(amnt3),sum(amnt4),sum(amnt5),sum(amnt6) 
FROM (select amnt1,amnt2,amnt3,amnt4,amnt5,amnt6 
      FROM Table1 
      where entryDate between '2018-05-01' and '2018-05-20'
      UNION ALL
      SELECT amnt1,amnt2,amnt3,amnt4,amnt5,amnt6 
      FROM Table2
      where entryDate between '2018-05-01' and '2018-05-20' 
      UNION ALL 
      select amnt1,amnt2,amnt3,amnt4,amnt5,amnt6 
      FROM Table3 
      where entryDate between '2018-05-01' and '2018-05-20'     
     ) s  

<强> SQLFiddle Demo

编辑:

  

如果每个表都有不同的列怎么办?例如,Table2具有amnt1,amnt2,amount7,amnt9。如果表1或表3中不存在这些列名,那么我想获取现有数据。

然后你只需把0作为那个地方(这里amt7的例子只在table2中可用):

SELECT sum(amnt1),sum(amnt2),sum(amnt3),sum(amnt4),sum(amnt5),sum(amnt6),
       sum(amt7) 
FROM (select amnt1,amnt2,amnt3,amnt4,amnt5,amnt6,0 AS amt7
      FROM Table1 
      where entryDate between '2018-05-01' and '2018-05-20'
      UNION ALL
      SELECT amnt1,amnt2,amnt3,amnt4,amnt5,amnt6,amt7
      FROM Table2
      where entryDate between '2018-05-01' and '2018-05-20' 
      UNION ALL 
      select amnt1,amnt2,amnt3,amnt4,amnt5,amnt6,0
      FROM Table3 
      where entryDate between '2018-05-01' and '2018-05-20'     
     ) s