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 "Summing for each group only" 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 May 22nd, 2008, 02:10 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
Question Summing for each group only

Hi All,
I need help trying to get results like below, the part in red.

I can calculate the:
qtr qtys to get qty total
and
qty total x cost to get total cost
my issue is with adding only for each Group (see text in red).

right now i can sum all to get grand total, but need help with just making it add for group1,
then group 2, etc. is this normally handled in the sql query or in the
asp rs?

any help/suggestions is greatly appreciated.


----------------1Q - 2Q - 3 Q - 4Q - Qty Total- Cost - Total Cost
Group1
-subGroup1
-----------Item1 1 - 1 - 0 - 0 - 2 - $100 - $200
-----------Item2 0 - 1 - 0 - 1 - 2 - $100 - $200
-subGroup2
-----------Item3 1 - 0 - 0 - 1 - 2 - $100 - $200
-----------Item4 0 - 0 - 0 - 1 - 1 - $100 - $100

Group2
-subGroup3
-----------Item5 0 - 0 - 0 - 1 - 1 - $100 - $100
-----------Item6 2 - 2 - 2 - 1 - 7 - $100 - $700

ETC....

Group1Totals 2 - 2 - 0 - 3 - 7 - blank -$700
Group2Totals 2 - 2 - 2 - 1 - 7 - blank -$800

ETC....
GRAND Total 4 - 4 - 2 - 4 - 14 - blank -$1500
Sponsored Links
  #2  
Old May 30th, 2008, 11:00 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

Ok, I've taken out the code for my grand total, I'm trying to just get the totals for each Category.

I went back and grouped my sql view and create SumItem for ItemCost.

Here is what I have so far but I'm having some trouble. 2 Problems - problem one is I'm not getting the right CategoryDesc to match the right Sum (code in red). problem two, it's not really where I want to have it but I've moved it and moved it and not getting the results I want. I would like this row to really be at the bottom (like example in first post). Please ignore the blank rows in code below, I haven't yet added those.

Code:
<%
Dim strLastCategory, strLastGrp, strCost,strSumItem
%>

    <TABLE border="3" width=1000>
<col width=100><col width=10><col width=10><col width=10><col width=10><col width=10><col width=20><col width=20><col width=20><col width=20><col width=20><col width=20>
      <TR bgcolor="#6699CC">
<TH>Description</TH>
<TH>1Q</TH>
<TH>2Q</TH>
<TH>3Q</TH>
<TH>4Q</TH>
<TH>Total Qty</TH>
<TH>Cost</TH>
<TH>1Q Sum</TH>
<TH>2Q Sum</TH>
<TH>3Q Sum</TH>
<TH>4Q Sum</TH>
<TH>Year Total</TH>
      </TR>
<%
strLastCategory = ""
strLastGrp = ""
strCost = ""


	do while not rs.EOF
	if i > iStopRec then
		exit do
	end if

	strCurrentCategory = rs("CategoryDesc")
	strCurrentGrp = rs("GrpDesc")
	
	
	If strCurrentCategory<>strLastCategory Then
	Response.Write("<tr bgcolor=#D3D3D3><td colwidth=""100"" colspan=""12""><b>" & strCurrentCategory & "</b></td></tr>")
	Response.Write("<tr bgcolor=yellow><td colwidth=""100""><b>" & strCurrentCategory & "</b></td>")
	Response.Write("<td colwidth=""100"">&nbsp;</td>")
	Response.Write("<td colwidth=""100"">&nbsp;</td>")
	Response.Write("<td colwidth=""100"">&nbsp;</td>")
	Response.Write("<td colwidth=""100"">&nbsp;</td>")
	Response.Write("<td colwidth=""100"">&nbsp;</td>")
	Response.Write("<td colwidth=""100""><b>" & strSumItem & "</b></td></tr>")
	strSumItem = 0	
             End If
	strLastCategory = strCurrentCategory
	strSumItem = strSumItem + rs("SumItem")

	If strCurrentGrp<>strLastGrp Then	
	Response.Write("<tr bgcolor=#D3D3D3><td colwidth=""100"" colspan=""12""><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;" & strCurrentGrp & "</i></td></tr>")	
	End If
	strLastGrp = strCurrentGrp
	
	i = i + 1
	
%>
<%
intItemCost = rs("ItemCost")
intItem = FormatCurrency((intItemCost / 1000),0)
%>

<TR>
<TD align=right><%=rs("ItemDesc")%> &nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#E0FFFF" align=center><%= intItem %></TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
<TD bgcolor="#F4A460" align=center>&nbsp;</TD>
 </TR>	

<%	
rs.MoveNext
loop
%>

<%
rs.Close
%>

</table>
Thanks for any suggestions with this.
  #3  
