We help IT Professionals succeed at work.

SQL Query to combine grouped records

kstahl
kstahl asked
on
225 Views
Last Modified: 2010-03-20
This should be easy, but I'm up against the clock and having an issue.

Here's a sample of my data in one SQL table.

PartNumber, PartRev, Bin, Location, Lot, Qty
P-720, B, PAL8202,SH,F100,7
P-720, B, PAL8202,SH,F100,2
P-820, D, PAL8201,SH,F102,8
P-820, D, PAL8201,SH,F102,10
P-820, D, PAL8201,SH,G100, 4

I need to group records where the PartNumber, Rev, Bin, Location, and Lot match and then total the qty.  I need an update query to set the new total and remove the extra records.

The query should result in
P-720, B, PAL8202,SH,F100,9
P-820, D, PAL8201,SH,F102,18
P-820, D, PAL8201,SH,G100, 4

The extra records should be deleted from my table.  Thank you!
Comment
Watch Question

Jim HornSQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015

Commented:
SELECT PartNumber, Rev, Bin, Location, Lot, Sum(qty) as qty_sum
FROM YourTableName
GROUP BY PartNumber, Rev, Bin, Location, Lot
ORDER BY PartNumber, Rev, Bin, Location, Lot

>I need an update query to set the new total and remove the extra records.
So... you want to delete the records in this table, and populate it with the summed records, all in one SQL?  

Author

Commented:
>So... you want to delete the records in this table, and populate it with the summed records, all in one SQL?  

Yes, I need to replace the duplicates with one record summed if possible.
SQL Server Data Dude
CERTIFIED EXPERT
Most Valuable Expert 2013
Author of the Year 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.