Postgresql 如何在Postgres Sql中两次选择不同格式的同一列?
我想要实际的列值和截断的列值,如下所示。谁都可以帮忙Postgresql 如何在Postgres Sql中两次选择不同格式的同一列?,postgresql,Postgresql,我想要实际的列值和截断的列值,如下所示。谁都可以帮忙 Select "sub1"."TimestampField","month_trunc1","month_trunc2" from ( Select "Id", date_trunc('month', "TimestampField") as "month_trunc1", "TimestampField" from "Table" ) as "sub1", ( Select "Id", date_trunc('mon
Select "sub1"."TimestampField","month_trunc1","month_trunc2"
from (
Select "Id", date_trunc('month', "TimestampField") as "month_trunc1", "TimestampField"
from "Table"
) as "sub1", (
Select "Id", date_trunc('month', "TimestampField") as "month_trunc2", "TimestampField"
from "Table"
) as "sub2"
WHERE "month_trunc2" = ("month_trunc1"+interval '1 month');
通过在同一个表上使用JOIN,可以简化示例:
SELSECT
"sub1"."mainfield",
"sub1"."TimestampField" as month1,
"sub2"."TimestampField" as month2,
date_trunc('month', "sub1"."TimestampField") as "month_trunc1",
date_trunc('month', "sub2"."TimestampField") as "month_trunc2"
FROM "Table" "sub1"
JOIN "Table" "sub2"
ON "month_trunc2" = ("month_trunc1"+interval '1 month');
请回答您的问题,并根据这些数据添加一些内容和预期输出。请(您的问题-请勿在注释中发布代码或其他信息)请注意,postgres不能在第条中使用别名,因此您必须将“month__-thrunc1/2”替换为完整的语句日期(“month”,“sub1/2”,“TimestampField”),我希望实际的TimestampField值和截断的TimestampField值date_-trunc(“month”,“TimestampField”)“月份\u trunc1”,“时间戳字段”