0

I have two queries that I'm grouping to get the account.

Is it possible make it in one query using the dept_id column, some of the ID's may no exist in both queries.

The output like this:

dept_id | totalstars | totalstarsgiven

Query:

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 Answers 4

1

Because you want to see the lines returned by both tables, you need to do a 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
Sign up to request clarification or add additional context in comments.

Comments

0

I think this will do what you want:

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

This will aggregate the departments by the number of stars they received (when dept_id = employeedept) and given (when dept_id = submitterdept)

Comments

0

Is this what you are after?

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

or you can try :

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

1 Comment

@Rahul I assumed we only want Distinct retruned. But you are correct if we want complete set from both statements then UNION ALL
0

This can be done in one query using conditional aggregation.

SELECT 
    employeedept as dept_id,
    sum(case when employeedept not in (22,16) then 1 else 0 end) as totalstars,
    count(submitterdept) as totalstarsgiven
FROM 
    Responses
WHERE 
    execoffice_status = 1 and YEAR([approveddate]) = 2015 and month([approveddate]) = 11  
GROUP BY 
    employeedept

3 Comments

@vjp it seems this ignores the submitterdept column count
just noticed and modified the query. Note that if there are nulls in the submitterdept they would be ignored when using count.
theres no nulls in any of the two columns, but it only seems to get the correct numbers for totalstars, totalstargiven are wrong

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.