If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > Consolidating Like Items and Summing Totals

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-28-02, 15:53
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Consolidating Like Items and Summing Totals

I need to know if there is an easy way to consolidate lines of the same item but differing quantities.

For example:

ITEM QTY
----- ----
ABC 1
ABC 3
ABC 6
ABD 2
ABD 1
ABE 1


I would like to display this information as:

ITEM QTY
----- ----
ABC 10
ABD 3
ABE 1

Summary: I want to consolidate those items which appear in the table more than once by combining their quantities and leaving one row that has the sum of all.

Your help is greatly appreciated.
TechRick
Reply With Quote
  #2 (permalink)  
Old 08-28-02, 17:39
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
selecting the totals is easy, right?

select ITEM, SUM(QTY)
from yourtable
group by ITEM

however, consolidating them and "leaving one row" is tricky

i suggest writing the total rows to a temporary table, deleting all rows from the original, then inserting the total rows back

create table temptable
( ITEM char(3)
, QTY integer )

insert into temptable
select ITEM, SUM(QTY)
from yourtable
group by ITEM

delete from your table

insert into yourtable
select * from temptable

drop temptable


rudy
http://rudy.ca/
Reply With Quote
  #3 (permalink)  
Old 08-29-02, 17:41
techrick techrick is offline
Registered User
 
Join Date: Aug 2002
Location: Southern California
Posts: 35
Thanks for the help. I was able to get it worked out with your suggestions.

Best Regards,
TechRick
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On