Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
I have the following table:Table 1Year---Plant---TRUCK---Quantity2013---ABC-----100-----42013---ABC-----101-----52013---XYZ-----100-----32013---XYZ-----101-----7I need a select that will show:Year---Plant---TRUCK---Quantity2013---ABC-----100-----92013---XYZ-----100-----10Of course I have more than 4 lines in my live table.Any help is deeply appreciated.
djj55
Constraint Violating Yak Guru
352 Posts
Posted - 2013-11-26 : 15:13:09
Here is something to look at:
SELECT Year, Plant, MIN(Truck) AS Truck, SUM(Quantity) AS QuantityFROM Table1 GROUP BY Year, Plant
djj
matrixrep
Starting Member
30 Posts
Posted - 2013-11-26 : 15:38:28
I change the data to represent what i am looking for:Table 1Year---Plant---TRUCK---Quantity2013---ABC-----100-----42013---ABC-----101-----52013---ABC-----102-----52013---XYZ-----100-----52013---XYZ-----101-----32013---XYZ-----102-----7I need a select that will show:Year---Plant---TRUCK---Quantity2013---ABC-----100-----92013---ABC-----101-----52013---XYZ-----100-----122013---XYZ-----101-----3I retain the truck 100 and merge the quantity of truck 102 for each plant.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2013-11-27 : 05:25:59
[code]SELECT Year,Plant,CASE WHEN TRUCK = 102 THEN 100 ELSE TRUCK END AS TRUCK,SUM(Quantity) AS QuantityFROM TableGROUP BY Year,Plant,CASE WHEN TRUCK = 102 THEN 100 ELSE TRUCK END[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
matrixrep
Starting Member
30 Posts
Posted - 2013-11-27 : 09:21:41
Thank you visakh16This is exactly what i was looking for.
visakh16
Very Important crosS Applying yaK Herder
52326 Posts
Posted - 2013-11-27 : 10:41:16
you're welcome------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs