Solved

Update with GROUP BY

Posted on 2008-10-06
6
4,397 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

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…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function

718 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