刚开始使用SQL Server。这似乎应该非常容易,但还没有找到答案。
试图摆脱这个:
id fruit value
1 apple 10
2 orange a
3 orange b
4 apple 20
5 apple 30
对此:
fruit quantitative qualitative
apple 10 NULL
orange NULL a
orange NULL b
apple 20 NULL
apple 30 NULL
像...一样的东西。
谢谢!
答案 0 :(得分:3)
select
fruit,
case when fruit = 'apple' then value else null end as quantitative,
case when fruit = 'orange' then value else null end as qualitative
from mytable
答案 1 :(得分:3)
您可以使用ISNUMERIC
和CASE
:
SELECT fruit,
quantitative=CASE WHEN ISNUMERIC(value)=1 THEN value ELSE NULL END,
qualitative=CASE WHEN ISNUMERIC(value)=0 THEN value ELSE NULL END
FROM dbo.Fruits
FRUIT QUANTITATIVE QUALITATIVE
apple 10 (null)
orange (null) a
orange (null) b
apple 20 (null)
apple 30 (null)