English 中文(简体)
总表
原标题:Matrix display from a SQL Server 2008 table

Does anyone know how to create a matrix display from a database table?
I m using ASP.NET C# and the database is SQL Server 2008.

表格就这样看。

enter image description here

我希望矩阵像这样。

“entergraph

问题回答

使用TSQL pivot

create table table1
(
    serverName varchar(30),
    app varchar(50)
);
go

insert table1 (serverName , app) values ( server1  ,  app A );
insert table1 (serverName , app) values ( server2  ,  app A );
insert table1 (serverName , app) values ( server2  ,  app B );
insert table1 (serverName , app) values ( server3  ,  app B );
insert table1 (serverName , app) values ( server1  ,  app C );
insert table1 (serverName , app) values ( server3  ,  app C );
go

create procedure GetPivotTable
as begin
DECLARE @PivotColumnHeaders VARCHAR(MAX)
SELECT @PivotColumnHeaders = 
  COALESCE(@PivotColumnHeaders +  ,[  + cast(t.serverName as varchar) +  ]  ,
   [  + cast(t.serverName as varchar)+  ] )
FROM (select distinct serverName from table1) t


DECLARE @PivotTableSQL NVARCHAR(MAX)
SET @PivotTableSQL = N 
select * from
(select  app, serverName from table1)  sourceTable
pivot
(
    count(serverName) for serverName in (  + @PivotColumnHeaders +  )
) pivottable
 
EXECUTE(@PivotTableSQL)
end

go

exec GetPivotTable

My suggestion here would be to pull the data out in exactly the format you have it, into a List of entities (at its simplest):

public class ServerApplicationRelationship
{
   public string Server{get;set;}
   public string Application{get;set;}
}

并形成一种习惯控制,使一个使用<代码>的超文本表格成为可能。 List<ServerApplicationRelationship> as its datasource.

从所提供的数据来看,表应十分容易。

如果你really想要从 s中提取数据,你可以使用 que等方法。

select application,
          case when exists(select 1 from example where application=ex.application and server= server 1 ) THEN 1 ELSE 0 end as [server 1],
          case when exists(select 1 from example where application=ex.application and server= server 2 ) THEN 1 ELSE 0 end as [server 2],
          case when exists(select 1 from example where application=ex.application and server= server 3 ) THEN 1 ELSE 0 end as [server 3]
    from yourTable ex
    group by application




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签