473,586 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Combine rows, accumulate total

6 New Member
Can anyone please help?
I'm trying to combine 2 or more similar rows into one row, accumulating a field for quantity. E.g.:

index, field1, field2, quantity
1, ccc, bbb, 5
2, aaa, bbb, 15
3, aaa, bbb, 3

Because the contents of fields 1 & 2 are the same in rows 2 and 3, I want to end up with only one row with the total quantity.
index, field1, field2, quantity
1, ccc, bbb, 5
2, aaa, bbb, 18

It doesn't matter if the row is a new one with a new index. Eg.
index, field1, field2, quantity
1, ccc, bbb, 5
4, aaa, bbb, 18

Thanks in advance for any help.
Dec 8 '06 #1
8 6914
cycleops
6 New Member
I suppose I should say that I'm not necessarily looking for a complete answer, any pointers would be good.
My approaches have been using
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT
which (obviously) is wrong, and the other bad approach was to select all possibly relevant rows and process them in PHP.
Again, any help is appreciated.
Dec 8 '06 #2
ronverdonk
4,258 Recognized Expert Specialist
This what you want?
Expand|Select|Wrap|Line Numbers
  1.  select field1,field2, sum(quantity) from xx group by field1, field2;
Ronald :cool:
Dec 8 '06 #3
cycleops
6 New Member
I think that's a piece of it -- it produces a correct record. What would you do next, insert it and delete the 2 rows? How is that done as one query?

Thanks so much Ronverdonk. I'm going to try expanding on your input.
Dec 8 '06 #4
cycleops
6 New Member
I'm still drawing a blank.
It's the approach I'm struggling with -- it's so different from a procedural language.
How do you combine such diverse operations into one query?
I seem to have a SELECT, to find the similar rows,
an UPDATE or INSERT, to write the total quantity record
and one or more DELETEs, to get rid of the spurious row(s)
Dec 8 '06 #5
ronverdonk
4,258 Recognized Expert Specialist
I can give you it up to the INSERT. But the Delete in the same statement? I don't think so, but maybe someone knows better.
Expand|Select|Wrap|Line Numbers
  1. INSERT INTO xx (field1,field2,quantity) 
  2.    SELECT field1,field2, sum(quantity) AS quantity 
  3.    FROM  xx 
  4.    GROUP BY field1, field2;
Ronald :cool:
Dec 8 '06 #6
cycleops
6 New Member
Wow, I stumped the Community Expert!
As an SQL novice, I assumed this part would be straightforward . I'll have to revisit the rest of my application and find a way to avoid the situation arising.
Thanks for the input, Ronald.
Dec 8 '06 #7
ronverdonk
4,258 Recognized Expert Specialist
I am not so easily stumped!

Are you talking about all functions in 1 query or all functions in 1 statement?
You can of course have more then 1 statement in a string and send that as 1 query in your programming language to the db.

Ronald :cool:
Dec 8 '06 #8
cycleops
6 New Member
I'm not sure what the difference is. Don't think that it matters except for the end result (similar rows are combined to show total, the row with total replaces the 2 or more rows that were in the database).
Dec 8 '06 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

7
12056
by: Egor Shipovalov | last post by:
I'm implementing paging through search results using cursors. Is there a better way to know total number of rows under a cursor than running a separate COUNT(*) query? I think PostgreSQL is bound to know this number after the first FETCH, isn't it? On a side note, why queries using LIMIT are SO terribly slow, compared to cursors and...
5
2994
by: jhutchings | last post by:
Hello everyone, I have a database where I collect shipment data from various tables. However, I have a problem. Whenever I want to see shipping data for orders that were set to ship on or before a certain date (in this case January 30th) the database will return 2 rows for an order as you can see below. Order ID: Line: Due Date: ...
5
7765
by: cesco | last post by:
I have a set of pairs defined as follow: set< pair<UserEquipment*, double> > numberOfFreqSlotsToAdd; and I need to iterate through all the elements of the set in order accumulate the second field of the pair (that is double). Is there any way I can use the algorithm accumulate to accomplish this? The following line of code double unit...
1
2624
by: ktang3227 | last post by:
Hi all, I have a table which list all the different product# and quantity of the product in each row. But for some reason, some of the product splites into 2 rows. For example; Product A has a quantity of 30, then it will shows like this: Product A 15 Product A 15
2
1389
by: is49460 | last post by:
Hello everyone! All ideas would be very appreciated. I have a table with let's say information about sales by category: product model price car honda $10000 car nissan $11000 truck chevy $15000 truck ford $16000
2
5710
denny1824
by: denny1824 | last post by:
Hi everyone, Here is my problem. I have a Table Variable (I could easily turn it into a Temp Table instead) that will sometimes have rows that are identical except for one specific column. The column is of nvarchar. When the rest of the columns for those rows are identical, I want to combine all of the values of that column into a single...
2
10618
by: kveerareddy | last post by:
How can i get the total rows of a table: If i use "select count(*) from <tablename>" it will take lot of time to get the total row count. for example in MSSQL we have "SELECT ROWS FROM sysindexes WHERE id = OBJECT_ID(<Table name>) AND indid < 2" I am looking some system index tables which hold the total rows as a statistic values of the...
21
5579
by: MNNovice | last post by:
I have a report (rptAPDetails) that is based on qryAPDetails shows grant expenses for all invoice payments. Similarly I have a report (rptPayDetails) that is based on qryPayDetails which shows payroll expenses for grants. I would like to generate a report combining these two reports. Where I will be able to combine expenses for all grants...
2
4992
by: mzmatterafact | last post by:
I'm back and please be warned I'm a total NEWBIE, and i've had success with my previous post so I would like to buy another vowel! Now i have taken my csv file and imported to a DataTable, i've parsed out the rows and columns to match two colums in SQL. If the two fields match, i want to create a record on a SQL Table. The problem is, I have...
0
7915
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7841
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8204
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8220
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5712
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5392
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3869
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2345
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1452
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.