简体   繁体   English

具有三个交叉表和多列的Sql Pivot 表动态

[英]Sql Pivot table with three cross tab and multiple columns dynamically

I have the following table with values as我有下表的值

CREATE TABLE stud 
  ( 
     sname NVARCHAR(10), 
     hr    NVARCHAR(30), 
     dt    DATETIME, 
     att   VARCHAR(3) 
  )

INSERT INTO stud VALUES ('Abi',  '1',  '21/01/2013','a')
INSERT INTO stud VALUES ('Abi',  '2',  '21/01/2013','p')
INSERT INTO stud VALUES ('bala',  '1',  '21/01/2013','p')
INSERT INTO stud VALUES ('bala',  '2',  '21/01/2013','a')
INSERT INTO stud VALUES ('bala',  '1',  '22/01/2013','od')
INSERT INTO stud VALUES ('bala',  '2',  '22/01/2013','ml')
INSERT INTO stud VALUES ('Abi',  '1',  '22/01/2013','ml')
INSERT INTO stud VALUES ('Abi',  '2',  '22/01/2013','od')

If i select this table i get the output as如果我选择这个表,我得到的输出为

SELECT * 
FROM   stud 
sname   hr            dt                att
Abi 1   2013-01-21 00:00:00.000  a
Abi 2   2013-01-21 00:00:00.000  p
bala    1   2013-01-21 00:00:00.000  p
bala    2   2013-01-21 00:00:00.000  a
bala    1   2013-01-22 00:00:00.000  od
bala    2   2013-01-22 00:00:00.000  ml
Abi 1   2013-01-22 00:00:00.000  ml
Abi 2   2013-01-22 00:00:00.000  od

but I want the output as follows in crystal report in ASP.NET ( Note : the date should given as dynamically as from_date to to to_date )但我希望在 ASP.NET 中的水晶报告中的输出如下(注意:日期应该像from_date到 to_date一样动态给出)

sname 21/01/2013 22/01/2013

I tried for this output from long days itself, but didn't get output.我从漫长的日子里尝试过这个输出,但没有得到输出。

If you are using SQL Server 2005+, then there are several ways that you can apply the PIVOT function.如果您使用的是 SQL Server 2005+,则可以通过多种方式应用PIVOT函数。

You can hard-code the values in the form of a static pivot:您可以以静态枢轴的形式对值进行硬编码:

select *
from
(
  select sname, 
    'hour_no_'+hr+'_'+convert(nvarchar(10), dt, 120) dt,
    att
  from stud
) st
pivot
(
  max(att)
  for dt in ([hour_no_1_2013-01-21], [hour_no_2_2013-01-21],
             [hour_no_1_2013-01-22], [hour_no_2_2013-01-22])
) piv

See SQL Fiddle with Demo参见SQL Fiddle with Demo

Or you can use dynamic sql to generate the sql statement at run-time.或者您可以使用动态 sql 在运行时生成 sql 语句。 The dynamic version of the query is:查询的动态版本是:

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

select @cols 
  = STUFF((SELECT ', ' + QUOTENAME('Hour_No_'+hr+'_'++convert(nvarchar(10), dt, 120)) 
           from stud
           group by hr, dt
           order by dt, hr
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT sname,' + @cols + ' from 
             (
                select sname, 
                  ''hour_no_''+hr+''_''+convert(nvarchar(10), dt, 120) dt,
                  att
                from stud
            ) x
            pivot 
            (
                max(att)
                for dt in (' + @cols + ')
            ) p '

execute(@query)

See SQL Fiddle with Demo .请参阅SQL Fiddle with Demo

Both give the result:两者都给出了结果:

| SNAME | HOUR_NO_1_2013-01-21 | HOUR_NO_2_2013-01-21 | HOUR_NO_1_2013-01-22 | HOUR_NO_2_2013-01-22 |
-----------------------------------------------------------------------------------------------------
|   Abi |                    a |                    p |                   ml |                   od |
|  bala |                    p |                    a |                   od |                   ml |

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM