没有这种合并的经验,但我希望这是例行公事(希望如此)它计算抛出我的列。实际数据约为 20k 行:

数据格式:

State   Owner   Job1    Job2    Job3    Job4
TN      Joe     123     456             234
TN      Frank           456     789
FL      Joe     123     456
FL      Frank   123

需要的结果:
State Owner JobCount
TN  Joe     3
TN  Frank   2
FL  Joe     2
FL  Frank   1

并卷起给所有者
Owner   JobCount
Joe     5
Frank   3

最佳答案

这是结果 1 的 TSQL

SELECT
    State
    ,Owner
    ,Sum (
    (
        CASE
            WHEN Job1 IS NULL THEN 0
            ELSE 1
        END)+
        (CASE
            WHEN  Job2 IS NULL THEN 0
            ELSE 1
        END) +
        (CASE
            WHEN Job3 IS NULL THEN 0
            ELSE 1
        END)+
        (CASE
            WHEN Job4 IS NULL THEN 0
            ELSE 1
        END))
FROM table
GROUP BY State, OWNER

关于sql - TSQL 统计字段并汇总,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44592069/

10-13 00:52