我的属性表有多个数据。
我给出了简单的例子
我有一张包含以下数据的表: -
<html>
<head></head>
<body>
<table border="1" style="width:100%">
<tr>
<td>ID</td>
<td>NAME</td>
<td>ROLL NO</td>
</tr>
<tr>
<td>1</td>
<td>Description</td>
<td>94</td>
</tr>
<tr>
<td>2</td>
<td>StockNumber</td>
<td>80</td>
</tr>
<tr>
<td>3</td>
<td>Keyword</td>
<td>20</td>
</tr>
</table>
</body>
</html>
&#13;
I want to show following output:-
<html>
<body>
<table border="1" style="width:100%">
<tr>
<td>Description</td>
<td>StockNumber</td>
<td>Keyword</td>
</tr>
</tr>
</table>
</body>
</html>
&#13;
我想显示行数据(即: - Description
,StockNumber
,Keyword
)作为列而不动态使用Pivot
。
有没有其他方法可以将行数据动态地显示为列
答案 0 :(得分:0)
请使用以下代码。它在SQL Server 2012中工作。
DECLARE @Attribute TABLE(
ID INT,
Name VARCHAR(250)
)
INSERT INTO @Attribute VALUES
(1, 'Description'),
(2, 'StockNumber'),
(3, 'Price'),
(4, 'Logo'),
(5, 'Diagram');
SELECT MAX(CASE WHEN Name = 'Description' THEN ID END) AS Description,
MAX(CASE WHEN Name = 'StockNumber' THEN ID END) AS StockNumber,
MAX(CASE WHEN Name = 'Price' THEN ID END) AS Price,
MAX(CASE WHEN Name = 'Logo' THEN ID END) AS Logo,
MAX(CASE WHEN Name = 'Diagram' THEN ID END) AS Diagram
FROM (
SELECT Name,ID,
ROW_NUMBER() OVER (PARTITION BY Name ORDER BY (SELECT NULL)) AS rn
FROM @Attribute) AS t
GROUP BY rn