我的查询是:
SELECT
ROUND(AVG([Q]), 1) AS ABC,
(SELECT ROUND(AVG([Q]), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Blue') AS 'Internal',
(SELECT ROUND(AVG([Q]), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Green') AS EXTERNAL
FROM
[Apple]
WHERE
[Fruit] = '125'
UNION ALL
SELECT
ROUND(EXP(AVG(LOG([Q]))), 1) AS ABC,
(SELECT ROUND(EXP(AVG(LOG([Q]))), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Blue') AS 'Internal',
(SELECT ROUND(EXP(AVG(LOG([Q]))), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Green') AS EXTERNAL
FROM
[Apple]
WHERE
[Q] != 0
结果:
ABC Internal EXTERNAL
-------------------------
5.6 5.9 6.3
4 4.4 4.8
并且预期结果是:
ABC Internal EXTERNAL
---------------------------------
X 5.6 5.9 6.3
Y 4.1 4.4 4.8
请提供有关如何为SQL Server中获取的结果集添加行名称的查询。
答案 0 :(得分:0)
您可以在以下语句中使用大小写:
CREATE TABLE AR
(
ABC DECIMAL(4,2),
INTERNAL DECIMAL(4,2),
External1 DECIMAL(4,2)
)
INSERT INTO AR
VALUES (5.6, 5.9, 6.3), (4, 4.4, 4.8)
SELECT
CASE
WHEN ABC = 5.60 THEN 'X'
ELSE 'Y'
END, *
FROM
AR
答案 1 :(得分:0)
--Added all records in #temp table
SELECT
--CASE WHEN ABC=5.60 THEN 'X'
--ELSE 'Y' END,
* INTO #temp FROM AR
--create surrogate key using identity column
Alter table #temp
add idnty int identity(1,1)
--add condition using identity column
SELECT
CASE WHEN idnty=1 THEN 'X'
ELSE 'Y' END, ABC, INTERNAL, External1 FROM #temp
drop table #temp
答案 2 :(得分:0)
也许这就是你需要的
SELECT
'first' as ResultSet,
ROUND(AVG([Q]), 1) AS ABC,
(SELECT ROUND(AVG([Q]), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Blue') AS 'Internal',
(SELECT ROUND(AVG([Q]), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Green') AS EXTERNAL
FROM
[Apple]
WHERE
[Fruit] = '125'
UNION ALL
SELECT
'second' as ResultSet,
ROUND(EXP(AVG(LOG([Q]))), 1) AS ABC,
(SELECT ROUND(EXP(AVG(LOG([Q]))), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Blue') AS 'Internal',
(SELECT ROUND(EXP(AVG(LOG([Q]))), 1)
FROM [Apple]
WHERE [Name] = 'DND' AND [Color] = 'Green') AS EXTERNAL
FROM
[Apple]
WHERE
[Q] != 0