Solved

Update with GROUP BY

Posted on 2008-10-06
6
4,392 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 143

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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
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 143

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

738 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