我在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
但它打破了查询,可能是因为我不确定在哪里正确放置它。
答案 0 :(得分:0)
由于我无法弄清楚如何在查询中将最终输出合计为CSV,我继续研究可能的VB脚本。 通过在我的VB脚本中添加以下行,我能够累计报告的列:
'Write header lines
line = "Report For Week of " & (dateadd("d",-7,sDate)) & ""
objTS.WriteLine line
line = "State,Data1,Data2"
objTS.WriteLine line
'Write detail lines
Do while not rs.eof
line = rs(0) & dl & rs(1) & dl & rs(2)
objTS.WriteLine line
if rs(1) >= 0 then
gtpTot = gtpTot + rs(1)
end if
if rs(2) >= 0 then
gtlTot = gtlTot + rs(2)
end if
rs.movenext
loop
'Output grand totals
line = "Grand Total," & gtpTot & dl & gtlTot
objTS.WriteLine line
此代码在文件末尾添加一个新行,显示我的数据的完整总数。