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 |
|
Mr Butter
Starting Member
3 Posts |
Posted - 2011-07-16 : 09:58:06
|
| I'm trying to get the following to work without a subquery. The results I am getting are separate rows. Any help will greatly be appreciated. Using t-sql.id phone# Type1 223 Mbl1 224 Hom2 333 Hom2 null Mblundesired resultId Mobile Home1 223 1 2242 null null2 333Desired resultid Mobile Home1 223 2242 null 333select ID,(case type when 'hom' then phone# end) as Home,(case type when 'mbl' then phone# end) as Mobilefrom table |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-07-16 : 10:21:53
|
[code]select ID, max(case type when 'hom' then phone# end) as Home, max(case type when 'mbl' then phone# end) as Mobilefrom tablegroup by ID[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
jcelko
Esteemed SQL Purist
547 Posts |
Posted - 2011-07-16 : 10:54:08
|
| People cannot read your mind, so post your code and clear specs if you really want help. Please post real DDL and not narrative or your own personal programming language. Learn to use ISO-11179 rules for the data element names, avoid needless dialect and use ISO-8601 temporal formats, codes and so forth. Please tell us what SQL product and release you are using. Tell us if you can change the DDL or if you are stuck with it. There is no such thing as a magical generic “id”, we never, never use anything but Latin-1 alphas, digits and underscore in data element names, etc. >> I am trying to get the following to work without a subquery. <<Why do you care HOW something is done and NOT what is done? Let's do your job for you, starting with a skeleton DDLCREATE TABLE Employee_Telephones(emp_id INTEGER NOT NULL, phone_type CHAR(3) NOT NULL CHECK (phone_type IN ('HOM', 'MBL')), PRIMARY KEY (emp_id, phone_type), phone_nbr CHAR(18) NOT NULL, --- international standards);SELECT emp_id, MAX(CASE phone_type WHEN 'HOM' THEN phone_nbr ELSE NULL END) AS home_phone_nbr, MAX (CASE phone_type WHEN 'MBL' THEN phone_nbr ELSE NULL END) AS mobile__phone_nbr, FROM Employee_Telephones GROUP BY emp_id;--CELKO--Books in Celko Series for Morgan-Kaufmann PublishingAnalytics and OLAP in SQLData and Databases: Concepts in Practice Data, Measurements and Standards in SQLSQL for SmartiesSQL Programming Style SQL Puzzles and Answers Thinking in SetsTrees and Hierarchies in SQL |
 |
|
|
russell
Pyro-ma-ni-yak
5072 Posts |
Posted - 2011-07-19 : 00:15:32
|
| Seems KH understood the problem just fine. Maybe he's a mind reader.Or maybe, he'd rather help people instead of rag on them as you do.Tell ya what Joe, you stop bashing newbies, and I'll stop checking all your posts for the unmistakable smell of doo-doo.Because every time I smell it, I'm going to point it out. Oops, think I just stepped in some. |
 |
|
|
|
|
|
|
|