【发布时间】:2014-07-12 02:24:33
【问题描述】:
我在 SQL Server 2005 中汇总了一个查询,按状态计算我们的体积:
SELECT ISNULL(q1.state,'UK') AS 'State', ISNULL(q1.TP,0) AS Transportation, ISNULL(q2.TL,0) AS Translation
FROM
(select p.state, count(Distinct t.patientID) as TP
from v_trip t
LEFT JOIN v_patient p ON t.patientid = p.ssn
where t.transtype not like 'translati%'
and created >= '5/18/2014'
and created < '5/25/2014'
group by p.state) AS q1
LEFT JOIN
(select p.state, count(Distinct t.patientID) as TL
from v_trip t
LEFT JOIN v_patient p ON t.patientid = p.ssn
where t.transtype like 'translati%'
and created >= '5/18/2014'
and created < '5/25/2014'
group by p.state) AS q2
ON q1.state = q2.state
ORDER BY q1.state
结果显示了每个州的交易量总和,我正在尝试计算总和并在我的结果底部输入。我已经尝试过ROLLUP,但它破坏了查询,可能是因为我不确定在哪里正确放置它。
【问题讨论】:
标签: sql