Register Blogs FAQ Members List Social Groups Calendar Search Today's Posts Mark Forums Read

Go Back   DeveloperBarn Forums > Programming & Scripting > ASP Development

Sponsored Links

Discuss "Loop / Summary help" in the ASP Development forum.

ASP Development - Learn coding practices and tips to get the best out of your Active Server Pages (ASP). Visit the ASP Development forum for help with ASP/VBScript and ASP/JScript applications.


Closed Thread
 
LinkBack Thread Tools Display Modes
  #1  
Old April 10th, 2008, 04:03 PM
Rebelle's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 295
Thanks: 54
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
Default Loop / Summary help

All,

I have the following code, which returns 49 records with the lines that I've commented out. but when I try to use the lines that are commented out, I don't get any results.

I'm trying to summarize the 49 records to display only 1 record. Tools and OnOrder repeat and I want to only show them once...and sum the SumShipped.

Code:
<%
'strLastTools = rs("Tools")
'strLastRegionID = rs("RegionID")
'strLastDistrictID = rs("DistrictID")
	do while not rs.EOF
		if i > iStopRec then
			exit do
		end if
	
	strQ1 = rs("OnOrder1")
	strQ1Shipped = strQ1Shipped + rs("SumShipped")

	strCurrentTools = rs("Tools")
'	strCurrentRegionID = rs("RegionID")
'	strCurrentDistrictID = rs("DistrictID")

'	If strCurrentTools<>strLastTools Then

		Response.Write("<tr bgcolor=#D3D3D3><td colwidth=""150""><b>" & strCurrentTools & "</b></a></td>")
			Response.Write("<td align=center colwidth=""50""><b>" & strQ1 & "</b></td>")
		strQ1 = 0
			Response.Write("<td align=center colwidth=""50""><b>" & strQ1Shipped & "</b></td></tr>")
		strQ1Shipped = 0
'	End If
'	strLastTools = strCurrentTools	
'	strLastRegionID = strCurrentRegionID
'	strLastDistrictID = strCurrentDistrictID

%>


<%
	i = i + 1
  rs.MoveNext
loop
%>
Any suggestions/help is greatly appreciated.
Sponsored Links
  #2  
Old April 11th, 2008, 03:57 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 460
Thanks: 31
Thanked 43 Times in 43 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

The reason it shows no records must have something to do with this line:-
Code:
If strCurrentTools<>strLastTools Then
Without seeing how these variables are populated it's difficult to see what the problem is.

Code:
'strLastTools = rs("Tools")

strCurrentTools = rs("Tools")
Both strCurrentTools and strLastTools are populated from the same recordset. How are these values retrieved from your db?

Could you post some more code?
  #3  
Old April 11th, 2008, 08:08 AM
Rebelle's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 295
Thanks: 54
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
Default

Thanks for the reply Richyrich!

Here is my sql statement:
Code:
sSql = "Select ToolID,RegionID,DistrictID,Tools, OnOrder1, OnOrder2, OnOrder3, OnOrder4, RolloutShipped,Sum(IsNull([RolloutShipped],0)) as SumShipped From vwSumShipped"
sSql = sSql & " WHERE 1=1"


sSql = sSql & " Group By ToolID,RegionID,DistrictID,Tools,OnOrder1,OnOrder2,OnOrder3,OnOrder4,RolloutShipped,SumShipped"
sSql = sSql & " Order By " & sOrderBy & " " & sOrderByDir
Then here is what I have:
Code:
<%
Dim strCurrentTools, strLastTools, strCurrentRegionID, strLastRegionID, strCurrentDistrictID, strLastDistrictID, strQ1, strQ1Shipped
if rs.EOF and rs.BOF then
else
%>


    <TABLE border="3" width=1000>
      <TR bgcolor="gray">
<TD>Tools</TD>
<td><b>Q1-08</b></td>
<td><b>Q1 Shipped</b></td>
</TR>

