這是我的查詢:
select fldUserId, count(*) AS TOTAL
from tblWorkHistory
where fldStatus = '1'
group by fldUserId
union
select fldEmpID, count(*) AS TOTAL
from tblQAHistory
where fldStatus = '1'
group by fldEmpID
輸出:
fldUserId TOTAL
16070004 34
19100015 1
19100015 7
191014571 3
我想將兩行與“19100015”合并為一行。
uj5u.com熱心網友回復:
將您的查詢用作子查詢:
with cte as (
select fldUserId, count(*) as TOTAL
from tblWorkHistory
where fldStatus = '1'
group by fldUserId
union
select fldEmpID, count(*)
from tblQAHistory
where fldStatus = '1'
group by fldEmpID
)
select fldUserId, sum(TOTAL) as TOTAL
from cte
group by fldUserId
uj5u.com熱心網友回復:
另一種方法是在外面只分組一次
select
t.fldUserId,
count(*) as TOTAL
from (
select fldUserId
from tblWorkHistory
where fldStatus = '1'
union all
select fldEmpID
from tblQAHistory
where fldStatus = '1'
) t
group by
t.fldUserId;
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/430433.html
