English 中文(简体)
Microsoft 访问交叉查询 - 转换为 MSSQL 视图
原标题:Microsoft Access Cross-tab Query - Convert to MSSQL View

I have the following table in access (odbc linked to actual mssql table) Table

I can create a cross-tab query of the table to transform it into this format Cross-tab

以下是访问中生成的 sql :

TRANSFORM First(dbo_85137_PHY_Long_MP.[StatColumnValue]) AS FirstOfStatColumnValue
SELECT dbo_85137_PHY_Long_MP.[StatDate]
FROM dbo_85137_PHY_Long_MP
GROUP BY dbo_85137_PHY_Long_MP.[StatDate]
PIVOT dbo_85137_PHY_Long_MP.[StatColumnName];

我试图在 mssql 内部创建相同的 sql, 但毫无用处( 试图创建视图) 。

最佳回答

由于您正在使用 SQL 服务器, 您需要使用 < a href=" http:// msdn. microsoft. com/ en- us/library/ ms 177410%28v=sql. 105%29. aspx" > PIVOT 操作员。 您可以通过静态 PIVOT 或动态 PIVOT 来做到这一点 。

“坚固”的静态 PIVOT

create table t1
(
  statno int,
  statdate datetime,
  statcolumnname varchar(50),
  statcolumnvalue int
)

insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1MA.MP00-1MA , 17)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1MA.MP01-1MA , 18)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1MA.MP02-1MA , 18)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1MA.MP03-1MA , 18)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1ME.MP10-1ME , 26)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1ME.MP11-1ME , 2)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-1ME.MP12-1ME , 2)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-2MC.MP08-2MC , 2)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-2MC.MP09-2MC , 22)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-2MC.MP0A-2MC , 22)
insert into t1 values (1,  4/1/2012 12:15:00 ,  MPB-2MC.MP0B-2MC , 22)

select *
from 
(
  select statdate, statcolumnname, statcolumnvalue
  from t1
) x
pivot
(
  min(statcolumnvalue)
  for statcolumnname in ([MPB-1MA.MP00-1MA], [MPB-1MA.MP01-1MA], [MPB-1MA.MP02-1MA])
) p

这是"""http://sqlfiddle.com/#!3/75c4b/1>SQL Fiddle ,并举一个实例。

如果您有许多栏目, 这将非常麻烦。 因此您也可以使用一个 < 坚固 > Dynamic Pivit , 这将生成在运行查询时要旋转的列列表 。 以下是代码 :

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct  ,  + QUOTENAME(c.statcolumnname) 
            FROM t1 c
            FOR XML PATH(  ), TYPE
            ).value( . ,  NVARCHAR(MAX) ) 
        ,1,1,  )


set @query =  SELECT statdate,   + @cols +   from 
            (
                select statdate, statcolumnname, statcolumnvalue
                from t1
           ) x
            pivot 
            (
                 min(statcolumnvalue)
                for statcolumnname in (  + @cols +  )
            ) p  


execute(@query)

两者的结果都一样。

问题回答

暂无回答




相关问题
Export tables from SQL Server to be imported to Oracle 10g

I m trying to export some tables from SQL Server 2005 and then create those tables and populate them in Oracle. I have about 10 tables, varying from 4 columns up to 25. I m not using any constraints/...

SQL server: Can NT accounts be mapped to SQL server accounts

In our database we have an SQL server account that has the correct roles to access some of the databases. We are now switching to windows authentication and I was wondering if we can create a NT user ...

SQL Server 2000, ADO 2.8, VB6

How to determine if a Transaction is active i.e. before issuing Begin Transaction I want to ensure that no previous transaction are open.. the platform is VB6, MS-SQL Server 2000 and ADO 2.8

热门标签