我有以下SQL,它给了我一个汇总表。它位于经典ASP页面中,连接到SQL Server数据库
SELECT D.DOC_TYPE AS 'Doc Type', D.DOC_REF AS 'Doc Ref',
SUM(CASE WHEN I.TEAM='OPS1' THEN 1 ELSE 0 END) AS OPS1,
SUM(CASE WHEN I.TEAM='OPS2' THEN 1 ELSE 0 END) AS OPS2,
SUM(CASE WHEN I.TEAM='OPS3' THEN 1 ELSE 0 END) AS OPS3,
SUM(CASE WHEN I.TEAM='OPS4' THEN 1 ELSE 0 END) AS OPS4,
SUM(CASE WHEN I.TEAM='OPS5' THEN 1 ELSE 0 END) AS OPS5,
SUM(CASE WHEN I.TEAM='OPS6' THEN 1 ELSE 0 END) AS OPS6,
SUM(CASE WHEN I.TEAM='OPS7' THEN 1 ELSE 0 END) AS OPS7,
SUM(CASE WHEN I.TEAM='OPS8' THEN 1 ELSE 0 END) AS OPS8,
SUM(CASE WHEN I.TEAM='OPS9' THEN 1 ELSE 0 END) AS OPS9,
SUM(CASE WHEN I.TEAM='OPS10' THEN 1 ELSE 0 END) AS OPS10,
SUM(CASE WHEN I.TEAM IN ('OPS1', 'OPS2', 'OPS3', 'OPS4', 'OPS5') THEN 1 ELSE 0 END) AS Div1,
SUM(CASE WHEN I.TEAM IN ('OPS6', 'OPS7', 'OPS8', 'OPS9', 'OPS10') THEN 1 ELSE 0 END) AS Div2,
SUM(CASE WHEN I.TEAM Not Like 'OPS%' THEN 1 ELSE 0 END) AS 'Other Teams',
COUNT(D.GUID) AS Total
FROM DOCUMENT AS D INNER JOIN INTRAY AS I ON I.DOC_GUID = D.GUID
WHERE D.DOC_TYPE like ?
AND I.FILE_SYSTEM='B' AND I.MAIL_STATUS IN ('N', 'NT')
GROUP BY D.DOC_TYPE, D.DOC_REF ;
这很好,但没有给出总排。所以我修改了小组,如下所示
GROUP BY D.DOC_TYPE, D.DOC_REF WITH ROLLUP ORDER BY 1 DESC
这给了我底部的总行,但第一列或第二列中没有任何内容表示它是一行。我还希望能够在ASP输出中以粗体文本显示总行。
整个ASP页面位于
之下<%@ Language="VBScript" %>
<%' Option Explicit %>
<!--#include virtual="bactu/IWStats/inc/images_conn.asp"-->
<HTML>
<HEAD>
<title>I@W Benefits - GIMs Summary</title>
</HEAD><span class="normtext">
<BODY>
<LINK href="../css/images_stats.css" type=text/css rel=stylesheet>
<!--#include virtual="bactu/IWStats/inc/header.asp"-->
<!--#include virtual="bactu/IWStats/inc/menu.asp"-->
<%
'set variables
Dim SQL, RS, cmd
Const adParamInput = 1
Const adDBDate = 133
const adVarWChar = 202
SQL = "SELECT D.DOC_TYPE AS 'Doc Type', D.DOC_REF AS 'Doc Ref', " & _
"SUM(CASE WHEN I.TEAM='OPS1' THEN 1 ELSE 0 END) AS OPS1, " & _
"SUM(CASE WHEN I.TEAM='OPS2' THEN 1 ELSE 0 END) AS OPS2, " & _
"SUM(CASE WHEN I.TEAM='OPS3' THEN 1 ELSE 0 END) AS OPS3, " & _
"SUM(CASE WHEN I.TEAM='OPS4' THEN 1 ELSE 0 END) AS OPS4, " & _
"SUM(CASE WHEN I.TEAM='OPS5' THEN 1 ELSE 0 END) AS OPS5, " & _
"SUM(CASE WHEN I.TEAM='OPS6' THEN 1 ELSE 0 END) AS OPS6, " & _
"SUM(CASE WHEN I.TEAM='OPS7' THEN 1 ELSE 0 END) AS OPS7, " & _
"SUM(CASE WHEN I.TEAM='OPS8' THEN 1 ELSE 0 END) AS OPS8, " & _
"SUM(CASE WHEN I.TEAM='OPS9' THEN 1 ELSE 0 END) AS OPS9, " & _
"SUM(CASE WHEN I.TEAM='OPS10' THEN 1 ELSE 0 END) AS OPS10, " & _
"SUM(CASE WHEN I.TEAM IN ('OPS1', 'OPS2', 'OPS3', 'OPS4', 'OPS5') THEN 1 ELSE 0 END) AS Div1, " & _
"SUM(CASE WHEN I.TEAM IN ('OPS6', 'OPS7', 'OPS8', 'OPS9', 'OPS10') THEN 1 ELSE 0 END) AS Div2, " & _
"SUM(CASE WHEN I.TEAM Not Like 'OPS%' THEN 1 ELSE 0 END) AS 'Other Teams', " & _
"COUNT(D.GUID) AS Total " & _
"FROM DOCUMENT AS D INNER JOIN INTRAY AS I ON I.DOC_GUID = D.GUID " & _
"WHERE D.DOC_TYPE like ? " & _
"AND I.FILE_SYSTEM='B' AND I.MAIL_STATUS IN ('N', 'NT')" & _
"GROUP BY D.DOC_TYPE, D.DOC_REF WITH ROLLUP ORDER BY 1 DESC ;"
'create recordset
Set RS = Server.CreateObject("ADODB.Recordset")
RS.cursortype=adOpenKeyset
'create command
Set cmd = CreateObject("ADODB.Command")
cmd.ActiveConnection = IConn
cmd.CommandText = SQL
'Append cmd parameters - CreateParameter(Name, Type, Direction, SizeIfString, Value)
cmd.Parameters.Append cmd.CreateParameter("QueryDocType", adVarWChar, adParamInput, 255, "GIM%" )
'Initiate recordset from command's execute method
Set RS = cmd.Execute
RS.MoveFirst%>
<br><br>New GIM* / RE_SB docs ;<br><br>
<table border="1">
<thead>
<tr>
<%For Each fld in rs.Fields%>
<th><span><%=Server.HTMLEncode(fld.Name)%></span></th>
<%Next %>
</tr>
</thead>
<tbody>
<%
Do Until rs.EOF%>
<tr>
<%For Each fld in rs.Fields%>
<td><%=RS(Server.HTMLEncode(fld.Name))%></th>
<%Next %>
</tr>
<%rs.MoveNext
Loop%>
</tbody>
</table>
<hr>
<%RS.Close
Set RS = Nothing
IConn.Close
Set IConn = Nothing%>
</BODY>
</HTML>
有人可以帮忙吗?我看过http://msdn.microsoft.com/en-gb/library/ms178544.aspx,但它似乎没有帮助。我不明白如何正确使用SELECt子句中的GROUPING函数来实现我的需要。
我试图复制http://mangalpardeshi.blogspot.co.uk/2009/08/rollup-and-order-by.html中给出的示例但没有成功。
------编辑16/07/14 12:57 -------
我根据Gordon Linoff的回答纠正了SQL。谁能告诉我如何修改表格绘制,以便最后一行是粗体文字?
Set RS = cmd.Execute
RS.MoveFirst%>
<br><br>New GIM* / RE_SB docs ;<br><br>
<table border="1">
<thead>
<tr>
<%For Each fld in rs.Fields%>
<th><span><%=Server.HTMLEncode(fld.Name)%></span></th>
<%Next %>
</tr>
</thead>
<tbody>
<%
Do Until rs.EOF%>
<tr>
<%For Each fld in rs.Fields%>
<td><%=RS(Server.HTMLEncode(fld.Name))%></th>
<%Next %>
</tr>
<%rs.MoveNext
Loop%>
</tbody>
</table>
答案 0 :(得分:2)
试试这个:
SELECT COALESCE(D.DOC_TYPE, 'Total') AS [Doc Type], D.DOC_REF AS [Doc Ref],
. . .
FROM DOCUMENT D INNER JOIN
INTRAY I
ON I.DOC_GUID = D.GUID
WHERE D.DOC_TYPE like ? AND I.FILE_SYSTEM='B' AND I.MAIL_STATUS IN ('N', 'NT')
GROUP BY D.DOC_TYPE, D.DOC_REF WITH ROLLUP
ORDER BY D.DOC_TYPE DESC;
这会将doc_type
替换为字符串'Total'
。注意:这假设DOC_TYPE
已经是一个字符串。如果没有,请将其转换为一个。然后更改order by
以引用表中的列。最终NULL
会显示DESC
个值。
另外,我建议您仅对字符串和日期常量使用单引号。将它们用于列别名是对未来问题的邀请。我用方括号替换了那些单引号。