0

我正在尝试将多个选择语句组合到 1 个查询中,但似乎无法正确处理。我想我需要做一个子选择。我确定这是我忽略的简单事情。在第二次查询之后我有更多,但它们都是相同的设置,除了不同的 TAS_UID 号码。

查询一:

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

查询 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
4

2 回答 2

2

您的查询不正确。它们可能应该类似于以下内容:

查询一:

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

查询 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
于 2013-10-28T17:36:46.193 回答
1

您可以使用 Case 语句来做到这一点。像这样的东西:

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
于 2013-10-28T18:00:57.017 回答