我有单行应该转换成列

时间:2016-03-16 20:34:51

标签: sql sql-server sql-server-2008 sql-server-2005 sql-server-2012

请告诉我们如何显示

| NumberOfQuotesGenerated | 11
| TotalAmountOfQuotes | 78100
| NumberOfInvoiceGenerated | 9
| TotalAmountOfInvoice | 8222

提前谢谢

1 个答案:

答案 0 :(得分:1)

您可以尝试Unpivot语法,如下所示

-- create table t(NumberOfQuotesGenerated int,TotalAmountOfQuotes int,NumberOfInvoiceGenerated int, TotalAmountOfInvoice int)
-- insert into t values (11, 78100, 9, 8222)

select * from 
(select * from t) s
unpivot
(
   data for 
   colname in 
      ([NumberOfQuotesGenerated],[TotalAmountOfQuotes],[NumberOfInvoiceGenerated],[TotalAmountOfInvoice])
)up

<强> Live SQL demo