我有这种表
+------------------+
| Name Date Category|
+------------------+
| Test1 1/1/2001 2 |
| Test2 2/1/2001 2 |
| Test3 3/1/2001 2 |
| Foo1 5/4/2011 2 |
| Foo2 6/4/2011 2 |
| Test1 6/4/2011 3 |
+------------------+
我想像这样显示结果:
Category Test1Date Foo2Date
-------------------------------
2 1/1/2011 6/4/2001
3 6/4/2011 NULL
我将如何在SQL中执行此操作?
答案 0 :(得分:3)
您可以使用带有CASE表达式的聚合函数将数据行转换为列:
select category,
max(case when name = 'Test1' then date end) Test1Date,
max(case when name = 'Foo2' then date end) Foo2Date
from yourtable
group by category;