0

I'm trying to combine multiple select statements into 1 query but can't seem to get it right. I figure I would need to do a sub select. I'm sure it's something simple I'm overlooking. I have more after the 2nd query but they're all the same setup except different TAS_UID numbers.

Query 1:

SELECT USR_USERNAME AS 'User Name', 
           COUNT(AD.app_uid) AS 'Total'
FROM   APP_DELEGATION AD
JOIN   USERS U
WHERE  AD.USR_UID = U.USR_UID
AND    AD.DEL_THREAD_STATUS = 'Closed'
GROUP BY AD.USR_UID

Query 2:

SELECT USR_USERNAME AS 'User Name', 
       COUNT(AD.app_uid) AS 'Total for Task A'
FROM   APP_DELEGATION AD
JOIN   USERS U
WHERE  AD.USR_UID = U.USR_UID
AND    AD.DEL_THREAD_STATUS = 'Closed'
AND    AD.TAS_UID = '23423423455'
GROUP BY AD.USR_UID
3
  • what do you want your result to look like? Your 2nd query appears to be just a subset of your first. Commented Oct 28, 2013 at 17:38
  • @nycdan Result should display as follows: 1st column: user names, 2nd column: count of all taks, column 3: count of only specified task uid Commented Oct 28, 2013 at 17:55
  • okay, I added an answer that I think will help. My syntax might be slightly off, but it should get you there. Commented Oct 28, 2013 at 18:43

2 Answers 2

2

Your queries are not correct. They should probably be something like the following:

Query 1:

SELECT USR_USERNAME AS 'User Name', COUNT(AD.app_uid) AS 'Total'
FROM APP_DELEGATION AS AD
  JOIN USERS AS U 
  ON AD.USR_UID = U.USR_UID
WHERE AD.DEL_THREAD_STATUS = 'Closed'
GROUP BY AD.USR_UID

Query 2:

SELECT USR_USERNAME AS 'User Name', COUNT(AD.app_uid) AS 'Total for Task A'
FROM APP_DELEGATION AS AD
  JOIN USERS AS U 
  ON AD.USR_UID = U.USR_UID
WHERE AD.DEL_THREAD_STATUS = 'Closed'
  AND AD.TAS_UID = '23423423455'
GROUP BY AD.USR_UID
Sign up to request clarification or add additional context in comments.

1 Comment

Did my answer solve your problem or are you still in need of something further?
1

You could do that with a Case statement. Something like this:

SELECT USR_USERNAME AS 'User Name'
  ,COUNT(AD.app_uid) AS 'Total'
  ,SUM(case when AD.TAS_UID = '23423423455' then 1 else 0 end) as 'Total for Task A'
FROM APP_DELEGATION AS AD
  JOIN USERS AS U 
  ON AD.USR_UID = U.USR_UID
WHERE AD.DEL_THREAD_STATUS = 'Closed'
GROUP BY AD.USR_USERNAME

3 Comments

wouldn't you need to remove the WHERE AND statement as it was already stated in the sum function? Also, what exactly is the case then 1 else 0 doing?
You are correct. The AND should come out. I will edit accordingly. Thanks. The case statement adds up the number of rows that meet the criteria. It's basically a count.
ah that would make sense. Kinda got what else 0 was doing but was unsure of the 1. Thanks!

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.