两个sql查询混合为一个

时间:2014-04-24 04:01:45

标签: sql qsqlquery

我想将两段代码合并为一块,但是我收到了一个错误:

第1部分

SELECT idstd,namestd, idmajor, 
      c1, c2, c3, c4, c5, c6, 
      c7, c8, c9, c10,c11,c12,
      c13,c14,c15 
FROM 
    (SELECT status, idstd,namestd, idmajor, 
           'C' + cast(row_number() 
            OVER (partition BY idstd, idmajor 
               ORDER BY (SELECT 1)) AS varchar(10)) col
     FROM tbcheked) src 
PIVOT (MAX(status) FOR col IN (C1, C2, C3, C4, C5, 
                               C6, C7, C8, C9, C10,
                               c11,c12,c13,c14,c15)) piv

此代码用于检查列status的值是presentabsent还是leave

示例:

id    |   c1      |   c2     |     c3  |     c4  |     c5  |
1        present     absent    present   leave     present

我有第二段状态计数代码:

    SELECT idstd, 
           namemajor, 
           SUM(CASE WHEN status = 'present' 
                    THEN 1 
                    ELSE 0 
               END) AS present, 
           SUM(CASE WHEN status = 'absent' 
                    THEN 1 
                    ELSE 0 
               END) AS absent, 
           SUM(CASE WHEN status = 'leave' 
                    THEN 1 
                    ELSE 0 
                END) AS leave, 
   FROM tbcheked GROUP BY idstd, namemajor 
   ORDER BY idstd

输出:

id     | present  |  absent  |  leave  |
1          3           1          1

现在,我想加入两个查询:

id    |   c1      |   c2     |     c3  |     c4  |     c5  |  present  |  absent  |  leave  |
1        present     absent    present   leave     present     3            1         1

2 个答案:

答案 0 :(得分:1)

使用join组合两个查询的输出。

select a.*, b.*
from (query 1)a join (query 2)b on a.id=b.id; 

答案 1 :(得分:1)

将每个部分视为子查询并加入它们:

select
    <whatever>
from
        (
            SELECT idstd,namestd, idmajor, 
                  c1, c2, c3, c4, c5, c6, 
                  c7, c8, c9, c10,c11,c12,
                  c13,c14,c15 
            FROM 
                (SELECT status, idstd,namestd, idmajor, 
                       'C' + cast(row_number() 
                        OVER (partition BY idstd, idmajor 
                           ORDER BY (SELECT 1)) AS varchar(10)) col
                 FROM tbcheked) src 
            PIVOT (MAX(status) FOR col IN (C1, C2, C3, C4, C5, 
                                           C6, C7, C8, C9, C10,
                                           c11,c12,c13,c14,c15)) piv
        ) as PivotedBit
INNER JOIN -- or OUTER, depending on your requirement
        (
            SELECT idstd, 
                   namemajor, 
                   SUM(CASE WHEN status = 'present' 
                            THEN 1 
                            ELSE 0 
                       END) AS present, 
                   SUM(CASE WHEN status = 'absent' 
                            THEN 1 
                            ELSE 0 
                       END) AS absent, 
                   SUM(CASE WHEN status = 'leave' 
                            THEN 1 
                            ELSE 0 
                        END) AS leave, 
            FROM tbcheked GROUP BY idstd, namemajor 
            --ORDER BY idstd -- cannot have this in a sub-query
        ) as SummingBit
    on SummingBit.idstd = PivotedBit.idstd
    -- and any other common keys
    ORDER BY idstd

它会对tbcheked执行两次完整扫描,因此速度不会很快。

我有一种预感,即使用求和查询中的PIVOT语句向CASE查询的内部选择添加虚拟列,然后将SUM()添加到PIVOT条款会更好。然而,我并没有完全想到这一点。