</CENTER>

<%
strLastTools = rs("Tools")
strLastRegionID = rs("RegionID")
strLastDistrictID = rs("DistrictID")
	do while not rs.EOF
		if i > iStopRec then
			exit do
		end if
	
	strQ1 = rs("OnOrder1")
	strQ1Shipped = strQ1Shipped + rs("SumShipped")

	strCurrentTools = rs("Tools")
	strCurrentRegionID = rs("RegionID")
	strCurrentDistrictID = rs("DistrictID")

'	If strCurrentTools<>strLastTools Then

		Response.Write("<tr bgcolor=#D3D3D3><td colwidth=""150""><b>" & strCurrentTools & "</b></a></td>")
			Response.Write("<td align=center colwidth=""50""><b>" & strQ1 & "</b></td>")
		strQ1 = 0
			Response.Write("<td align=center colwidth=""50""><b>" & strQ1Shipped & "</b></td></tr>")
		strQ1Shipped = 0
'	End If
'	strLastTools = strCurrentTools	
'	strLastRegionID = strCurrentRegionID
'	strLastDistrictID = strCurrentDistrictID

%>


<%
	i = i + 1
  rs.MoveNext
loop
%>
<%
rs.Close
end if
%>
I'm getting results like this 49 records, instead of it just showing me one line with the Q1 Shipped column summed up:
ToolName------------Q1-08-------------Q1 Shipped
Hammer----------------6-------------------1
Hammer----------------6-------------------0
Hammer----------------6-------------------1
Hammer----------------6-------------------0

Thanks for your help.
  #4  
Old April 11th, 2008, 08:55 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 460
Thanks: 31
Thanked 43 Times in 43 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

I'm not quite sure where all the other fields you include in the SELECT statement come into it?

I would have thought something as simple as this would suffice.
Code:
sSQL = "SELECT Tools, onOrder1, Sum(IsNull([RolloutShipped],0)) As sum_shipped FROM vwSumShipped WHERE 1=1 GROUP BY Tools ORDER BY " & sOrderBy & " " & sOrderByDir
These are the only fields I can see you using.

Also not sure what the significance of WHERE 1=1 is?

Could you give an example of what it looks like against what you want it to look like?

Comments on this post
Rebelle agrees: Thanks a bunch!!!
The Following User Says Thank You to richyrich For This Useful Post:
jmurrayhead (May 27th, 2008)
  #5  
Old April 11th, 2008, 09:26 AM
Rebelle's Avatar
Barn Enthusiast
 
Join Date: Mar 2008
Posts: 295
Thanks: 54
Thanked 1 Time in 1 Post
Rep Power: 1
Rebelle is on a distinguished road
Default

Hey Richyrich,

Thanks for the info.

about the where 1=1....someone told me to always use this.

Anyhow, with the info above, I removed the other fields....I was thinking I needed them...but I did added the OnOrder2,OnOrder3, and OnOrder4 back in.

With your help, I got it to look like I wanted.

Like:
Tools----------------Q1-08--------Q1 Shipped
Hammer----------------6---------------2
Jigsaw-----------------4---------------3


I guess I didn't need all those extra commented out lines....so I removed them as so:

Code:
<%
	do while not rs.EOF
		if i > iStopRec then
			exit do
		end if
	
	strTools = rs("Tools")
	strQ1 = rs("OnOrder1")
	strQ1Shipped = strQ1Shipped + rs("SumShipped")
	strQ2 = rs("OnOrder2")
	strQ3 = rs("OnOrder3")
	strQ4 = rs("OnOrder4")

	strCurrentTools = rs("Tools")

		Response.Write("<tr bgcolor=#D3D3D3><td colwidth=""150""><b>" & strTools & "</b></a></td>")
			Response.Write("<td align=center colwidth=""50""><b>" & strQ1 & "</b></td>")
		strQ1 = 0
			Response.Write("<td align=center colwidth=""50""><b>" & strQ1Shipped & "</b></td>")
		strQ1Shipped = 0
			Response.Write("<td align=center colwidth=""50""><b>" & strQ2 & "</b></td>")
		strQ2 = 0
			Response.Write("<td align=center colwidth=""50""><b>" & strQ3 & "</b></td>")
		strQ3 = 0
			Response.Write("<td align=center colwidth=""50""><b>" & strQ4 & "</b></td></tr>")
		strQ4 = 0	

