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

前端之家收集整理的这篇文章主要介绍了不使用crosstab实现PostgreSQL的行转列应用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

表结构及数据


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

执行结果

原文链接:https://www.f2er.com/postgresql/195534.html

猜你在找的Postgre SQL相关文章