Convert one record into form of two column

2020-07-29 03:56发布

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.

3条回答
SAY GOODBYE
2楼-- · 2020-07-29 04:07

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 |
查看更多
爷的心禁止访问
3楼-- · 2020-07-29 04:23

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 
查看更多
Melony?
4楼-- · 2020-07-29 04:29
select TextKey, TextValue
from (
    SELECT 100 AS A ,200 AS B ,300 AS C
) t unpivot (TextValue for TextKey in ([A], [B], [C])) tp
查看更多
登录 后发表回答