查询总和3个连接

时间:2017-06-21 18:02:39

标签: sql ms-access join

我需要对第3列求和并在查询中显示 按日期和ID分组

这是exi表

+------+------------+----------+
|exiID |  exiDate   | exiavail |
+------+------------+----------+
| 011  | 01/22/2017 |   255.00 |
+------+------------+----------+
| 011  | 01/23/2017 |    45.00 |
+------+------------+----------+
| 012  | 01/23/2017 |   102.00 |
+------+------------+----------+
| 012  | 01/23/2017 |    55.00 |
+------+------------+----------+

这是rec表

+------+------------+----------+
|recID |  recDate   | recreciv |
+------+------------+----------+
| 012  | 01/22/2017 |    15.00 |
+------+------------+----------+
| 011  | 01/23/2017 |    12.00 |
+------+------------+----------+
| 012  | 01/23/2017 |    32.00 |
+------+------------+----------+
| 011  | 01/24/2017 |     9.00 |
+------+------------+----------+

这是sal表

+------+------------+----------+
|salID |   salDate  | salreciv |
+------+------------+----------+
| 011  | 01/22/2017 |    21.00 |
+------+------------+----------+
| 012  | 01/23/2017 |     7.00 |
+------+------------+----------+
| 012  | 01/23/2017 |    11.00 |
+------+------------+----------+
| 011  | 01/24/2017 |     2.00 |
+------+------------+----------+

查询所需的连接表和SUM exiavail,recreciv,salreciv 按日期和ID分组:( 查询结果必须像这样

+------------+------+----------+----------+----------+
|     Date   |  ID  | exiavail | recreciv | salreciv |
+------------+------+----------+----------+----------+
| 01/22/2017 | 011  |   255.00 |     0.00 |    21.00 |
+------------+------+----------+----------+----------+
| 01/22/2017 | 012  |     0.00 |    15.00 |     0.00 |
+------------+------+----------+----------+----------+
| 01/23/2017 | 011  |    45.00 |    12.00 |     0.00 |
+------------+------+----------+----------+----------+
| 01/23/2017 | 012  |   157.00 |    32.00 |    18.00 |
+------------+------+----------+----------+----------+
| 01/24/2017 | 011  |     0.00 |     9.00 |     2.00 |
+------------+------+----------+----------+----------+
| 01/24/2017 | 012  |     0.00 |     0.00 |     0.00 |
+------------+------+----------+----------+----------+

3 个答案:

答案 0 :(得分:0)

您可以在日期和ID上使用JOIN (假设3个表中的ID匹配)

select 
  a.exiDate as date
  , a.exiID as id 
  , sum(a.exiavail) exiavail
  , sum(b.recreciv) recreciv
  , sum(c.salreciv) salreciv
from exi as a
left join rec as b on a.date = b.date and a.exiID = b.recID
left join sal as c on a.date = c.date and a.exiID = c.salID
group by a.exiDate,  a.exiID

答案 1 :(得分:0)

示例数据显示ID和日期对在所有3个表中都不相同。需要所有可能对的数据集,然后将每个表连接到该数据集,并在两个字段上使用复合连接,并在该查询中执行求和和分组。

UNION查询可以构建所有对数据集。

SELECT exiID AS ID, exiDate AS TxnDate AS Source FROM exi
UNION SELECT recID, recDate FROM rec
UNION SELECT salID, salDate FROM sal;

答案 2 :(得分:0)

看看这是否有帮助。您需要更新表名,但我希望这会让您在正确的轨道上进行操作。基本上,您需要总结每个表中的每个值和GROUP by ID和Date

    if object_id ('TESTING1') IS NOT NULL DROP TABLE TESTING1
    if object_id ('TESTING2') IS NOT NULL DROP TABLE TESTING2
    Create table TESTING1
    (id INT,
     Date DATETIME,
      val  INT)

       INSERT INTO dbo.testing1 values ('11', '01/23/2017', '255')
       INSERT INTO dbo.testing1 values ('11', '01/23/2017', '45')
       INSERT INTO dbo.testing1 values ('12', '01/22/2017', '102')
       INSERT INTO dbo.testing1 values ('12', '01/23/2017', '55')

       Create table TESTING2
       (id INT,
         Date DATETIME,
       val1 INT)


  INSERT INTO dbo.testing2 values ('12', '01/22/2017', '15')
  INSERT INTO dbo.testing2 values ('11', '01/23/2017', '12')
  INSERT INTO dbo.testing2 values ('12', '01/24/2017', '32')
  INSERT INTO dbo.testing2 values ('11', '01/23/2017', '9')



  SELECT distinct a.id, a.date, c.table1val,  b.table2val
  from testing1 a
  join (select id, date, sum(val1) as table2val
    from dbo.testing2
    group by id, date) b on b.id = a.id and b.date = a.date

   join (select id, date, sum(val) as table1val
    from dbo.testing1
     group by id, date) c on c.id = a.id and c.date = a.date