Solved

Update with GROUP BY

Posted on 2008-10-06
6
4,389 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 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
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

 
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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

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…
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 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