面试时,遇到的数据库题

版权声明:本文为博主原创文章,未经博主允许不得转载。否则,吔屎伺候。 https://blog.csdn.net/guowujun321/article/details/78023759

1.假设有数据库表 table1 如下

如何将它变成下图,(大概就是如何查询使结果这样展示)


首先是根据date进行排列,再就是result的分列。在数据库这块确实薄弱,以下是个人写法:

select date,sum(case when result='有' then 1 else 0 end ) as 有,
sum(case when result='无' then 1 else 0 end) as 无 
from table1 group by date

记录一下,应该有更简单的方式,希望有大牛告知......

猜你喜欢

转载自blog.csdn.net/guowujun321/article/details/78023759