473,472 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

SQL Combining multiple rows into a single row

26 New Member
So I have a table named Table1. Inside Table1 are the fields: SKU, VendrItem, Qty and EachCost.
Expand|Select|Wrap|Line Numbers
  1. SKU   VendrItem   Qty   EachCost
  2. 123   ABC         2     $2      
  3. 123   ABC         5     $2      
  4. 475   TGE         4     $9      
  5. 887   ASW         8     $4      
  6. 475   TGE         45    $9       
  7. 788   WQQ         14    $1       
  8. 475   TGE         20    $9       
  9. 945   WWT         77    $3       
  10. 788   WQQ         8     $1      
  11.  
I want to combine the rows with the same SKU number.

I want less rows without changing anything other than the Qty field.

So instead of having multiple SKU rows with different quantities, it should have a single SKU row with the combined quantities.

So I want the table to eventually look like this:
Expand|Select|Wrap|Line Numbers
  1. SKU   VendrItem   Qty   EachCost
  2. 123   ABC         7     $2            
  3. 475   TGE         69    $9      
  4. 887   ASW         8     $4            
  5. 788   WQQ         22    $1             
  6. 945   WWT         77    $3       
  7.  
I don't know how (or even if) I can do this. What code in SQL do I write to combine rows?
Jul 9 '14 #1

✓ answered by twinnyfo

If this is your case, I would not alter your tables, but still use an aggregate query.

To use an aggregate query, in the query builder, there should be a Summation notation (capital Greek letter sigma). Click on that and this will add another row to your query builder called "Total". When you add fields to your query, the default "Total" row will be "Group By". But you can change this to Sum, Count, Where, Min, Max and others.

You will want to Group By "SKU", "Vendor Item" and "Cost" and Sum "Quantity".

Hope this helps. Aggregate queries are very useful when you get the hang of using them.

7 2938
twinnyfo
3,653 Recognized Expert Moderator Specialist
You can use a simple aggregate Query to Sum the Quantity field, but you would have to Append that to a new Table. I am not sure how to do that on your current table without being destructive or too ambiguous for hte query to figure out.

Is this an inventory list or a sales list? If it is an inventory list, how did the multiple entries get there, rather than updating the Quantities? If it is a sales list, then it would make sense to keep the separate records to show separate sales, and then only aggregate to provide a report of sales activity.

This is an unusual procedure, but there are plenty of circumstances that I can think of that would warrants combining of rows like this to get your data set right for future operations.
Jul 9 '14 #2
Jrod2541
26 New Member
What's happening here is these are the parts(SKU) customers order, I take these parts and choose the vendor(VendrItem)that sell that part the cheapest(EachCost).

I already have the cheapest vendor in there as well as the cheapest part, I need to combine the SKUs.

The reason the SKUs are seperated in the first place is because different customers ordered them, but they're still the same parts.

I chose the cheapest vendors that will sell me those parts and now I need to combine the rows so I can tell the vendor how much of each SKU I need.





In other words, let's say I have 3 customers: A, B and C. Customer A orders 60 parts (SKU # is 123), Customer B orders 10 parts(SKU # is 445) and Customer C orders 15 parts (SKU # is 123).

There are two vendors: NIU and RWE

NIU sells the SKU # 123 parts for $1 each and sells the SKU # 445 $5 each

The vendor RWE sells the SKU # 123 parts for $2 each and sells the SKU # 445 $4 each

I choose Vendor NIU for the SKU # 123 parts because they sell them cheaper and I choose vendor RWE for SKU # 445 because they're cheaper.

Vendors have different names for their items so for SKU # 123, the vendor NIU calls it 45-NY and for vendor RWE, SKU #445 is 432

So I make a query using the info above to spit out this table:
Expand|Select|Wrap|Line Numbers
  1. SKU    VendrItem    Qty   EachCost
  2. 123    45-NY        60    $1
  3. 123    45-NY        15    $1
  4. 445    432          10    $4
  5.  
I want to give my order to the vendor, so Instead of showing them two different orders for a single part #, I want to combine them so I only give them one order instead of two.

Expand|Select|Wrap|Line Numbers
  1. SKU    VendrItem    Qty   EachCost
  2. 123    45-NY        75    $1
  3. 445    432          10    $4
  4.  

Can you give me a link on where I can learn aggregate queries?

Is there another way I should do this?
Jul 9 '14 #3
twinnyfo
3,653 Recognized Expert Moderator Specialist
If this is your case, I would not alter your tables, but still use an aggregate query.

To use an aggregate query, in the query builder, there should be a Summation notation (capital Greek letter sigma). Click on that and this will add another row to your query builder called "Total". When you add fields to your query, the default "Total" row will be "Group By". But you can change this to Sum, Count, Where, Min, Max and others.

You will want to Group By "SKU", "Vendor Item" and "Cost" and Sum "Quantity".

Hope this helps. Aggregate queries are very useful when you get the hang of using them.
Jul 9 '14 #4
Rabbit
12,516 Recognized Expert Moderator MVP
If that is your order detail table, shouldn't there be an order number of some sort? How will you know who bought what?
Jul 9 '14 #5
NeoPa
32,556 Recognized Expert Moderator MVP
Combining Rows-Opposite of Union might lead you in a viable direction. Not necessarily best for what you need but I'll let you work that out for yourself.
Jul 10 '14 #6
Jrod2541
26 New Member
Thanks everyone. I used the method twinnyfo gave me and it worked.
Jul 10 '14 #7
twinnyfo
3,653 Recognized Expert Moderator Specialist
Jrod, glad I could be of service. Lots of different ways ot skin a cat.
Jul 10 '14 #8

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

Similar topics

1
by: Tuhin Kumar | last post by:
Hi, I would like to know how to insert multiple rows into a table, using a single INSERT statement. My requirement is like this I have a table ABC which contains multiple employees entries with...
5
by: BerkshireGuy | last post by:
I was searching the threads about combing multiple rows into one and found some good stuff, but need your help to expand on it. I have a table as follows Policy Number DateOfTrans TransType...
14
by: jackiefm | last post by:
I realize the thread I am responding to was posted in January but I am basically having the same issue. I am not familiar with VBA but use Access daily. I have written simple scripts but nothing to...
2
by: Michael | last post by:
It seems that a gridview allows us to delete only a single row at a time. How to extend this functionality to select multiple rows and delete all of the selected rows in a single stroke? just like...
6
by: Jeremy Goodman | last post by:
Access 2007; Merging records containing multivalue drop down lists. I have a database showing legislation information divided by State/territory. The database needs to be able to show the info...
1
by: samuel ray | last post by:
Hi, I'm using AIX(ksh shell). > cat temp.txt "a","b",0 "c",bc",0 "a1","b1",0 "cc","cb",1
5
by: jamespfisher | last post by:
Hey all I'm writing a large extract report on clients of a pharmacy and the drugs they are prescribed (maximum of 6 drugs in total). Data structure of the drugs table looks like this: ...
7
by: almaroc | last post by:
i am using access 2007 and i am trying to combine multiple rows into one. i am using the code from this site but i continue to get this error: Compile error in query expression....
3
by: wyosorensen | last post by:
Hi, I work for an insurance company and what I'm doing is creating a database for writing policies where I can have one policy that covers multiple buildings. I have a form that they can fill out...
0
by: PreethiGowri | last post by:
I have a table something like name id date login logout arjun 1 2012-11-28 9-30 7-30 arjun 1 2012-11-29 9-30 8-00 arjun 1 2012-11-30 9-40 8-00 i want to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
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.