Convert one record into form of two column

2020-07-29 03:50发布

问题:

In my old Stored Proceder result returned in form of counts . it returned as below. Result is correct but now I want this row result in form of table. Please look into this and help me.

Result in form of Row:

SELECT 100 AS A ,200 AS B ,300 AS C

Required Result in form of column:

TextKey  |  TextValue
---------------------
   A           100
   B           200 
   C           300

Thanks in Advance.

回答1:

Simplest would be to use a UNION ALL

SELECT 'A' AS TextKey, 100 AS TextValue UNION ALL
SELECT 'B', 200 UNION ALL
SELECT 'C', 300 


回答2:

select TextKey, TextValue
from (
    SELECT 100 AS A ,200 AS B ,300 AS C
) t unpivot (TextValue for TextKey in ([A], [B], [C])) tp


回答3:

Query: SQLFIDDLEExample

SELECT a.* 
FROM (VALUES ('A', 100), 
             ('B', 200), 
             ('C', 300)) as a(TextKey, TextValue)

Result:

| TEXTKEY | TEXTVALUE |
-----------------------
|       A |       100 |
|       B |       200 |
|       C |       300 |