码迷,mamicode.com
首页 > 数据库 > 详细

不使用crosstab实现PostgreSQL的行转列应用

时间:2014-11-04 21:10:41      阅读:846      评论:0      收藏:0      [点我收藏+]

标签:postgresql   行转列   

表结构及数据

bubuko.com,布布扣

select logdatetime,sum(login) as login,sum(logo) as logo

from(
SELECT date(logdatetime) AS "logdatetime", case logfrom when ‘login‘ then COUNT(id) else 0 end AS "login",
case logfrom when ‘logo‘ then COUNT(id) else 0 end AS "logo"
FROM "log" WHERE date(logdatetime) >= ‘2014-11-04‘ AND date(logdatetime) <= ‘2014-11-05‘ GROUP BY "logdatetime", "logfrom"
) T
GROUP BY "logdatetime"

ORDER BY "logdatetime" ASC

执行结果

bubuko.com,布布扣

不使用crosstab实现PostgreSQL的行转列应用

标签:postgresql   行转列   

原文地址:http://blog.csdn.net/wang_quan_li/article/details/40792243

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!