%>
I guess what was throwing me off too...was I had the sumShipped in the groupby.

Thanks again!
  #6  
Old April 11th, 2008, 09:33 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 460
Thanks: 31
Thanked 43 Times in 43 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

No Problem. Glad you got it sorted.

In general, you only need to group by the elements you want contracted. In this case you only wanted to see 1 of each Tool element, so if you group everything by that element, you will only receive 1 record for each instance.

Hope that makes sense...
  #7  
Old April 11th, 2008, 09:38 AM
mehere's Avatar
Super Sarcasm Mistress
 
Join Date: Mar 2008
Location: Wide Awake In Dreamland
Posts: 160
Thanks: 12
Thanked 29 Times in 27 Posts
Rep Power: 1
mehere will become famous soon enough

Awards Showcase
Microsoft SQL Server Classic ASP 
Total Awards: 2

Default

Quote:
Originally Posted by richyrich View Post
Also not sure what the significance of WHERE 1=1 is?
Using the WHERE 1 = 1 ... is good to use when building dynamic SQL statements ... for example a search function on your site. this way when the WHERE clause is created ... you use AND or OR without figuring out when to use WHERE. something like this
Code:
strSQL = "SELECT * FROM tbl_TableName WHERE 1= 1 "
if request("field1") <> "" then
     strSQLEND = strsQLEND & "OR field1 = " & request("field1") & " "
end if
if request("field2") <> "" then
      strSQLEND = strsQLEND & "OR field2 = " & request("field2") & " "
end if
strSQL = strSQL & strSQLEND
set rs = dbConn.Execute(strSQL)
__________________
Quote of the Month:
Quality: The race for quality has no finish line- so technically, it's more like a death march.

Questions to Ponder:
What do you do when you see an endangered animal eating an endangered plant?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright © 2008 sbenj69
  #8  
Old April 11th, 2008, 10:12 AM
richyrich's Avatar
Super Moderator
 
Join Date: Mar 2008
Location: Somewhere only we know...
Posts: 460
Thanks: 31
Thanked 43 Times in 43 Posts
Blog Entries: 1
Rep Power: 2
richyrich will become famous soon enoughrichyrich will become famous soon enough

Awards Showcase
JavaScript Classic ASP 
Total Awards: 2

Default

Quote:
Originally Posted by mehere View Post
Using the WHERE 1 = 1 ... is good to use when building dynamic SQL statements ... for example a search function on your site. this way when the WHERE clause is created ... you use AND or OR without figuring out when to use WHERE. something like this
Code:
strSQL = "SELECT * FROM tbl_TableName WHERE 1= 1 "
if request("field1") <> "" then
     strSQLEND = strsQLEND & "OR field1 = " & request("field1") & " "
end if
if request("field2") <> "" then
      strSQLEND = strsQLEND & "OR field2 = " & request("field2") & " "
end if
strSQL = strSQL & strSQLEND
set rs = dbConn.Execute(strSQL)
That's good. Hadn't thought of that...

Comments on this post
sbenj69 agrees:
Closed Thread

  DeveloperBarn Forums > Programming & Scripting > ASP Development

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Programmatically Add Item to Validation Summary jmurrayhead .Net Code Samples 0 March 20th, 2008 09:41 AM


All times are GMT -4. The time now is 09:21 AM.



Content Relevant URLs by vBSEO 3.2.0