我想查询一个列很少(例如下面的4列)的表,我想从中创建一个快照表,该表可以提供每天对属性计数的深入了解
userId | attr1 | attr2 | attr3 |
---|---|---|---|
1 | true | Hello | Dog |
2 | false | Hi | |
3 | Hello | ||
4 | false | Cat | |
5 | true | Hi | Dog |
6 | false | Cat | |
7 | Hi | ||
8 | false | Dog | |
9 | Cat | ||
10 | false | Hello | Cat |
例如从上面
userIdCount | attrName | dateWhenSnapshotInserted |
---|---|---|
7 | attr1 | April 16, 2024 |
6 | attr2 | April 16, 2024 |
7 | attr3 | April 16, 2024 |
在这种情况下,我们的原始表中可能有数百万行,包含超过100列,我们希望每天用每个属性和创建此快照时的日期的非空值的用户计数填充快照表.
我的疑问如下:-
select COUNT(DISTINCT userId) as userIdCount from orig_table where
attr1 IS NOT NULL UNION Select COUNT(DISTINCT userId) as userIdCount
from orig_table where attr2 IS NOT NULL UNION Select COUNT(DISTINCT
userId) as userIdCount from orig_table where attr3 IS NOT NULL;
是否有优化的方法将多个 Select 查询写在一起.我首先创建多个 Select 查询并将其一一插入到输出表中,但这似乎是一个糟糕的模式.请建议如何实现这一点,以便可以扩展.
ps:新手(第一次探索DB查询)