Go Back   Rhinocerus > Newsgroup > Newsgroup comp.databases.* > Newsgroup comp.databases.ms-sqlserver

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 09-30-2011, 07:01 PM
Phil Hunt
Guest
 
Posts: n/a
Default SQL help

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
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 09-30-2011, 07:34 PM
Hugo Kornelis
Guest
 
Posts: n/a
Default Re: SQL help

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 (permalink)  
Old 09-30-2011, 07:39 PM
Lennart Jonsson
Guest
 
Posts: n/a
Default Re: SQL help

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
  #4 (permalink)  
Old 09-30-2011, 07:43 PM
Phil Hunt
Guest
 
Posts: n/a
Default Re: SQL help

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@gmail.com> wrote in message
news:j655ug$uv3$1@dont-email.me...
> 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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 11:52 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.