table总列结果缺少某些值

时间:2015-02-16 05:26:35

标签: asp.net sql-server vbscript

在asp.net页面上运行sql server 2005查询并尝试总计值表底部的值时,我注意到总值忽略了我用粗体记下的值。对应于周末期间发生的小结果#。我无法弄清楚为什么遗漏这些价值观。我认为它是转换单元(int,dbl等)中的舍入问题,并尝试在不同的转换之间进行更改但没有成功

(我是初学者并继承了预先存在的代码,所以请假设我对任何回复都知之甚少)。由于我无法附加输出的屏幕截图,我只复制了一列数据作为示例。所有帮助表示感谢,谢谢!

发货日期:托运人总数:

2015年2月2日249

2/3/2015 254

2/4/2015 255

2/5/2015 263

2/6/2015 209

2/7/2015 6

2/9/2015 322

2/10/2015 304

2/11/2015 270

2/12/2015 212

2/13/2015 205

2015年2月14日 2

总计:2543



<%@ Language="VBScript" %>
<%	Option Explicit %>
<%
	Response.Expires = 0

	dim D1
		D1 = Request.Form("D1")
	dim D2
		D2 = Request.Form("D2")


	dim DispResults
		DispResults	=	Request.Form("A")

	dim TotalShippers, TotalShipperLines, TotalQuantityShipped, TotalKitsShipped, IntlTotalShippers, IntlTotalShipperLines, IntlTotalQuantityShipped, IntlTotalKitsShipped, GrandTotalShippers, GrandTotalShipperLines, GrandTotalQuantityShipped, GrandTotalKitsShipped
	IF D1 = "" THEN
		D1 = Month(Date()) & "/" & "1" & "/" & Year(Date())
	ELSE
	END IF

	IF D2 = "" THEN
		D2 = Date
	ELSE
	END IF

	
%>
<html>

<head>
<title>Daily Shipping Log</title>
<style>
body  {
	font-family: Tahoma, Verdana, Arial;
	font-size: 16px;
}
select	{
	font-family: Tahoma, Verdana, Arial;
	font-size: 16px;
}
</style>
</head>

<body onLoad="document.Search.D1.focus()">
<center>
<img src="logo.jpg" ALT="Daily Shipping Log">

<br>

<form action="default.asp" method="POST" name="Search">
<input type=hidden name="A" value="1">

<table>
	<tr>
		<td align="center" valign="top">
			<table>

				<tr>
					<td align="center" valign="bottom">
						Enter Starting Date:&nbsp;
					</td>
					<td align="center" valign="bottom">
						<input type="text" name="D1" size="24" value="">
					</td>
				</tr>
				<tr>
					<td align="center" valign="bottom">
						Enter Ending Date:&nbsp;
					</td>
					<td align="center" valign="bottom">
						<input type="text" name="D2" size="24" value="">
					</td>
				</tr>
				<tr>
					<td colspan=2 align="center" valign="bottom">
						<input type="submit" value="Generate Report">
					</td>
				</tr>
			</table>
		</td>
		<td align="center" valign="top">
			<table>
				<tr>
					<td align="center" valign="top" style="background-color:#FFFF99; font-size:10px;">
						<b>Note:</b><br><br>
						Date formats must be in the following manner:<br><br>
						<b>MM/DD/YY</b> if using a 2-digit Month and Day.<br>
						<b>M/D/YY</b> is using a 1-digit Month or Day.<br><br>
						Other date formats will not return any records.
					</td>
				</tr>
			</table>
		</td>
	</tr>
</table>

</form>

<br>
<%

