?
Solved

sql query - show output in one row if possible

Posted on 2011-03-08
9
Medium Priority
?
256 Views
Last Modified: 2012-05-11
hi experts I have attached my code and my output which shows how many hours a particular room is used for each day of the week

As you can see in the spreadsheet there are alot of null values

Is there anyway of putting this into one line as shown

Thanks
select distinct room_code, room_desc, room_location_code, room_location, --day,
(case day_of_week when '1' then sum(total_hours) end) as 'Mon',
(case day_of_week when '2' then sum(total_hours) end) as 'Tue',
(case day_of_week when '3' then sum(total_hours) end) as 'Wed',
(case day_of_week when '4' then sum(total_hours) end) as 'Thu',
(case day_of_week when '5' then sum(total_hours) end) as 'Fri',
(case day_of_week when '6' then sum(total_hours) end) as 'Sat',
(case day_of_week when '7' then sum(total_hours) end) as 'Sun',
(case day_of_week when 'a' then sum(total_hours) end )as 'Mon-Fri'

   from nicisreports..nrc_RoomUsage
where acad_period like '10/11'
and room_location_code in ('na')
and room_code like 'eb02'
and day not in ('sat-Sun', 'mon-sun')
--and day_of_week like '3'
group by room_code, room_desc, room_location_code, room_location, day_of_week
order by room_code, room_desc, room_location_code, room_location

Open in new window

room-location.xls
0
Comment
Question by:lisa_mc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 7

Expert Comment

by:lundnak
ID: 35070499
In SQL 2005 you could using the PIVOT clause.
http://msdn.microsoft.com/en-us/library/ms177410.aspx
0
 
LVL 7

Expert Comment

by:lundnak
ID: 35070712
Something like this.

select Room_Code, Room_Desc, Room_Location_Code, Room_Location, [Mon], [Tue], [Wed], [Thu], [Fri], [Sat], [Sun], [Mon-Fri]
From (Select room_code, room_desc, room_location_code, room_location,
		     Case day_of_week when '1' then 'Mon'
							  When '2' then 'Tue'
							  When '3' then 'Wed'
							  When '4' then 'Thu'
							  When '5' then 'Fri'
							  When '6' then 'Sat'
							  When '7' then 'Sun'
							  When 'a' then 'Mon-Fri'
							  else 'other' end day_of_week,
		     total_hours
      From nicisreports..nrc_RoomUsage
	  where acad_period like '10/11'
	    and room_location_code in ('na')
		and room_code like 'eb02') p
PIVOT (Sum(total_hours)
	   For day_of_week in ([Mon], [Tue], [Wed], [Thu], [Fri], [Sat], [Sun], [Mon-Fri])) as pvt

Open in new window

0
 
LVL 3

Author Comment

by:lisa_mc
ID: 35070750
hiya

Im gettin this error

Msg 325, Level 15, State 1, Line 69
Incorrect syntax near 'PIVOT'. You may need to set the compatibility level of the current database to a higher value to enable this feature. See help for the stored procedure sp_dbcmptlevel.
0
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
LVL 7

Expert Comment

by:lundnak
ID: 35071354
What is the compatibility level for your database?
0
 
LVL 11

Expert Comment

by:yuching
ID: 35079825
u can use query below
select  room_code, room_desc, room_location_code, room_location, --day,
Sum(case day_of_week when '1' then total_hours Else 0 end) as 'Mon',
Sum(case day_of_week when '2' then total_hours Else 0 end) as 'Tue',
Sum(case day_of_week when '3' then total_hours Else 0 End) as 'Wed',
Sum(case day_of_week when '4' then total_hours Else 0 end) as 'Thu',
Sum(case day_of_week when '5' then total_hours Else 0 End) as 'Fri',
Sum(case day_of_week when '6' then total_hours Else 0 end) as 'Sat',
Sum(case day_of_week when '7' then total_hours Else 0 end) as 'Sun',
Sum(case day_of_week when 'a' then total_hours Else 0 end )as 'Mon-Fri'
from nicisreports..nrc_RoomUsage
where acad_period like '10/11'
and room_location_code in ('na')
and room_code like 'eb02'
and day not in ('sat-Sun', 'mon-sun')
--and day_of_week like '3'
group by room_code, room_desc, room_location_code, room_location, day_of_week
order by room_code, room_desc, room_location_code, room_location

Open in new window

0
 
LVL 3

Author Comment

by:lisa_mc
ID: 35080962
hi lundak when I check the compatibility is 80

yuching - when I run that code I am getting all the null values except now the first value is shown under mon, second value shown under tues and so on
0
 
LVL 11

Accepted Solution

by:
yuching earned 2000 total points
ID: 35081534
oh should take out the group by for days _of_week as below
select  room_code, room_desc, room_location_code, room_location, --day,
Sum(case when day_of_week ='1' then total_hours Else 0 end) as 'Mon',
Sum(case when day_of_week = '2' then total_hours Else 0 end) as 'Tue',
Sum(case when day_of_week ='3' then total_hours Else 0 End) as 'Wed',
Sum(case when day_of_week = '4' then total_hours Else 0 end) as 'Thu',
Sum(case when day_of_week = '5' then total_hours Else 0 End) as 'Fri',
Sum(case when day_of_week = '6' then total_hours Else 0 end) as 'Sat',
Sum(case when day_of_week = '7' then total_hours Else 0 end) as 'Sun',
Sum(case when day_of_week = 'a' then total_hours Else 0 end )as 'Mon-Fri'
from nicisreports..nrc_RoomUsage
where acad_period like '10/11'
and room_location_code in ('na')
and room_code like 'eb02'
and day not in ('sat-Sun', 'mon-sun')
--and day_of_week like '3'
group by room_code, room_desc, room_location_code, room_location
order by room_code, room_desc, room_location_code, room_location

Open in new window

0
 
LVL 3

Author Closing Comment

by:lisa_mc
ID: 35081610
Great job that worked

thank you so much
0
 
LVL 7

Expert Comment

by:lundnak
ID: 35082767
FYI.  The compatibility for your database needs to 90 or greater for pivot tables to work.
0

Featured Post

Get MongoDB database support online, now!

At Percona’s web store you can order your MongoDB database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card. Handle your MongoDB database support now!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question