Solved

sql query - show output in one row if possible

Posted on 2011-03-08
9
249 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
  • 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
 
LVL 7

Expert Comment

by:lundnak
ID: 35071354
What is the compatibility level for your database?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now