Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rows to columns, kind of...

Status
Not open for further replies.

gjsaturday

Technical User
Jan 18, 2005
45
US
My table looks like

tb1
id m t w th f s
1 1 0 0 1 0 0
2 0 1 0 0 1 0
3 1 0 1 0 1 0

I'm trying to get a result set to look like

id Day
1 Monday
1 Thursday
2 Tuesday
2 Friday
3 Monday
3 Wednesday
3 Friday

I'm using the following:

select id,
case WHEN m <> 0 THEN 'Monday'
when t <> 0 then 'Tuesday'
when w <> 0 then 'Wednesday'
when th <> 0 then 'Thursday'
when f <> 0 then 'Friday'
when s <> 0 then 'Saturday'
ELSE 'None' end
from tb1

my result set looks like
id Day
1 Monday
2 Tuesday
3 Monday

How can I get the other days?

Thanks in advance.
 
Try this.

Code:
select id, 'Monday' from tb1 where m = 1 union
select id, 'Tuesday' from tb1 where t = 1 union
select id, 'Wednesday' from tb1 where w = 1 union
select id, 'Thursday' from tb1 where th = 1 union
select id, 'Friday' from tb1 where f = 1 union
select id, 'Saturday' from tb1 where s = 1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top