| Author |
Topic |
|
ab
Starting Member
1 Post |
Posted - 2008-02-11 : 00:35:52
|
| hi,i have a table as follows:location1 location2 Accidenta b 3b a 5c d 3b c 4c b 5a b 2I want to get the following output:location1 location2 Accidenta b 10c d 3b c 9I am not being able to write the correct sql query for this. Can somebody please help?thanks,A |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-02-11 : 00:54:18
|
quote: mysql query
Is it MYSQL or Microsoft SQL Server ?This is a MS SQL Server forum KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2008-02-11 : 01:24:24
|
| i don't understand the output needed. am I being dense?-ec |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-02-11 : 01:32:18
|
[code]a b 10 froma b 3b a 5a b 2b c 9fromb c 4c b 5[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2008-02-11 : 01:36:46
|
yes, i was being dense -ec |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-02-11 : 01:45:15
|
i understand what OP wants but have not figured out how to do it yet  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2008-02-11 : 13:58:33
|
how about this?CREATE TABLE [test] (col1 CHAR(1), col2 CHAR(1), col3 INT)INSERT INTO [test] VALUES ('a', 'b', '3')INSERT INTO [test] VALUES ('b', 'a', '5')INSERT INTO [test] VALUES ('c', 'd', '3')INSERT INTO [test] VALUES ('b', 'c', '4')INSERT INTO [test] VALUES ('c', 'b', '5')INSERT INTO [test] VALUES ('a', 'b', '2')-- DROP TABLE [test]SELECT 'Location' = CASE WHEN ASCII(col1) + ASCII(col2) = 195 THEN 'ab' WHEN ASCII(col1) + ASCII(col2) = 197 THEN 'bc' WHEN ASCII(col1) + ASCII(col2) = 199 THEN 'cd' ELSE 'NA' END, SUM(col3) AS TotalFROM testGROUP BY ASCII(col1) + ASCII(col2)ORDER BY Location ASCdon't know if this works in mySQL, but it works fine in MSSQL.-ec |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-02-11 : 14:35:00
|
create table [#test] (col1 char(1), col2 char(1), col3 int)insert into [#test] values ('a', 'b', '3')insert into [#test] values ('b', 'a', '5')insert into [#test] values ('c', 'd', '3')insert into [#test] values ('b', 'c', '4')insert into [#test] values ('c', 'b', '5')insert into [#test] values ('a', 'b', '2')select col1, col2, col3 =sum(col3)from ( select col1 = case when col1 < col2 then col1 else col2 end, col2 = case when col1 < col2 then col2 else col1 end, col3 from #test ) agroup by col1, col2order by col1, col2drop table #testResults:col1 col2 col3 ---- ---- ----------- a b 10b c 9c d 3(3 row(s) affected)CODO ERGO SUM |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-11 : 15:00:07
|
UNION ALL?SELECT Location1, Location2, SUM(Accident) AS AccidentFROM ( SELECT Location1, Location2, Accident FROM Table1UNION ALLSELECT Location2, Location1, Accident FROM Table1) AS xGROUP BY Location1, Location2 E 12°55'05.25"N 56°04'39.16" |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2008-02-11 : 15:16:06
|
quote: Originally posted by Peso UNION ALL?SELECT Location1, Location2, SUM(Accident) AS AccidentFROM ( SELECT Location1, Location2, Accident FROM Table1UNION ALLSELECT Location2, Location1, Accident FROM Table1) AS xGROUP BY Location1, Location2 E 12°55'05.25"N 56°04'39.16"
That would work if you added this line:where Location1 < Location2 Otherwise, you would get this:Location1 Location2 Accident --------- --------- ----------- b a 10a b 10c b 9b c 9d c 3c d 3 CODO ERGO SUM |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-11 : 15:19:24
|
You're right!Change SUM to SUM() / 2?You're approach is more efficient. Only on SCAN/SEEK. My has two. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
eyechart
Master Smack Fu Yak Hacker
3575 Posts |
Posted - 2008-02-11 : 15:43:41
|
| i like my solution because of it's ridiculousness and lack of scalability. Even if this was originally a homework question by the OP, i think I am the one who learned something from MVJ and Peso. thx guys.-ec |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-02-11 : 20:54:44
|
quote: Originally posted by eyechart i like my solution because of it's ridiculousness and lack of scalability. Even if this was originally a homework question by the OP, i think I am the one who learned something from MVJ and Peso. thx guys.-ec
At least you have a solution, I have to submit blank for the homework But now i can copy from MVJ and Peso's solutions and got full marks for it  KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|