0

I have a query like:

SELECT          a.EmployeeId,
                count(a.JobStatusId) cTotOut
FROM            ActivityRecord a
where           a.jobstatusid <> 5
GROUP BY        a.EmployeeId

SELECT          a.EmployeeId,
                count(a.JobStatusId) cTotHis
FROM            ActivityRecord a
where           a.jobstatusid = 5
GROUP BY        a.EmployeeId

The result on my PC is like:

enter image description here

I want the queries above become a single query and become something like:

---------------------------------------
EmployeeId       cTotOut       cTotHis
---------------------------------------
A                7             5
B                5             7
C                4             8

I have a question:
Is it possible with two different condition into a single query?

Please advice.
Thank you.

2 Answers 2

1

Use conditional aggregation:

SELECT
    EmployeeId,
    COUNT(CASE WHEN jobstatusid <> 5 THEN 1 END) AS cTotOut,
    COUNT(CASE WHEN jobstatusid = 5  THEN 1 END) AS cTotHis
FROM ActivityRecord
GROUP BY EmployeeId;
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, thanks a lot. This one is correct! Do you have any idea for Linq syntax?
@Haminteu I don't use LINQ very often, so off the top of my head, no, I don't know how to express conditional aggregation in a LINQ query.
just remove the linq question. I found it. Thanks a lot anyway. Cheers,
0
SELECT          a.EmployeeId,
            SUM(
             CASE
               WHEN a.JobStatusId<>5 THEN 1
               ELSE 0 END) cTotOut,
           SUM(
             CASE
               WHEN a.JobStatusId=5 THEN 1
               ELSE 0 END)cTotHis
  FROM            ActivityRecord a
  GROUP BY        a.EmployeeId

You can try this. Sorry, about LinQ I know nothing, if it is possible and how

Comments

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.