?
Solved

Update with GROUP BY

Posted on 2008-10-06
6
Medium Priority
?
4,400 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
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 
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 400 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 1600 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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
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
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…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

770 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