Solved

Update with GROUP BY

Posted on 2008-10-06
6
4,370 Views
Last Modified: 2008-10-06
SQL Tables:
Item ( ItemID int )
ItemTransactionHistory ( ItemTransactionHistoryID int, ItemID_fk int, TransactionDate DateTime, Amount float )

ItemID_fk is a foreign key to the Item table, and there will be multiple records with the same ItemID_fk. In the ItemTransactionHistory, TransactionDate is a "short" date, meaning it only contains "10/06/2008" and no time. There can be multiple transactions on the same day (the time is ignored so it's always midnight). There will be multiple transactions on the same day, for different amounts.

I need to summarize all amounts for that particular day.

The easiest method for me to do this is to simply SET all Amount values for a particular day to be the SUM of the days transactions, then I will delete the extra records.

UPDATE ItemTransactionHistory
SET Amount = SUM(Amount)
GROUP BY ItemID, TransactionDate

This is terribly wrong but it hopefully expresses what I am looking for. I need the update to be GROUPed by ItemID and TransactionDate

Sample Data:

ItemTransactionHistoryID  /  ItemID_fk   /  TransactionDate  /   Amount float
10001 / 111 / 10-01-2008 / 2.00
10002 / 111 / 10-05-2008 / 2.00
10003 / 111 / 10-05-2008 / 3.00
10004 / 121 / 10-01-2008 / 3.00
10005 / 121 / 10-04-2008 / 4.25
10006 / 121 / 10-04-2008 / 5.00
10007 / 121 / 10-04-2008 / 7.25
10008 / 121 / 10-06-2008 / 6.00
10009 / 121 / 10-12-2008 / 9.00

The expected UPDATEd result set should look like this:
10001 / 111 / 10-01-2008 / 2.00
10002 / 111 / 10-05-2008 / 5.00
10003 / 111 / 10-05-2008 / 5.00
10004 / 121 / 10-01-2008 / 3.00
10005 / 121 / 10-04-2008 / 16.50
10006 / 121 / 10-04-2008 / 16.50
10007 / 121 / 10-04-2008 / 16.50
10008 / 121 / 10-06-2008 / 6.00
10009 / 121 / 10-12-2008 / 9.00


0
Comment
Question by:SubtleTechnology
  • 2
  • 2
  • 2
6 Comments
 
LVL 142

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 22647774
2 things:
* Amount float  << this is a bad choice. use DECIMAL(20,4) instead... float is unprecise data type

* I seriously doubt that you want to update/summarize the existing amount into several rows, as that will make all the data "wrong", at the end?

I think what you want is doing this SELECT:

SELECT ItemID_fk ,  TransactionDate  , sum(amount)
  FROM ItemTransactionHistory
 GROUP BY ItemID_fk ,  TransactionDate  

Open in new window

0
 

Author Comment

by:SubtleTechnology
ID: 22647815
It does make the data wrong, but I'm going to delete all but one row.. this is simpler than trying to insert a new record etc.
0
 

Author Comment

by:SubtleTechnology
ID: 22647952
I already got the select answer in a previous post.. I need to update now. Yes what i'm asking sounds wrong, but it is correct for what I need done.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 22647985
UPDATE ItemTransactionHistory
SET Amount =  ( SELECT sum(amount)  FROM ItemTransactionHistory R  WHere  R.ItemID_fk=ItemID_fk and  R.TransactionDate =TransactionDate GROUP BY ItemID_fk ,  TransactionDate  )
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 100 total points
ID: 22648038
>It does make the data wrong, but I'm going to delete all but one row.. this is simpler than trying to insert a new record etc.

you should make a temp table, then?

anyhow, remove the group by from pratima_mcs' suggestion
UPDATE ItemTransactionHistory

SET Amount =  ( SELECT sum(amount)  FROM ItemTransactionHistory R  WHere  R.ItemID_fk=ItemID_fk and  R.TransactionDate =TransactionDate  )

Open in new window

0
 
LVL 39

Accepted Solution

by:
Pratima Pharande earned 400 total points
ID: 22648063
UPDATE ItemTransactionHistory
SET Amount =  ( SELECT sum(amount)  FROM ItemTransactionHistory R  WHere  R.ItemID_fk=ItemTransactionHistory.ItemID_fk and  R.TransactionDate =ItemTransactionHistory.TransactionDate GROUP BY R.ItemID_fk ,  R.TransactionDate  )
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now