按状态获取每个位置的计数

时间:2019-07-10 09:28:53

标签: sql sql-server join

我有两个表,分别为QueueTableLocation

QueueTable数据如下,

QueueID FkBranchID  AddedTime   Status
11001           10  2019-07-02  5
11002           10  2019-07-03  5
11003           10  2019-07-04  6
11004           10  2019-07-05  5
11005           20  2019-07-06  6
11006           20  2019-07-07  5
11007           20  2019-07-08  6
11008           20  2019-07-09  6
11009           20  2019-07-10  5
11010           30  2019-07-11  5
11011           30  2019-07-12  6
11012           30  2019-07-13  5
11013           30  2019-07-14  6
11014           30  2019-07-15  5
11015           40  2019-07-16  5
11016           40  2019-07-17  5
11017           40  2019-07-02  5
11018           40  2019-07-03  5
11019           40  2019-07-04  6
11020           50  2019-07-05  5
11021           50  2019-07-06  5
11022           50  2019-07-13  6

Location表数据如下

BranchID    BranchName
10          Delhi
20          Karnataka
30          Telangana
40          Gujarat
50          Tamil Nadu

现在我需要通过联接表来检索以下输出

enter image description here

我尝试了这个查询,我知道还有更多错误的事情,如何解决这个问题

select b.BranchId AS ID, b.BranchName,count(lo.QueueID) as Count1 
from Location b
left outer join
(
select br.BranchId,l.QueueID 
from QueueTable l
LEFT OUTER join Location br 
on l.FkBranchId = br.BranchId
where  l.IsActive = 1
GROUP BY l.QueueID) lo on lo.BranchId = b.BranchId

但是它抛出一个错误

  

选择列表中的'Branch.BranchId'列无效,因为它既不包含在聚合函数中也不在GROUP BY子句中。

4 个答案:

答案 0 :(得分:1)

试试这个-

SELECT L.BranchID,
L.BranchName,
COUNT(CASE WHEN Status = 5 THEN 1 ELSE NULL END) [Count1(5)], 
COUNT(CASE WHEN Status = 6 THEN 1 ELSE NULL END) [Count2(6)]
-- You can add more Column here based on Number of Status you have.
FROM Location L
LEFT JOIN QueueTable Q ON L.BranchID = Q.FKBranchID
WHERE Q.IsActive = 1
GROUP BY L.BranchID,L.BranchName

答案 1 :(得分:1)

加入表,按BranchId,BranchName分组,条件是使用CASE语句有条件地计数5和6的出现:

select 
  l.BranchId AS ID, 
  l.BranchName,
  count(case q.Status when 6 then 1 end) as Count1,
  count(case q.Status when 5 then 1 end) as Count2  
from Location l left join QueueTable q
on l.BranchId = q.FkBranchId
where l.IsActive = 1
group by l.BranchId, l.BranchName

答案 2 :(得分:1)

一种简单的方法是:

select
     a.branchid,
     a.branchname,
     (select count(*) from queuetable where fkbranchid=a.branchid and status=5) status_5_count,
     (select count(*) from queuetable where fkbranchid=a.branchid and status=6) status_6_count

from
     location a

您可以在此上实时执行此查询 SqlFiddle

答案 3 :(得分:0)

使用动态查询来专门计算status的动态情况

DECLARE @col VARCHAR(1000)
DECLARE @sql VARCHAR(2000)

SELECT @col = COALESCE(@col + ', ','') + QUOTENAME(CAST([status] AS VARCHAR))
FROM #QueueTable
GROUP BY [status]

SET @sql = '
select BranchID, BranchName, ' + @col + '
from (
    select l.BranchID, l.BranchName, FkBranchID, q.status
    from #Location l 
    join #QueueTable q on q.FkBranchID=l.BranchID)p
PIVOT(COUNT(FkBranchID) FOR status IN ( ' + @col + ' )
) AS pvt
ORDER BY BranchID'

PRINT @sql
EXEC (@sql)

输出

BranchID    BranchName  5   6
10          Delhi       3   1
20          Karnataka   2   3
30          Telangana   3   2
40          Gujarat     4   1
50          Tamil Nadu  2   1