Old May 30th, 2008, 11:05 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

i'm not sure i'm understanding you ... can you give an example of what is displaying, what the data is and why it's wrong.
__________________
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
  #4  
Old May 30th, 2008, 11:27 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

ok, i hope this shows up ok.

Description---1Q--2Q--3Q--4Q--TotalQty--Cost--1QSum--2QSum--3QSum--4QSum--YrTotal

CategoryName1
CategoryName1---------------------------Cost Is Blank (should show $36)
--GroupName1
----Item1--------------------------------$10----------------------------
----Item2--------------------------------$11----------------------------
--GroupName2
----Item3--------------------------------$5-----------------------------
----Item4--------------------------------$2-----------------------------
----Item5--------------------------------$8-----------------------------
CategoryName2
CategoryName2---------------------------$36 (this total is correct 4 Category1, when it should show Category2 total $15)
--GroupName3
----Item6---------------------------------$7-----------------------------
----Item7---------------------------------$5-----------------------------
--GroupName4
----Item8---------------------------------$3-----------------------------
CategoryName3
CategoryName3---------------------------$15 (this total is correct 4 Category2)

Right now, I have all the qtr data blank, concentrating on the Cost field here.
  #5  
Old May 30th, 2008, 11:31 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

in order to get the correct sum ... in the correct category you need to display it at the end. since you are looping through the records to get your total. until you get to the records, you don't have a total.
  #6  
Old May 30th, 2008, 11:36 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

Quote:
Originally Posted by mehere View Post
in order to get the correct sum ... in the correct category you need to display it at the end. since you are looping through the records to get your total. until you get to the records, you don't have a total.
Thanks Mehere, but when I've tried putting the code in red at the end, I only get one big grand total. Do I have to perform another loop after the one I have?
  #7  
Old May 30th, 2008, 11:59 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

okay ... try changing this line
Code:
If strCurrentCategory<>strLastCategory Then
to this line
Code:
If strCurrentCategory<>strLastCategory and strLastCategory <> "" Then
  #8  
Old May 30th, 2008, 12:16 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

Quote:
Originally Posted by mehere View Post
okay ... try changing this line
Code:
If strCurrentCategory<>strLastCategory Then
to this line
Code:
If strCurrentCategory<>strLastCategory and strLastCategory <> "" Then
whoops...hehe...that gets rid of my first CategoryName1 all together and my CategoryName2 shows but with cost sum of Category1 items.

I'll keep moving it around and see where I can put it....when I tried to put at the end it would just give me the lastCategoryName with a total of everything...I need to to show each CategoryName and sum items in that Category.

Thanks...I'll keep trying but if you have any other ideas please let me know.
  #9  
Old May 30th, 2008, 12:21 PM
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

okay ... try this:
Code:
If strCurrentCategory<>strLastCategory Then
if strLastCategory <> "" then
     Response.Write("<tr bgcolor=yellow><td colwidth=""100""><b>" & strLastCategory & "</b></td>")
     Response.Write("<td colwidth=""100"">&nbsp;</td>")
     Response.Write("<td colwidth=""100"">&nbsp;</td>")
     Response.Write("<td colwidth=""100"">&nbsp;</td>")
     Response.Write("<td colwidth=""100"">&nbsp;</td>")
     Response.Write("<td colwidth=""100"">&nbsp;</td>")
     Response.Write("<td colwidth=""100""><b>" & strSumItem & "</b></td></tr>")
     strSumItem = 0
end if
Response.Write("<tr bgcolor=#D3D3D3><td colwidth=""100"" colspan=""12""><b>" & strCurrentCategory & "</b></td></tr>")

Last edited by mehere; May 30th, 2008 at 12:37 PM.
  #10  
Old May 30th, 2008, 12:32 PM
BLaaaaaaaaaarche's Avatar
Moderator
 
Join Date: Mar 2008
Posts: 55
Thanks: 10
Thanked 7 Times in 5 Posts
Rep Power: 1
BLaaaaaaaaaarche is on a distinguished road

Awards Showcase
HTML & CSS Classic ASP 
Total Awards: 2

Default

Not sure exactly what you are trying to do, but to display your categories for each change in category, use this logic:

Code:
Do While Not rs.EOF
	strCurrentCategory = rs("Category")
	If strCurrentCategory <> strLastCategory OR strLastCategory = "" Then
		response.write strCurrentCategory
	End If


	' Display Records Here


	strLastCategory = strCurrentCategory
rs.MoveNext
Loop
You must make sure that you ORDER BY the category field.
__________________
"You'll never be as perfect as BLaaaaaaaaarche."
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


All times are GMT -4. The time now is 03:15 PM.



Content Relevant URLs by vBSEO 3.2.0