1

私は3つのテーブルを持っています

**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 の ID {1,2} の C1 エントリの数

どんな提案でも大歓迎です。

4

2 に答える 2

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)
于 2013-05-14T12:46:41.960 に答える
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フィドル

|                     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 |
于 2013-05-14T13:04:40.327 に答える