[英]need help in pivoting a data?
First of all, I am new to SQL.首先,我是 SQL 的新手。 Here is the sample (for both
table1
and table2
, I have created a SNO
as primary key and it's also identity column)这是示例(对于
table1
和table2
,我创建了一个SNO
作为主键,它也是标识列)
Table1 :表1 :
PID PNAME PartID
--- ----- ------
0 Length 1
1 Breadth 1
2 Height 1
0 Area 2
1 Volume 2
Table2 :表2 :
SampleID PID Pvalue PartID ModifiedDate Operator
-------- --- ------ ------ ------------ --------
0 0 10 1 10-Mar-14 Test
0 1 10 1 10-Mar-14 Test
0 2 Fail 1 10-Mar-14 Test
1 0 20 1 12-Mar-14 Test
1 1 Fail 1 12-Mar-14 Test
1 2 Fail 1 12-Mar-14 Test
0 0 10 2 13-Mar-14 Test1
0 1 10 2 13-Mar-14 Test1
Depending upon the PartID
, I must get the following results根据
PartID
,我必须得到以下结果
PARTID: 1参与者:1
PNAME 0 1
------------ --------- ---------
Length 10 20
Breadth 10 Fail
Height Fail Fail
ModifiedDate 10-Mar-14 12-Mar-14
Operator Test Test
PARTID: 2当事人:2
PNAME 0
------------ ---------
Area 10
Volume 10
ModifiedDate 13-Mar-14
Operator Test1
How to achieve the desired output as mentioned above in SQL Server 2008?如何在 SQL Server 2008 中实现上述所需的输出?
You can use PIVOT to get the result but you will also need to unpivot the ModifiedDate
and Operator
columns so you can display them in a single column with the PName
.您可以使用 PIVOT 来获取结果,但您还需要对
ModifiedDate
和Operator
列进行反PName
以便您可以将它们显示在带有PName
的单个列中。 Your final result will need a dynamic solution but it would be much easier to write this static first, then convert to dynamic sql.您的最终结果将需要一个动态解决方案,但首先编写此静态代码,然后转换为动态 sql 会容易得多。
The basic syntax will be:基本语法将是:
select pname, [0], [1]
from
(
select t2.sampleid, pname = c.col, c.value
from table1 t1
inner join table2 t2
on t1.partid = t2.partid
and t1.pid = t2.pid
cross apply
(
select Pname, pvalue union all
select 'ModifiedDate', convert(varchar(10), ModifiedDate, 120) union all
select 'Operator', Operator
) c (col, value)
where t1.partid = 1
) d
pivot
(
max(value)
for sampleid in ([0], [1])
) p;
See SQL Fiddle with Demo .请参阅SQL Fiddle with Demo 。 You'll see that I used
CROSS APPLY
to convert the 3 columns PName
, ModifiedDate
and Operator
into a single column.您会看到我使用
CROSS APPLY
将 3 列PName
、 ModifiedDate
和Operator
转换为单个列。 This is necessary so you can easily get to the values for each SampleId
.这是必要的,因此您可以轻松获取每个
SampleId
的值。 The above version is a static version meaning you are hard-coding the values for the final columns, but if you want to have this adjust based on the PartId
, you will need to use dynamic SQL:上面的版本是静态版本,这意味着您正在对最终列的值进行硬编码,但是如果您想根据
PartId
进行此调整,则需要使用动态 SQL:
DECLARE @cols AS NVARCHAR(MAX),
@query AS NVARCHAR(MAX),
@partid int,
@paramdef nvarchar(max)
set @partid = 1
set @paramdef = '@partid int'
select @cols = STUFF((SELECT ',' + QUOTENAME(sampleid)
from Table2
where partid = @partid
group by sampleid
order by sampleid
FOR XML PATH(''), TYPE
).value('.', 'NVARCHAR(MAX)')
,1,1,'')
set @query = 'SELECT pname,' + @cols + '
from
(
select t2.sampleid, pname = c.col, c.value
from table1 t1
inner join table2 t2
on t1.partid = t2.partid
and t1.pid = t2.pid
cross apply
(
select Pname, pvalue union all
select ''ModifiedDate'', convert(varchar(10), ModifiedDate, 120) union all
select ''Operator'', Operator
) c (col, value)
where t1.partid = @partid
) x
pivot
(
max(value)
for sampleid in (' + @cols + ')
) p '
exec sp_executesql @query, @paramdef, @partid = @partid;
See SQL Fiddle with Demo .请参阅SQL Fiddle with Demo 。 Both give a result:
两者都给出了结果:
| PNAME | 0 | 1 |
|--------------|------------|------------|
| Breadth | 10 | Fail |
| Height | Fail | Fail |
| Length | 10 | 20 |
| ModifiedDate | 2014-03-10 | 2014-03-12 |
| Operator | Test | Test |
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.