如何在SQL中跨多个表聚合列值计数?

时间:2019-04-20 17:59:09

标签: sql

我有多个表(假设有10个表),如下所示:

process.env.MY_API_KEY

我想计算每个表中C = 1列的次数,并创建一个新表,如下所示:

********  ********        *********  
* day1 *  * day2 *  ....  * day10 *  
********  ********        *********  
A B C ..  A B C ..        A B C ...  
1 4 9 ..  7 6 8 ..        9 6 2 ...  
7 2 1 ..  0 2 1 ..        0 5 1 ...  
3 3 1 ..  0 9 7 ..        1 4 1 ...  
7 8 0 ..  1 6 5 ..        2 6 1 ...  
********  ********        *********  

类似这样的东西:

**********
* output *
**********
day   num  
 1     2  
 2     1  
 .     .  
 .     .  
10     3
**********

什么是最好的方法? (我是SQL新手)

2 个答案:

答案 0 :(得分:1)

request.query.orderBy.timestamp == 'ASC'

然后在其上进行COUNT(*)天的分组。

答案 1 :(得分:1)

我是新手,但会尽力而为:

假设您有第1天〜第3天,只有三个表:

SELECT
    1 AS day,
    COUNT(day1.C) AS num
FROM
    day1
WHERE day1.C = 1
UNION ALL
SELECT
    2 AS day,
    COUNT(day2.C) AS num
FROM
    day2
WHERE day2.C = 1
UNION ALL
SELECT
    3 AS day,
    COUNT(day3.C) AS num
FROM
    day3
WHERE day3.C = 1

这将为您提供:

    day    num
1    1      2
2    2      1
3    3      3