0

我有一列包含 0-9 的单个数字。我需要知道每个数字出现多少次。即0-4次,1-5次等`。

我有 6 列要计算,所以如果可能的话,我不想执行 9*6 查询。

编辑:

col1    col2    col3
 1        2      1 
 2        1      3 
 6        3      8

我需要知道 1 在第 1 列中出现了多少次,在第 2 列中出现了多少次,在第 3 列中出现了多少次。这适用于所有数字。

谢谢,丹尼

4

1 回答 1

2

假设你有这组记录,

╔══════╦══════╦══════╦══════╦══════╦══════╗
║ COL1 ║ COL2 ║ COL3 ║ COL4 ║ COL5 ║ COL6 ║
╠══════╬══════╬══════╬══════╬══════╬══════╣
║    1 ║    6 ║    3 ║    1 ║    2 ║    2 ║
║    2 ║    7 ║    3 ║    8 ║    3 ║    9 ║
║    3 ║    8 ║    3 ║    8 ║    8 ║    9 ║
║    4 ║    9 ║    2 ║    7 ║    1 ║    1 ║
║    5 ║    0 ║    1 ║    3 ║    1 ║    6 ║
╚══════╩══════╩══════╩══════╩══════╩══════╝

询问

SELECT  a.val, 
        MAX(CASE WHEN colName = 'col1' THEN TotalCount ELSE 0 END) col1,
        MAX(CASE WHEN colName = 'col2' THEN TotalCount ELSE 0 END) col2,
        MAX(CASE WHEN colName = 'col3' THEN TotalCount ELSE 0 END) col3,
        MAX(CASE WHEN colName = 'col4' THEN TotalCount ELSE 0 END) col4,
        MAX(CASE WHEN colName = 'col5' THEN TotalCount ELSE 0 END) col5,
        MAX(CASE WHEN colName = 'col6' THEN TotalCount ELSE 0 END) col6
FROM
        (
           SELECT 0 val UNION ALL
           SELECT 1 UNION ALL
           SELECT 2 UNION ALL
           SELECT 3 UNION ALL
           SELECT 4 UNION ALL
           SELECT 5 UNION ALL
           SELECT 6 UNION ALL
           SELECT 7 UNION ALL
           SELECT 8 UNION ALL
           SELECT 9
        ) a
        LEFT JOIN
        (
            SELECT col1 val, COUNT(*) TotalCount, 'col1' colname 
            FROM tableName 
            GROUP BY col1
            UNION ALL
            SELECT col2 val, COUNT(*), 'col2' colname 
            FROM tableName 
            GROUP BY col2
            UNION ALL
            SELECT col3 val, COUNT(*), 'col3' colname 
            FROM tableName 
            GROUP BY col3
            UNION ALL
            SELECT col4 val, COUNT(*), 'col4' colname 
            FROM tableName 
            GROUP BY col4
            UNION ALL
            SELECT col5 val, COUNT(*), 'col5' colname 
            FROM tableName 
            GROUP BY col5
            UNION ALL
            SELECT col6 val, COUNT(*), 'col6' colname 
            FROM tableName 
            GROUP BY col6
        ) b ON a.val = b.val
GROUP By a.val

输出

╔═════╦══════╦══════╦══════╦══════╦══════╦══════╗
║ VAL ║ COL1 ║ COL2 ║ COL3 ║ COL4 ║ COL5 ║ COL6 ║
╠═════╬══════╬══════╬══════╬══════╬══════╬══════╣
║   0 ║    0 ║    1 ║    0 ║    0 ║    0 ║    0 ║
║   1 ║    1 ║    0 ║    1 ║    1 ║    2 ║    1 ║
║   2 ║    1 ║    0 ║    1 ║    0 ║    1 ║    1 ║
║   3 ║    1 ║    0 ║    3 ║    1 ║    1 ║    0 ║
║   4 ║    1 ║    0 ║    0 ║    0 ║    0 ║    0 ║
║   5 ║    1 ║    0 ║    0 ║    0 ║    0 ║    0 ║
║   6 ║    0 ║    1 ║    0 ║    0 ║    0 ║    1 ║
║   7 ║    0 ║    1 ║    0 ║    1 ║    0 ║    0 ║
║   8 ║    0 ║    1 ║    0 ║    2 ║    1 ║    0 ║
║   9 ║    0 ║    1 ║    0 ║    0 ║    0 ║    2 ║
╚═════╩══════╩══════╩══════╩══════╩══════╩══════╝
于 2013-09-22T21:20:32.333 回答