Solved

sql query - show output in one row if possible

Posted on 2011-03-08
9
254 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
Industry Leaders: 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!

 
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

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…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

737 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