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 |
|
turtle1353
Starting Member
9 Posts |
Posted - 2005-12-13 : 17:45:25
|
Hi SQL gurus!Some help would be greatly appreciated with this select statement. How how to get column_Category so cost_xxx will output as shown?Table 1column_Item column_Category1 Toys2 Shoes3 BooksTable 2column_Item column_Cost column_Ordered column_Customer1 1.00 2 John Doe2 1.00 1 John Doe3 1.50 2 John DoeNeeded this Output:Customer Cost_Toys Cost_Shoes Cost_BooksJohn Doe 2.00 1.00 3.00SELECT customer, SUM(cost * ordered) AS Cost_Toys, SUM(cost * ordered) AS Cost_Shoes, SUM(cost * ordered) AS Cost_BooksFROM table1, table2WHERE customer = 'John Doe' GROUP BY customer Thanks a lot! |
|
|
rockmoose
SQL Natt Alfen
3279 Posts |
Posted - 2005-12-13 : 17:50:54
|
| Hi turtle!Welcome to SqlTeam.SELECT customer, SUM(cost * ordered) AS Cost_Toys, SUM(cost * ordered) AS Cost_Shoes, SUM(cost * ordered) AS Cost_BooksFROM table1 INNER JOIN table2 ON table1.column_Item = table2.column_ItemWHERE customer = 'John Doe' GROUP BY customerrockmoose |
 |
|
|
turtle1353
Starting Member
9 Posts |
Posted - 2005-12-13 : 18:44:23
|
| Hi rockmoose,Thanks for that answering so quick! I almost got it but when I run it, the total for cost_xxx come out the same (7.00). I'm trying to get it so that they come out sorted based on column_category. That's where I get stuck. :( |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-12-13 : 20:28:02
|
use order bySELECT customer, SUM(cost * ordered) AS Cost_Toys, SUM(cost * ordered) AS Cost_Shoes, SUM(cost * ordered) AS Cost_BooksFROM table1 INNER JOIN table2 ON table1.column_Item = table2.column_ItemWHERE customer = 'John Doe' GROUP BY customerorder by table1.column_category -----------------[KH]Learn something new everyday |
 |
|
|
turtle1353
Starting Member
9 Posts |
Posted - 2005-12-13 : 22:37:14
|
Hi khtanI tried to use ORDER BY but when I use I get the error:"Column name 'Table1.category' is invalid in the ORDER BY clause because it is not contained in either an aggreagte function or the GROUP BY clause."When I try to add table1.category like this:SELECT Table2.customer, Table1.category, SUM(Table2.cost * Table2.ordered) AS Cost_Toys, SUM(Table2.cost * Table2.ordered) AS cost_shoes, SUM(Table2.cost * Table2.ordered) AS cost_booksFROM Table1 INNER JOIN Table2 ON Table1.item = Table2.itemWHERE (Table2.customer = 'John Doe')GROUP BY Table2.customer, Table1.categoryORDER BY Table1.categoryI'll get this output:customer category cost_toys cost_shoes cost_booksJohn Doe Books 4 4 4John Doe Shoes 1 1 1John Doe Toys 2 2 2What I'm trying to get is this:customer cost_toys cost_shoes cost_booksJohn Doe 2 1 4 Getting close!Thanks khtan! |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2005-12-13 : 23:45:42
|
Sorry.. did not read your requirement carefully.select customer, sum(Toys) as Cost_Toys, sum(Shoes) as Cost_Shoes, sum(Books) as Cost_Booksfrom( select t2.customer, case when t1.category = 'Toys' then sum(cost * ordered) else 0 end as [Toys], case when t1.category = 'Shoes' then sum(cost * ordered) else 0 end as [Shoes], case when t1.category = 'Books' then sum(cost * ordered) else 0 end as [Books] from Table1 t1 inner join Table2 t2 on t1.item = t2.item group by t2.customer, t1.category) as tgroup by customer Also refer here for crosstab [url]http://www.sqlteam.com/item.asp?ItemID=2955[/url]-----------------[KH]Learn something new everyday |
 |
|
|
turtle1353
Starting Member
9 Posts |
Posted - 2005-12-14 : 00:36:53
|
| Wow! Thanks a lot khtan! |
 |
|
|
|
|
|
|
|