dbTalk Databases Forums  

SQL help

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss SQL help in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Phil Hunt
 
Posts: n/a

Default SQL help - 09-30-2011 , 02:01 PM






Table2 ID Status
789 4
789 4
1374

1374

1374

1940 15
1940 15
1940 15


I want to Select the above to output the followng

789 0
1374 0
1940 1 (because it has Status of 15)


I have been staring at this so long, my head and eye hurts. Can someone help
?

Thanks

Reply With Quote
  #2  
Old   
Hugo Kornelis
 
Posts: n/a

Default Re: SQL help - 09-30-2011 , 02:34 PM






Hi Phil,

SELECT DISTINCT ID, CASE WHEN Status = 15 THEN 1 ELSE 0 END
FROM YourTable;
--
Hugo Kornelis, SQL Server MVP
My SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis

Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: SQL help - 09-30-2011 , 02:39 PM



On 2011-09-30 21:01, Phil Hunt wrote:
Quote:
Table2 ID Status
789 4
789 4
1374

1374

1374

1940 15
1940 15
1940 15


I want to Select the above to output the followng

789 0
1374 0
1940 1 (because it has Status of 15)


I have been staring at this so long, my head and eye hurts. Can someone help
?

I assume there is a good reason for the design of this table. Something
like:

select id, max(case when status = 15 then 1 else 0 end)
from t2
group by id

will do.


/Lennart

Reply With Quote
  #4  
Old   
Phil Hunt
 
Posts: n/a

Default Re: SQL help - 09-30-2011 , 02:43 PM



Thanks guys.
It is not the table. I am showing the tuples after a whole bunch of joins.
I should be able to work this into my scheme.


"Lennart Jonsson" <erik.lennart.jonsson (AT) gmail (DOT) com> wrote

Quote:
On 2011-09-30 21:01, Phil Hunt wrote:
Table2 ID Status
789 4
789 4
1374

1374

1374

1940 15
1940 15
1940 15


I want to Select the above to output the followng

789 0
1374 0
1940 1 (because it has Status of 15)


I have been staring at this so long, my head and eye hurts. Can someone
help
?


I assume there is a good reason for the design of this table. Something
like:

select id, max(case when status = 15 then 1 else 0 end)
from t2
group by id

will do.


/Lennart

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.