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 |
|
eazyc10
Starting Member
1 Post |
Posted - 2005-12-16 : 14:06:06
|
| Hi,I have a SQL query question.I am trying to take data from a table, and sum of the qty's for each unique item. It would look something like this:item qtyA 2A 3B 1C 2B 4A 2C 1B 1And I would like to query this and put it into a new table as follows:item qtyA 7B 6C 3In essence summing the qty for each unique item.Is this possible through just plan SQL or do I need to look into some more programming?Thanks |
|
|
Srinika
Master Smack Fu Yak Hacker
1378 Posts |
Posted - 2005-12-16 : 14:39:59
|
| Insert Into NewTableSelect Item, sum(qty) from OldTbl group by ItemAdvice: Read about aggregate functions, group by ... |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2005-12-16 : 14:56:48
|
| [code]USE NorthwindGOSET NOCOUNT ONCREATE TABLE myTable99(item char(1), qty int)GOINSERT INTO myTable99(item, qty)SELECT 'A', 2 UNION ALLSELECT 'A', 3 UNION ALLSELECT 'B', 1 UNION ALLSELECT 'C', 2 UNION ALLSELECT 'B', 4 UNION ALLSELECT 'A', 2 UNION ALLSELECT 'C', 1 UNION ALLSELECT 'B', 1GOSELECT * FROM myTable99GOSELECT item, SUM(qty) AS SUM_qty INTO myNewTable99 FROM myTable99 GROUP BY itemGOSELECT * FROM myNewTable99GODROP TABLE myTable99, myNewTable99GO[/code]Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxAdd yourself!http://www.frappr.com/sqlteam |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-19 : 01:16:05
|
| Why is myNewTable99 needed?MadhivananFailing to plan is Planning to fail |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-12-19 : 01:42:28
|
quote: Originally posted by madhivanan Why is myNewTable99 needed?MadhivananFailing to plan is Planning to fail
quote: And I would like to query this and put it into a new table
-----------------[KH]Learn something new everyday |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2005-12-19 : 02:10:58
|
Well. I didnt read question fully >>I would like to query this and put it into a new table as follows:Why do you need seperate table?You can just use Select StatementMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|