Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

sql query - show output in one row if possible

Posted on 2011-03-08
9
Medium Priority
?
257 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
Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

 
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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.
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…

618 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