IF D1 = "" THEN
ELSEIF DispResults = 1 THEN
	Response.Write "Data Reported is for Date Range:&nbsp;<b>" & D1 & "</b> to <b>" & D2 & "</b><br>"
	Response.Write "<hr color=""#65035A"" size=2 width=""100%"">"
	Response.Write "<table cellpadding=5 cellspacing=0 border=1 bordercolor=#666666>"
	Response.Write "<tr align=center>"	
	Response.Write "<td><b>Domestic:</b>"
	Response.Write "</td><td><b>International:</td></tr>"
	Response.Write "<tr align=center valign=top>"
	Response.Write "<tr align=center>"
	Response.Write "<td>"
	Response.Write "<table cellpadding=5>"
	Response.Write "<tr align=center valign=top>"	
	Response.Write "<td>"
	Response.Write "<b>Shipped Date:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Total Shippers:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Total Line Items Shipped:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Total Quantity Items Shipped:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Configured Kits Shipped:</b>"
	Response.Write "</td></tr>"


	dim oConn, oRS, oRS2
	Set oConn = Server.CreateObject("ADODB.Connection")
	oConn.Open """
	Set oRS = Server.CreateObject("ADODB.Recordset")
	Set oRS2 = Server.CreateObject("ADODB.Recordset")

	oRS.open "Select s.ShipDate, Count(distinct CASE when s.country='USA' then l.ShipmentNo end) as TotalShippers, " & _
		 "count(case when s.country='USA' then convert(int,l.ShipmentLineNo) END) as TotalLinesShipped, " & _
		 "sum(case when s.country='USA' then (l.TotalShippedQuantity) else 0 END) as TotalItemsShipped, " & _
		 "Count(distinct CASE when s.country<>'USA' then l.ShipmentNo end) as IntlTotalShippers, " & _
		 "count(case when s.country<>'USA' then convert(int,l.ShipmentLineNo) END) as IntlTotalLinesShipped, " & _
		 "sum(case when s.country<>'USA' then (l.TotalShippedQuantity) END) as IntlTotalItemsShipped, " & _
		 "Count(distinct l.ShipmentNo) as GrandTotalShippers, " & _
		 "convert(int, count(l.ShipmentLineNo)) as GrandTotalLinesShipped, " & _
		 "sum(l.TotalShippedQuantity) as GrandTotalItemsShipped " & _
		 "From shipments s, shipments_li l " & _
		 "Where (s.ShipDate between '" & D1 & "' AND '" & D2 & "') " & _
		 "and  s.ShipmentNo = l.ShipmentNo " & _
		 "and l.TotalShippedQuantity>0 and s.status<>'X' AND " & _
		 "s.shipfrominventory not in ('BI','CI','CS','IT')" & _
		 " Group by s.ShipDate order by s.ShipDate", oConn
	
	
	DO WHILE NOT oRS.EOF
	Response.Write "<tr align=center><td>" & oRS("ShipDate") & "</td>"
	Response.Write "<td>" & oRS("TotalShippers").value & "</td>"
	Response.Write "<td>" & oRS("TotalLinesShipped").value & "</td>"
	Response.Write "<td>" & oRS("TotalItemsShipped").value & "</td>"
	
	oRS2.open "SELECT s.ShipDate, count(case when s.country='USA' THEN (l.ShipmentLineNo) end) AS CountOfKits, " & _
		  "count(case when s.country<>'USA' THEN (l.ShipmentLineNo) end) AS IntlCountOfKits, " & _	
	          "count(l.ShipmentLineNo) AS GrandTotalCountOfKits " & _
		  "FROM (shipments s INNER JOIN shipments_li l ON s.ShipmentNo = l.ShipmentNo) " & _
		  "WHERE (l.TotalShippedQuantity>0) AND (s.Status<>'x') " & _
		  "AND s.ShipDate='" & oRS("ShipDate").Value & "' " & _
		  "AND (Left(l.partNo,5)<>'hires') AND (l.PartDescription<>'CHARGER KIT, AURIA') " & _
		  "AND (Left(l.partNo,8) NOT IN ('303-M220' , '303-M218','303-M219')) " & _
		  "AND (Left(l.partNo,4)<>'zeni') AND (Left(l.partNo,2)<>'sw') and (Left(l.partNo,7)<>'sr3upus')" & _	
		  "and l.Configuration is not null AND  s.shipfrominventory not in ('BI','CI','CS','IT')" & _
		  "GROUP BY s.ShipDate " & _
		  "ORDER By s.ShipDate", oConn

IF oRS2.EOF THEN Response.Write "<td>" & 0 & "</td></tr>" ELSE	
DO WHILE NOT oRS2.EOF
	Response.Write "<td>" & oRS2("CountOfKits").Value & "</td></tr>"
	TotalKitsShipped = CInt(oRS2("CountofKits").Value) + TotalKitsShipped
		
	TotalShippers = CInt(oRS("TotalShippers").Value) + TotalShippers
	TotalShipperLines = CInt(oRS("TotalLinesShipped").Value) + TotalShipperLines
	TotalQuantityShipped = CInt(oRS("TotalItemsShipped").Value) + TotalQuantityShipped
		

	oRS2.MoveNext
	Loop
	oRS2.Close

	oRS.MoveNext
	Loop
	oRS.Close

	Set oRS2 = Nothing			
	Set oRS = Nothing
	oConn.Close
	Set oConn = Nothing 

	Response.Write "<tr align=center><td><b>Totals:</b></td>"
	Response.Write "<td><b>" & TotalShippers & "</b></td>"
	Response.Write "<td><b>" & TotalShipperLines & "</b></td>"
	Response.Write "<td><b>" & TotalQuantityShipped & "</b></td>"
	Response.Write "<td><b>" & TotalKitsShipped & "</b></td></tr>"
	Response.Write "</td>"
	Response.Write "</tr>"
	Response.Write "</table>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<table cellpadding=5>"
	Response.Write "<tr align=center>"	
	Response.Write "<td>"
	Response.Write "<b>Total Shippers:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Total Line Items Shipped:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Total Quantity Items Shipped:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Configured Kits Shipped:</b>"
	Response.Write "</td></tr>"
Set oConn = Server.CreateObject("ADODB.Connection")
	oConn.Open "Driver={SQL Server}; Server=ABSQL21; Database=DF_WHSE; Uid=ReadOnlyUser; Pwd=bionics;"
	Set oRS = Server.CreateObject("ADODB.Recordset")
	Set oRS2 = Server.CreateObject("ADODB.Recordset")

	oRS.open "Select s.ShipDate, Count(distinct CASE when s.country='USA' then l.ShipmentNo end) as TotalShippers, " & _
		 "count(case when s.country='USA' then convert(int,l.ShipmentLineNo) END) as TotalLinesShipped, " & _
		 "sum(case when s.country='USA' then (l.TotalShippedQuantity) END) as TotalItemsShipped, " & _
		 "Count(distinct CASE when s.country<>'USA' then l.ShipmentNo end) as IntlTotalShippers, " & _
		 "count(case when s.country<>'USA' then convert(int,l.ShipmentLineNo) END) as IntlTotalLinesShipped, " & _
		 "sum(case when s.country<>'USA' then (l.TotalShippedQuantity) else 0 END) as IntlTotalItemsShipped " & _
		 "From shipments s, shipments_li l " & _
		 "Where (s.ShipDate between '" & D1 & "' AND '" & D2 & "') " & _
		 "and  s.ShipmentNo = l.ShipmentNo " & _
		 "and l.TotalShippedQuantity>0 and s.status<>'X' AND " & _
		 "s.shipfrominventory not in ('BI','CI','CS','IT')" & _
		 "Group by s.ShipDate order by s.ShipDate", oConn
	
	
	DO WHILE NOT oRS.EOF
	Response.Write "<tr align=center>"
	Response.Write "<td>" & oRS("IntlTotalShippers").value & "</td>"
	Response.Write "<td>" & oRS("IntlTotalLinesShipped").value & "</td>"
	Response.Write "<td>" & oRS("IntlTotalItemsShipped").value & "</td>"

	oRS2.open "SELECT s.ShipDate, count(case when s.country='USA' THEN (l.ShipmentLineNo) end) AS CountOfKits, " & _
		  "count(case when s.country<>'USA' THEN (l.ShipmentLineNo) end) AS IntlCountOfKits " & _
		  "FROM (shipments s INNER JOIN shipments_li l ON s.ShipmentNo = l.ShipmentNo) " & _
		  "WHERE (l.TotalShippedQuantity>0) AND (s.Status<>'x') " & _
		  "AND s.ShipDate='" & oRS("ShipDate").Value & "' " & _
		  "AND (Left(l.partNo,5)<>'hires') AND (l.PartDescription<>'CHARGER KIT, AURIA') " & _
		  "AND (Left(l.partNo,8) NOT IN ('303-M220' , '303-M218','303-M219')) " & _
		  "AND (Left(l.partNo,4)<>'zeni') AND (Left(l.partNo,2)<>'sw') and (Left(l.partNo,7)<>'sr3upus')" & _	
		  "and l.Configuration is not null AND  s.shipfrominventory not in ('BI','CI','CS','IT')" & _
		  "GROUP BY s.ShipDate " & _
		  "ORDER By s.ShipDate", oConn

IF oRS2.EOF THEN Response.Write "<td>" & 0 & "</td></tr>" ELSE	
DO WHILE NOT oRS2.EOF


	Response.Write "<td>" & oRS2("IntlCountOfKits").Value & "</td></tr>"
	IntlTotalKitsShipped = CInt(oRS2("IntlCountofKits").Value) + IntlTotalKitsShipped
	IntlTotalShippers = CInt(oRS("IntlTotalShippers").Value) + IntlTotalShippers
	IntlTotalShipperLines = CInt(oRS("IntlTotalLinesShipped").Value) + IntlTotalShipperLines
	IntlTotalQuantityShipped = CInt(oRS("IntlTotalItemsShipped").Value) + IntlTotalQuantityShipped
	GrandTotalShippers = TotalShippers + IntlTotalShippers
	GrandTotalShipperLines = TotalShipperLines + IntlTotalShipperLines
	GrandTotalQuantityShipped = TotalQuantityShipped + IntlTotalQuantityShipped
	GrandTotalKitsShipped = IntlTotalKitsShipped + TotalKitsShipped		

	oRS2.MoveNext
	Loop
	oRS2.Close

	oRS.MoveNext
	Loop
	oRS.Close
	Set oRS2 = Nothing			
	Set oRS = Nothing
	oConn.Close
	Set oConn = Nothing 
	Response.Write "<tr align=center>"
	Response.Write "<td><b>" & IntlTotalShippers & "</b></td>"
	Response.Write "<td><b>" & IntlTotalShipperLines & "</b></td>"
	Response.Write "<td><b>" & IntlTotalQuantityShipped & "</b></td>"
	Response.Write "<td><b>" & IntlTotalKitsShipped & "</b></td></tr>"
	Response.Write "</td></tr></table></td></tr>"
	Response.Write "<tr align=center><td colspan=2><table><tr align=center><td>"
	Response.Write "<b>Grand Total Shippers:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Grand Total Line Items Shipped:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Grand Total Quantity Items Shipped:</b>"
	Response.Write "</td>"
	Response.Write "<td>"
	Response.Write "<b>Grand Total Configured Kits Shipped:</b>"
	Response.Write "</td></tr>"
	Response.Write "<tr align=center>"
	Response.Write "<td><b>" & GrandTotalShippers & "</b></td>"
	Response.Write "<td><b>" & GrandTotalShipperLines & "</b></td>"
	Response.Write "<td><b>" & GrandTotalQuantityShipped & "</b></td>"
	Response.Write "<td><b>" & GrandTotalKitsShipped & "</b></td></tr>"
	Response.Write "</td></tr></table>"
ELSE
END IF
%>

</body>

</html>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

我很好奇你是如何确定数字被排除在总数之外的,因为你已经承认对它的工作原理知之甚少。

跳出来的一件事是该列上的COUNT(DISTINCT)。每天进行聚合然后总计可能会比在整个集合中同时聚合更多的数字。我猜这是问题所在。

作为第二个猜测,这段代码对我来说很奇怪。为什么它会循环遍历oRS2但重复添加来自oRS的值?

DO WHILE NOT oRS2.EOF
    Response.Write "<td>" & oRS2("CountOfKits").Value & "</td></tr>"
    TotalKitsShipped = CInt(oRS2("CountofKits").Value) + TotalKitsShipped

    TotalShippers = CInt(oRS("TotalShippers").Value) + TotalShippers
    TotalShipperLines = CInt(oRS("TotalLinesShipped").Value) + TotalShipperLines
    TotalQuantityShipped = CInt(oRS("TotalItemsShipped").Value) + TotalQuantityShipped
    oRS2.MoveNext
LOOP