Solved

sql query - show output in one row if possible

Posted on 2011-03-08
9
255 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
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
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 500 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

Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

Question has a verified solution.

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

INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

728 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