使用日期加入多个表

时间:2013-05-14 12:39:29

标签: postgresql join

我有三张桌子

**Table A**
+-----------------+
| Name      | ID  |
+------------------
| A1        | 1   |
| A2        | 2   |
| A3        | 3  |
+------------------ 

**Table B**                  
+---------------------------------------+-----+-------+
| TimeStamp                             | Type|    ID |
+---------------------------------------+-----+-------+
| 2013-05-10 08:10:10.161302-04         | B1   |  1   |
| 2013-05-10 09:20:10.171302-04         | B1   |  1   |
| 2013-05-12 08:10:10.161302-04         | B2   |  3   | 
| 2013-05-13 08:10:10.161302-04         | B2   |  3   |
| 2013-05-14 08:10:10.161302-04         | B1   |  2   |
+---------------------------------------+------+------+

**Table C**                  
+---------------------------------------+-----+-------+
| TimeStamp                             | Type|  ID   |
+---------------------------------------+-----+-------+
| 2013-05-12 08:10:10.161302-04         | C1   |  1   |
| 2013-05-12 09:20:10.171302-04         | C2   |  2   |
| 2013-05-12 08:10:10.161302-04         | C1   |  2   |
| 2013-05-12 08:10:10.161302-04         | C3   |  1   |
| 2013-05-13 08:10:10.161302-04         | C1   |  2   |
| 2013-05-13 08:10:10.161302-04         | C3   |  3   |
| 2013-05-14 08:10:10.161302-04         | C1   |  1   |
+---------------------------------------+------+------+-

**Expected Result of Query**
+---------------------------------------+-----------------------------+
| Date                | B.count(B1 entries)   |  C.count(c1 entries)  |
+---------------------+-----------------------+-----------------------+
| 2013-05-10          |        2              |    0                  |
| 2013-05-12          |        0              |    2                  |
| 2013-05-13          |        0              |    1                  |
| 2013-05-14          |        1              |    1                  |
+---------------------+-----------------------+-----------------------+
  • 表B和表C中的ID参见表A.ID
  • 我正在寻找的查询是:每天计数表B中B1条目的数量和表C中C1条目的数量{1,2}

欢迎任何建议。

2 个答案:

答案 0 :(得分:0)

怎么样:

SELECT a.ID, a.Name, b.Date, ISNULL(b.BCount, 0), ISNULL(c.CCount, 0)
FROM TableA a
LEFT JOIN (
    SELECT ID, CONVERT(DATE, TimeStamp) AS Date, Type, COUNT(*) AS BCount
    FROM TableB
    GROUP BY ID, CONVERT(DATE, TimeStamp), Type
    HAVING Type = 'B1'
) b ON a.ID = b.ID
LEFT JOIN (
    SELECT ID, CONVERT(DATE, TimeStamp) AS Date, Type, COUNT(*) AS CCount
    FROM TableC
    GROUP BY ID, CONVERT(DATE, TimeStamp), Type
    HAVING TYPE = 'C1'
) c ON b.ID = c.ID AND b.Date = c.Date
WHERE a.ID IN (1, 2)

答案 1 :(得分:0)

尝试此查询

select 
case when b.dtpart is null then a.dtpart else b.dtpart end as DTPart,
case when a.cnt is null then 0 else a.cnt end as aCnt, 
case when b.cnt is null then 0 else b.cnt end as bCNT 
from 
(select 
date_trunc('day', "TimeStamp" ) as dtpart, count(*) as cnt 
from tbl1 
where "ID" in (1,2)
group by date_trunc('day', "TimeStamp" )) a 
full join 
(select 
date_trunc('day', "TimeStamp" ) as dtpart, count(*) as cnt 
from tbl2 
where "ID" in (1,2)
group by date_trunc('day', "TimeStamp" )) b
on a.dtpart=b.dtpart

SQL FIDDLE

|                     DTPART | ACNT | BCNT |
--------------------------------------------
| May, 14 2013 00:00:00+0000 |    1 |    1 |
| May, 10 2013 00:00:00+0000 |    2 |    0 |
| May, 13 2013 00:00:00+0000 |    0 |    1 |
| May, 12 2013 00:00:00+0000 |    0 |    4 |