首页 > 代码库 > 不使用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

执行结果


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