| Author |
Topic  |
|
|
AskSQLTeam
Ask SQLTeam Question
USA
0 Posts |
Posted - 05/07/2002 : 11:05:56
|
| Ed writes "I continue to find needs for summing strings in SQL. I am basically trying to "Flatten" one-to-manys. For example, if you have a table with 2 columns, 20 rows. Lets say the first column contains the days of the week, monday, tues etc. randomly. Lets say the second column contains names, Joe, Fred, Bill etc. What I want to be able to do is say "select sum(name) from mytable where day='Monday'" and return one row that contains the two columns, one of "monday" and one with "Joe, Fred and John" in it if those happened to be the names that correspnded to those days. I know you can't "sum" strings, but it would make my life a lot simpler. Any Ideas?" |
|
|
YellowBug
Aged Yak Warrior
United Kingdom
616 Posts |
|
|
Nazim
A custom title
United Arab Emirates
1408 Posts |
|
|
jrjohnson@infoed.org
Starting Member
1 Posts |
Posted - 05/09/2002 : 07:31:57
|
quote:
Have a look at this code:
http://vyaskn.tripod.com/code/cat.txt
It should work with slight modifications.
Thanks, this is nice, but I was looking for something I could use in an in-line SQL statement, like "select last_name,first_name, concatenate(select all books from pubs) where last_name <> 'Jones'" where the "concatenate" function does not exisit. The results would hoepfully look like..... Smith Fred book1, book2, book3 Johnson Ed booka, bookb Murphy Paul bookc, booka,book1,book3
etc. I know I can do it with a "program" or stored procedure, but in my application it needs to be much more "dynamic". In Oracle I can write functions, and use them in SQL statements. Thanks!
|
 |
|
|
robvolk
Most Valuable Yak
USA
15559 Posts |
Posted - 05/09/2002 : 08:30:59
|
quote: In Oracle I can write functions, and use them in SQL statements.
You can write functions in SQL Server 2000, and also use them in SQL statements. It's not a feature that's limited to Oracle. I hate to gripe (yeah, I know, you don't believe that!) but if Oracle does what you want, maybe you should just use Oracle instead and not complain about SQL Server.
This link might help too:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=9978
But it's just a variation on Rob's link that was posted earlier:
http://www.sqlteam.com/Forums/topic.asp?TOPIC_ID=14095
The point is, they all work in SQL Server, and they do what you want done. You could even modify them to work as functions.
|
 |
|
|
Teroman
Posting Yak Master
United Kingdom
115 Posts |
Posted - 05/09/2002 : 08:56:43
|
you can do it for one name like this
declare @foo char(1000) select @foo = isnull(rtrim(@foo)+',', '') + rtrim(BookName) from Books where name = 'Smith' select @foo
dont know how much help that'll be
col
|
 |
|
| |
Topic  |
|
|
|