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 |
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2007-10-11 : 08:56:24
|
| I would like to make 3x3 tableas following john | mike | wendy< 100€ | 3 0 0100-200€ | 0 2 0> 200€ | 1 0 1table: name (john, mike, wendy) money (numer)how should i do it?i tried with inner join and subquery and a case but it doesn't work.thank you |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2007-10-11 : 09:03:23
|
| You have to show your actual table structure and some sample data; I am not sure what name (john,mike,wendy) means -- are those column names or values in your table? be more specific and you'll get faster and better help.- Jeffhttp://weblogs.sqlteam.com/JeffS |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-11 : 09:05:04
|
[code]DECLARE @Sample TABLE (Name VARCHAR(20), Money INT)INSERT @SampleSELECT 'John', 50 UNION ALLSELECT 'John', 150 UNION ALLSELECT 'John', 250 UNION ALLSELECT 'Mike', 50 UNION ALLSELECT 'Wendy', 111 UNION ALLSELECT 'Wendy', 150SELECT SUBSTRING(Section, 2, 20) AS Section, SUM(CASE WHEN Name = 'John' THEN 1 ELSE 0 END) AS John, SUM(CASE WHEN Name = 'Mike' THEN 1 ELSE 0 END) AS Mike, SUM(CASE WHEN Name = 'Wendy' THEN 1 ELSE 0 END) AS WendyFROM ( SELECT CASE WHEN Money < 100 THEN 'a< 100' WHEN Money >= 200 THEN 'c>= 200' ELSE 'b100-200' END AS Section, Name FROM @Sample ) AS dGROUP BY SectionORDER BY Section[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
slimt_slimt
Aged Yak Warrior
746 Posts |
Posted - 2007-10-12 : 04:48:13
|
| i've solved it like this:select value, C1, C2, C3from(select value, SUM(CASE WHEN money between '0' and '50' then 1 else 0 END) as C1, SUM(CASE WHEN money between '51' and '100' then 1 else 0 END) as C2, SUM(CASE WHEN money between '101' and '200' then 1 else 0 END) as C3,from table1group by value) as v group by value, v.C1, v.C2, v.C3 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-10-12 : 06:05:44
|
Good for you! E 12°55'05.25"N 56°04'39.16" |
 |
|
|
|
|
|
|
|