看了邹建blog中的这篇文章,感觉很实用
问题描述:
有表tb,数据如下
A
1 2 5 3 4
2 2 3 4 5
0 3 4 2 5
如何输出
A
1 2 5 3 4 5 1 1
2 2 3 4 5 5 2 1
0 3 5 2 6 6 0 2
答:
解决示例
--测试数据
DECLARE @t TABLE(A1 int, A2 int, A3 int, A4 int, A5 int)
INSERT @t SELECT 1,2,5,3,4
--查询
SELECT *,
[min] = (
SELECT MIN(v) FROM(
SELECT v=A.A1 UNION SELECT v=A.A2 UNION SELECT v=A.A3
UNION SELECT v=A.A4 UNION SELECT v=A.A5
)B),
[max] = (
SELECT MAX(v) FROM(
SELECT v=A.A1 UNION SELECT v=A.A2 UNION SELECT v=A.A3
UNION SELECT v=A.A4 UNION SELECT v=A.A5
)B),
[count>=5] = (
SELECT COUNT(*) FROM(
SELECT v=A.A1 UNION SELECT v=A.A2 UNION SELECT v=A.A3
UNION SELECT v=A.A4 UNION SELECT v=A.A5
)B WHERE v>=5)
FROM @t A
查询结果: