我试图想到这个最好的头衔。我有一个非常复杂的查询(使用传统的ASP和SQL服务器),我将使其更简单,例如用途。
这是针对report.asp
SQL = "select EMPL.employeeName, " & _
"COUNT(BUSINESS.ID) AS theTotal, " & _
"COUNT(CASE WHEN BUSINESS.ID IS NULL THEN 1 END) as unTouched " & _
"FROM EMPL Inner Join BUSINESS " & _
"WHERE EMPL.STATUS = 1 " & _
"GROUP BY EMPL.employeeName " & _
"ORDER BY " & theOrder
在HTML部分中,我有一个看起来像这样的表:
<table>
<tr>
<th><a href="report.asp?sort=1">Employee</th>
<th><a href="report.asp?sort=2">Total</th>
<th><a href="report.asp?sort=3">Untouched</th>
</tr>
<tr>
<td><%=RS("employeeName")%></td>
<td><%=RS("theTotal")%></td>
<td><%=RS("unTouched")%></td>
</tr>
</table>
最后,在页面顶部,我有一个order by子句的select case,如下所示:
theOrder = request.querystring("sort")
SELECT CASE theOrder
CASE 1
theOrder = "EMPL.employeeName"
CASE 2
theOrder = "theTotal, EMPL.employeeName"
CASE 3
theOrder = "unTouched, EMPL.employeeName"
CASE ELSE
theOrder = "EMPL.employeeName"
END SELECT
一切都按预期工作正常。现在,我想在结尾处添加一个列(theTotal - unTouched)。
所以我在表行之前的循环代码中添加它:
do while not RS.eof
TheDIfference = RS("theTotal") - RS("unTouched")
...
我添加了“差异”的表格标题和显示减法的新行,一切都很完美。
这是问题所在。我希望能够按该列排序,就像其他2列一样。所以我试着在我的select语句中添加一行说:
SUM(theTotal - unTouched) AS theDifference
那不行,所以我把它拼写出来:
SUM(COUNT(BUSINESS.ID)- COUNT(CASE WHEN BUSINESS.ID IS NULL THEN 1 END)) AS theDifference
那也没有用。
那么有没有一种正确的方法将它包含在select语句中,以便我可以执行orderby或解决方法,而不必将其放在语句本身中?
感谢任何帮助,谢谢!
编辑:整个真实查询:
SQL = "SELECT E.[theTotal], E.[unTouched], (E.[theTotal] - E.[unTouched]) [theDifference] " & _
"FROM ( " & _
"SELECT EMPL.EMPLDesc, EMPL.EMPLID, EMPL.EMPLSalesmanID, COUNT(BLXF.BLXFBSNSID) AS theTotal, " & _
"COUNT(CASE WHEN RIGHT(PROJ.ProjCode,1) = 'p' THEN 1 END) AS totalChamber, " & _
"COUNT(CASE WHEN RIGHT(PROJ.ProjCode,1) = 'c' THEN 1 END) AS totalCity, " & _
"COUNT(CASE WHEN LIST.LISTDESC LIKE '%-MUL%' THEN 1 END) AS MulCnt, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-MUL%') THEN 1 END) AS MulCntChamber, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-MUL%') THEN 1 END) AS MulCntCity, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-MUL%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) AS MulCntChamberUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-MUL%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) AS MulCntCityUN, " & _
"COUNT(CASE WHEN LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%' THEN 1 END) AS NonMulCnt, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%') THEN 1 END) AS NonMulCntChamber, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%') THEN 1 END) AS NonMulCntCity, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) AS NonMulCntChamberUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) AS NonMulCntCityUN, " & _
"COUNT(CASE WHEN BLXF.BLXFLastUpdate IS NULL THEN 1 END) as unTouched, " & _
"COUNT(CASE WHEN (BLXF.BLXFLastUpdate IS NULL AND RIGHT(PROJ.ProjCode,1) = 'p') THEN 1 END) as unTouchedChamber, " & _
"COUNT(CASE WHEN (BLXF.BLXFLastUpdate IS NULL AND RIGHT(PROJ.ProjCode,1) = 'c') THEN 1 END) as unTouchedCity, " & _
"COUNT(CASE WHEN (BLXF.BLXFLastUpdate IS NULL AND LIST.LISTDESC LIKE '%-MUL%') THEN 1 END) as unTouchedMul, " & _
"COUNT(CASE WHEN (BLXF.BLXFLastUpdate IS NULL AND LIST.LISTDESC NOT LIKE '%-MUL%') THEN 1 END) as unTouchedNonMul, " & _
"COUNT(CASE WHEN LIST.LISTDESC LIKE '%-RR-%' THEN 1 END) as reRunCnt, " & _
"COUNT(CASE WHEN (LIST.LISTDESC LIKE '%-RR-%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) as reRunCntUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-RR-%') THEN 1 END) AS reRunCntChamber, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-RR-%') THEN 1 END) AS reRunCntCity, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-RR-%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) AS reRunCntChamberUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-RR-%' AND BLXF.BLXFLastUpdate IS NULL) THEN 1 END) AS reRunCntCityUN " & _
"FROM BLXF INNER JOIN LIST ON BLXF.BLXFLISTID = LIST.LISTID " & _
"INNER JOIN BSST ON BLXF.BLXFBSSTID = BSST.BSSTID " & _
"INNER JOIN EMPL ON LIST.LISTEMPLID = EMPL.EMPLID " & _
"INNER JOIN PROJ ON LIST.LISTPROJID = PROJ.PROJID " & _
"WHERE (LIST.LISTAvailable = 1) AND (BSST.BSSTTerminalInd = 0) AND (PROJ.PROJPJSTID = 2) AND (EMPL.EMPLStatus = 'y') " & _
"GROUP BY EMPL.EMPLDesc, EMPL.EMPLID, EMPL.EMPLSalesmanID " & _
") E " & _
"ORDER BY " & theOrderBy
UPDATED QUERY 2:
SQL = "SELECT E.[theTotal], E.[unTouched], (E.[theTotal] - E.[unTouched]) [theDifference] " & _
"FROM ( " & _
"SELECT E.EMPLDesc, E.EMPLID, E.EMPLSalesmanID, COUNT(B.BLXFBSNSID) AS theTotal, " & _
"COUNT(CASE WHEN RIGHT(PROJ.ProjCode,1) = 'p' THEN 1 END) AS totalChamber, " & _
"COUNT(CASE WHEN RIGHT(PROJ.ProjCode,1) = 'c' THEN 1 END) AS totalCity, " & _
"COUNT(CASE WHEN LIST.LISTDESC LIKE '%-MUL%' THEN 1 END) AS MulCnt, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-MUL%') THEN 1 END) AS MulCntChamber, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-MUL%') THEN 1 END) AS MulCntCity, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-MUL%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) AS MulCntChamberUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-MUL%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) AS MulCntCityUN, " & _
"COUNT(CASE WHEN LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%' THEN 1 END) AS NonMulCnt, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%') THEN 1 END) AS NonMulCntChamber, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%') THEN 1 END) AS NonMulCntCity, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) AS NonMulCntChamberUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC NOT LIKE '%-MUL%' AND LIST.LISTDESC NOT LIKE '%-RR-%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) AS NonMulCntCityUN, " & _
"COUNT(CASE WHEN B.BLXFLastUpdate IS NULL THEN 1 END) as unTouched, " & _
"COUNT(CASE WHEN (B.BLXFLastUpdate IS NULL AND RIGHT(PROJ.ProjCode,1) = 'p') THEN 1 END) as unTouchedChamber, " & _
"COUNT(CASE WHEN (B.BLXFLastUpdate IS NULL AND RIGHT(PROJ.ProjCode,1) = 'c') THEN 1 END) as unTouchedCity, " & _
"COUNT(CASE WHEN (B.BLXFLastUpdate IS NULL AND LIST.LISTDESC LIKE '%-MUL%') THEN 1 END) as unTouchedMul, " & _
"COUNT(CASE WHEN (B.BLXFLastUpdate IS NULL AND LIST.LISTDESC NOT LIKE '%-MUL%') THEN 1 END) as unTouchedNonMul, " & _
"COUNT(CASE WHEN LIST.LISTDESC LIKE '%-RR-%' THEN 1 END) as reRunCnt, " & _
"COUNT(CASE WHEN (LIST.LISTDESC LIKE '%-RR-%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) as reRunCntUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-RR-%') THEN 1 END) AS reRunCntChamber, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-RR-%') THEN 1 END) AS reRunCntCity, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'p' AND LIST.LISTDESC LIKE '%-RR-%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) AS reRunCntChamberUN, " & _
"COUNT(CASE WHEN (RIGHT(PROJ.ProjCode,1) = 'c' AND LIST.LISTDESC LIKE '%-RR-%' AND B.BLXFLastUpdate IS NULL) THEN 1 END) AS reRunCntCityUN " & _
"FROM BLXF B INNER JOIN LIST ON B.BLXFLISTID = LIST.LISTID " & _
"INNER JOIN BSST ON B.BLXFBSSTID = BSST.BSSTID " & _
"INNER JOIN EMPL E ON LIST.LISTEMPLID = E.EMPLID " & _
"INNER JOIN PROJ ON LIST.LISTPROJID = PROJ.PROJID " & _
"WHERE (LIST.LISTAvailable = 1) AND (BSST.BSSTTerminalInd = 0) AND (PROJ.PROJPJSTID = 2) AND (E.EMPLStatus = 'y') " & _
"GROUP BY E.EMPLDesc, E.EMPLID, E.EMPLSalesmanID " & _
") E " & _
"ORDER BY " & theOrderBy
答案 0 :(得分:0)
因为这两个字段是计算列,然后才能通过别名访问它们,所以需要将它们包装在子查询中。
SELECT E.[employeeName], E.[theTotal], E.[unTouched]
, (E.[theTotal] - E.[unTouched]) [theDifference]
FROM (
SELECT E.employeeName,
COUNT(B.ID) [theTotal],
COUNT(CASE WHEN B.ID IS NULL THEN 1 END) [unTouched]
FROM EMPL E
INNER JOIN BUSINESS B ON E.SomeField = B.SomeField
WHERE E.STATUS = 1
GROUP BY E.employeeName
) E
ORDER BY E.[employeeName];
INNER JOIN
需要加入一些形成EMPL
表和BUSINESS
表之间关系的字段。