我遇到了一个带有UNION查询的SQL Server 2012的小问题。
我有一个简单的select语句但它没有按照sl_no列
按顺序返回数据SELECT *
FROM
(SELECT '1'SL_NO, '10000' [KEY METRIC] UNION
SELECT '2', '5000' UNION
SELECT '3', '1000' UNION
SELECT '4', '750' UNION
SELECT '5', '500' UNION
SELECT '6', '250' UNION
SELECT '7', '100' UNION
SELECT '8', '50' UNION
SELECT '9', '25' UNION
SELECT '10', '10' UNION
SELECT '11', '5' UNION
SELECT '12', 'AAL' UNION
SELECT '13', 'Standard Deviation' UNION
SELECT '14', 'Coefficient of Variation') A
order by SL_NO
我需要以下列格式提供这些数据
|1 |10000 |
|2 |5000 |
|3 |1000 |
|4 |750 |
|5 |500 |
|6 |250 |
|7 |100 |
|8 |50 |
|9 |25 |
|10 |10 |
|11 |5 |
|12 |AAL |
|13 |Standard Deviation|
|14 |Coefficient of Variation|
答案 0 :(得分:2)
为什么将int存储为varchar
SELECT *
FROM
(SELECT 1 SL_NO, '10000' [KEY METRIC] UNION
SELECT 2, '5000' UNION
SELECT 3, '1000' UNION
SELECT 4, '750' UNION
SELECT 5, '500' UNION
SELECT 6, '250' UNION
SELECT 7, '100' UNION
SELECT 8, '50' UNION
SELECT 9, '25' UNION
SELECT 10, '10' UNION
SELECT 11, '5' UNION
SELECT 12, 'AAL' UNION
SELECT 13, 'Standard Deviation' UNION
SELECT 14, 'Coefficient of Variation') A
order by SL_NO
答案 1 :(得分:2)
使用以下内容:
ORDER BY CAST(SL_NO AS INT)
答案 2 :(得分:0)
SELECT *
FROM
(SELECT 1 SL_NO, '10000' [KEY METRIC] UNION
SELECT 2, '5000' UNION
SELECT 3, '1000' UNION
SELECT 4, '750' UNION
SELECT 5, '500' UNION
SELECT 6, '250' UNION
SELECT 7, '100' UNION
SELECT 8, '50' UNION
SELECT 9, '25' UNION
SELECT 10, '10' UNION
SELECT 11, '5' UNION
SELECT 12, 'AAL' UNION
SELECT 13, 'Standard Deviation' UNION
SELECT 14, 'Coefficient of Variation') A
order by SL_NO Asc