VSmoL
VSmoL

Reputation: 47

SQL Select two rows with one different true value

my problem is that I have database which saves data like this way. Example you have posted something, but there are to types of posts. Other one is Weekly post and other one is Daily post. On my database I have column which tells me is it Weekly or Daily post.

id | senderID | message | weekly | daily
1       5       "hello"     1        0
2       5      "Fun joke"   0        1

So how I am able to select those two rows to one row showing if they have done weekly and daily message the row should look like this

senderID | message | weekly | daily
   5       "hello"     1        1

But if sender havent done daily message it shows

senderID | message | weekly | daily
   5       "hello"     1        0

Upvotes: 0

Views: 50

Answers (1)

Andy-Delosdos
Andy-Delosdos

Reputation: 3720

try this:

select 
    senderid, 
    case when sum(weekly) > 0 then 1 else 0 end [weekly], 
    case when sum(daily) > 0 then 1 else 0 end [daily]
from TABLENAME
group by senderid

Replace TABLENAME with the actual table name :)

Upvotes: 1

Related Questions