如何连接从两个不同列计数的两个查询?

时间:2016-01-19 18:40:31

标签: sql

我有两个查询,我正在分组以获取该帐户。

是否可以使用dept_id列在一个查询中创建,两个查询中可能不存在某些ID。

输出如下:

dept_id | totalstars | totalstarsgiven

查询:

SELECT  
    employeedept as dept_id,
    COUNT(*) as 'totalstars'
FROM 
    Responses a 
WHERE
    execoffice_status = 1
    AND YEAR ([approveddate]) = 2015
    AND MONTH ([approveddate]) = 11 
    and employeedept not in (22,16) 
GROUP BY 
    execoffice_status, employeedept

SELECT 
    a.submitterdept as dept_id,
   COUNT(*) as 'totalstarsgiven'
FROM 
    Responses a 
WHERE 
    execoffice_status = 1
    AND YEAR ([approveddate]) = 2015
    AND MONTH ([approveddate]) = 11
GROUP BY 
    execoffice_status, submitterdept

4 个答案:

答案 0 :(得分:1)

因为您想要查看两个表返回的行,您需要执行Full Outer Join

 SELECT NVL(ed.dept_id, sd.dept_id), NVL(ed.totalstars, 0) totalstars,
 NVL(sd.totalstarsgiven, 0) totalstarsgiven 
 FROM
      (SELECT  employeedept as dept_id, COUNT(*) as totalstars
         FROM Responses a    
         WHERE execoffice_status = 1
         and YEAR ([approveddate]) =2015
         and month ([approveddate]) =11 
         and employeedept not in (22,16) 
         GROUP BY execoffice_status, employeedept) ed
 FULL OUTER JOIN 
      (SELECT  a.submitterdept as dept_id, COUNT(*) as totalstarsgiven
         FROM Responses a    
         WHERE execoffice_status = 1
         and YEAR ([approveddate]) =2015
         and month ([approveddate]) =11
         GROUP BY execoffice_status, submitterdept) sd 
 ON ed.deptId = sd.deptId

答案 1 :(得分:0)

这就是你想要的吗?

SELECT  employeedept as dept_id, COUNT(*) as 'totalstarsgiven'
        FROM Responses a    
        WHERE execoffice_status = 1
        and YEAR ([approveddate]) =2015
        and month ([approveddate]) =11 
        and employeedept not in (22,16) 
        GROUP BY execoffice_status, employeedept
UNION
SELECT  a.submitterdept as dept_id, COUNT(*) as 'totalstarsgiven'
        FROM Responses a    
        WHERE execoffice_status = 1
        and YEAR ([approveddate]) =2015
        and month ([approveddate]) =11
        GROUP BY execoffice_status, submitterdept

或者您可以尝试:

SELECT  CASE WHEN a.employeedept not in (22,16) THEN employeedept ELSE  a.submitterdept END as dept_id, COUNT(*) as 'totalstarsgiven'
        FROM Responses a    
        WHERE execoffice_status = 1
        and YEAR ([approveddate]) =2015
        and month ([approveddate]) =11
        GROUP BY execoffice_status, CASE WHEN a.employeedept not in (22,16) THEN employeedept ELSE  a.submitterdept END

答案 2 :(得分:0)

这可以使用条件聚合在一个查询中完成。

echo yes | taco feedback

答案 3 :(得分:0)

我认为这会做你想做的事情:

SELECT
    employeedept as dept_id
    , COUNT(*) as totalstars
    , totalstarsgiven
FROM
    Responses a
    LEFT JOIN (
        SELECT
            a.submitterdept as dept_id
            , COUNT(*) as totalstarsgiven
        FROM
            Responses a
        WHERE
            execoffice_status = 1
            and YEAR ([approveddate]) =2015
            and month ([approveddate]) =11
        GROUP BY 
            execoffice_status
            , submitterdept
    ) b
        ON a.employeedept = b.dept_id
WHERE
    execoffice_status = 1
    and YEAR ([approveddate]) =2015
    and month ([approveddate]) =11 
    and employeedept not in (22,16)
GROUP BY 
    execoffice_status
    , employeedept

这将根据收到的星数(dept_id = employeedept)和给定的星号(当dept_id = submitterdept时)汇总各部门