SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Iron Speed
Go Back   ASP Free ForumsDatabaseSQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
  #1  
Old April 8th, 2008, 07:49 AM
moracom's Avatar
moracom moracom is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 212 moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 13 h 54 m 51 sec
Reputation Power: 39
Send a message via MSN to moracom Send a message via Yahoo to moracom
Database - Recordset - Display correct SUM

I have this SQL statement in my ASP page that retrieves some records from a database. There are 3 records in the database but I want it to show me the sum of column LQShipped and show me the latest date in the LastShipDate column. Currently it shows me all three records and the LQShipped is not totalled. I just want one record with the total of LQShipped and the latest date in the LastShipDate.

Code:
SELECT ID, SoldTo, SoldToName, SoldToAdd1, SoldToAdd2, SoldToAdd3, SoldToAdd4, SoldToCty, SoldToState, SoldToZip, LastShipDate, ItemNumber, ItemDescription, UOM, Conversion, SUM(LQShipped) as LQShipped, TQOrder, LSTake, Balance, InStore, SellThru, RunningSellThru, RepID, RepName, Stamp FROM dbo.Inventory WHERE SoldTo = '" & Request.Form("SoldTo") & "' AND ItemNumber = '" & Request.Form("Item") & "' GROUP BY ID, SoldTo, SoldToName, SoldToAdd1, SoldToAdd2, SoldToAdd3, SoldToAdd4, SoldToCty, SoldToState, SoldToZip, LastShipDate, ItemNumber, ItemDescription, UOM, Conversion, LQShipped, TQOrder, LSTake, Balance, InStore, SellThru, RunningSellThru, RepID, RepName, Stamp ORDER BY ItemDescription, LastShipDate ASC


Thanks in advance for your help.
__________________
If I have been of useful help please don't hesitate to provide me with some Reputation points

Be in Control of your Data and get it your way. Customized Web Applications.

Reply With Quote
  #2  
Old April 8th, 2008, 07:54 AM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,483 mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 3 Weeks 3 Days 21 h 7 m 11 sec
Reputation Power: 1450
try this:
Code:
SELECT 
	ID, SoldTo, SoldToName, SoldToAdd1, SoldToAdd2, SoldToAdd3, SoldToAdd4, 
	SoldToCty, SoldToState, SoldToZip, Max(LastShipDate) as LastShipDate, ItemNumber, ItemDescription, 
	UOM, Conversion, SUM(LQShipped) as LQShipped, TQOrder, LSTake, Balance, 
	InStore, SellThru, RunningSellThru, RepID, RepName, Stamp 
FROM dbo.Inventory 
WHERE SoldTo = '" & Request.Form("SoldTo") & "' 
AND ItemNumber = '" & Request.Form("Item") & "' 
GROUP BY 
	ID, SoldTo, SoldToName, SoldToAdd1, SoldToAdd2, SoldToAdd3, SoldToAdd4, 
	SoldToCty, SoldToState, SoldToZip, ItemNumber, ItemDescription, 
	UOM, Conversion, TQOrder, LSTake, Balance, 
	InStore, SellThru, RunningSellThru, RepID, RepName, Stamp 
ORDER BY ItemDescription ASC
__________________
Come JOIN the party!!!

Quote of the Month:
Trouble: Luck can't last a lifetime unless you die young.

Questions to Ponder:
Do cemetery workers prefer the graveyard shift?

iif([sarcasm]=true,iif([you have to ask]=true,"didn't work","ha ha ha"),"not sarcasm")
copyright© 2008 sbenj69

Last edited by mehere : April 8th, 2008 at 07:59 AM.

Reply With Quote
  #3  
Old April 8th, 2008, 08:16 AM
moracom's Avatar
moracom moracom is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 212 moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 13 h 54 m 51 sec
Reputation Power: 39
Send a message via MSN to moracom Send a message via Yahoo to moracom
still showing me 3 records and no total in the LQShipped column. Any other suggestions?

Reply With Quote
  #4  
Old April 8th, 2008, 08:58 AM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,483 mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 3 Weeks 3 Days 21 h 7 m 11 sec
Reputation Power: 1450
it shouldn't. i ran a quick test of the query and it seemed to work here, provided every column in your GROUP BY clause is exactly the same.

Reply With Quote
  #5  
Old April 8th, 2008, 09:21 AM
moracom's Avatar
moracom moracom is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Posts: 212 moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level)moracom User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 1 Day 13 h 54 m 51 sec
Reputation Power: 39
Send a message via MSN to moracom Send a message via Yahoo to moracom
the only columns that are not the same are the LastShipDate hence i just want to show the latest date and the LQShipped hence i want to sum it up. Any ideas?

Reply With Quote
  #6  
Old April 8th, 2008, 10:00 AM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,483 mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 3 Weeks 3 Days 21 h 7 m 11 sec
Reputation Power: 1450
if that's the case, then what i posted should work. can you post the DDL for your table along with insert statements for those 3 recrods and i can test this. if i could replicate your table along with the 3 records you're talking about, i could get a better picture of what your issue is. but as stated, a quick test shows that this sample will work.

Reply With Quote
  #7  
Old April 8th, 2008, 10:11 AM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,483 mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 3 Weeks 3 Days 21 h 7 m 11 sec
Reputation Power: 1450
-->Moved to SQL Development as it's query related not ASP related.

Reply With Quote
  #8  
Old April 8th, 2008, 10:16 AM
mehere's Avatar
mehere mehere is offline
Senior Sarcasm Wizardess
Click here for more information.
 
Join Date: Feb 2005
Location: Dreamland
Posts: 12,483 mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)mehere User rank is General 7th Grade (Above 100000 Reputation Level)  Folding Points: 10976 Folding Title: Novice Folder
Time spent in forums: 4 Months 3 Weeks 3 Days 21 h 7 m 11 sec
Reputation Power: 1450
this is a quick sample that i did
Code:
create table testtable (
	id int,
	soldto int,
	soldtoname varchar(25),
	lastshipdate smalldatetime,
	lqshipped money
)

insert into testtable values (1,1,'jane doe','01/05/2008',5.00)
insert into testtable values (1,1,'jane doe','02/05/2008',10.00)
insert into testtable values (1,1,'jane doe','03/05/2008',15.00)

select id, soldto, soldtoname, max(lastshipdate), sum(lqshipped)
from testtable
group by id, soldto, soldtoname
if you run this in your database you will see that only one record is returned, with the date of 3/5/08 and the sum of $30.00.

the only thing i can think of in your case ... is the ID a unique value for each record. if so, you can't use it in your group by clause.
Comments on this post
Wolffy agrees: Bingo, ID's are usually unique and thus can't be used in a query that GROUPS. Wouldn't make sense
anyway.

Reply With Quote
  #9  
Old April 8th, 2008, 12:17 PM
geino geino is offline
Registered User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Apr 2008
Posts: 6 geino User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 19 m 20 sec
Reputation Power: 0
moracom

Take a look at:
forums.aspfree.com/asp-development-5/asp-data-read-and-update-problems-220470.html

The code I put in there does a sumTotal that adds the total of each record and displays the result at the bottom.

After the database is opened it defines the variable and sets it to zero
Dim sumTotal
sumTotal = "0.00"

It then goes into the Do/While loop
After all of the variables are defined the sumTotal is calculated using the current sum plus the current total value

It then returns to the loop until end of file.
The sumTotal is then displayed

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseSQL Development > Database - Recordset - Display correct SUM


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |