Monday, March 26, 2012

Problems formatting my table...

I have a table that looks like this...

City State Server Type
Chicago IL Svr1 Data
Chicago IL Svr2 Data
Chicago IL Svr3 Backup
Chicago IL Svr4 Backup
Atlanta GA Svr1 Data
Atlanta GA Svr2 Data

I already have a function to convert the server rows into a comma delimited string...
ALTER function dbo.fnGetServers (@.City varchar(25), @.State varchar(25), @.Type varchar(25), @.Tree varchar(25))
returns varchar(1000)
as
begin
declare @.NewSvrCol varchar(1000)
select @.NewSvrCol = ''
select @.NewSvrCol = @.NewSvrCol + Server + ', ' from serverops.dbo.v_userviews where city = @.City and State = @.State and Type = @.Type and Tree = @.Tree
select @.NewSvrCol = left(@.NewSvrCol, len(@.NewSvrCol)-1)
return(@.NewSvrCol)
end

Any suggestions on how to display the table in the following format?

City State DataSvrs BackupSvrs
Chicago IL Svr1,Svr2 Svr3,Svr4
Atlanta GA Svr1,Svr2 NULLread the article titled Cross-Tab Reports in Books online and the read about CASE statements.|||read the article titled Cross-Tab Reports in Books online and the read about CASE statements.

Thanks a lot, I've made some progress but am still having a slight problem...
I am using this query:

ALTER VIEW v_USAtlasAuthServers
as
select distinct u.City, u.State,
CASE u.Type WHEN 'Data' then dbo.fnGetServers(City, State, Type) ELSE null END as Data_Servers,
CASE u.Type WHEN 'Backup' then dbo.fnGetServers(City, State, Type) ELSE null END as Backup_Servers
from serverops.dbo.v_userviews u

where u.tree='tree1' and u.country='united states' and u.type in ('Data Server','Backup Server')
group by u.city, u.state, u.type

which gives me the following table:

City State Data_Servers Backup_Servers
Chicago IL Svr1,Svr2 NULL
Chicago IL NULL Svr3,Svr4
Atlanta GA Svr1,Svr2 NULL

Which is not right as I want Chicago, IL to be in one row...any idea on how to fix this?sql

No comments:

Post a Comment