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.
| Author |
Topic |
|
collie
Constraint Violating Yak Guru
400 Posts |
Posted - 2010-03-02 : 08:05:10
|
| Hi,I have a table with the following:Item Carton Price Key Country12 0012 23 6 TH12 0012 12 7 TH13 2309 18 7 uk14 0099 1 9 US14 0099 1 9 FRWhat i need to do is find rows that have same item and carton but different keys and same country. In that case i have to sum the price.In this example the first 2 rows will sum up to 35.How can i do this query?ThanksWhisky-my beloved dog who died suddenly on the 29/06/06-I miss u so much. |
|
|
ms65g
Constraint Violating Yak Guru
497 Posts |
Posted - 2010-03-02 : 08:09:28
|
Do you mean this:SELECT Item, Carton, SUM(Price), CountryFROM table_nameGROUP BY Item, Carton, CountryHAVING MIN(Key) < MAX(Key) |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 08:09:28
|
| select item,carton,coutry,sum(price) as price from your_tablegroup by item,carton,coutryhaving count(*)>1MadhivananFailing to plan is Planning to fail |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2010-03-02 : 08:10:03
|
| select item, carton,country,sum(price)from tgroup byitem, carton,country???? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2010-03-02 : 08:10:41
|
MadhivananFailing to plan is Planning to fail |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2010-03-02 : 08:39:24
|
quote: Originally posted by LoztInSpace select item, carton,country,sum(price)from tgroup byitem, carton,country????
this will just return sum of all possible groups of item, carton,country. you need to put a condition to ensure you return only rows that have same item and carton but different keys and same country as done by other posters------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|
|
|
|
|