【发布时间】:2012-11-06 03:54:09
【问题描述】:
我有一个如下所示的数据库;
circuit_uid | customer_name | location | reading_date | reading_time | amps | volts | kw | kwh | kva | pf | key
--------------------------------------------------------------------------------------------------------------------------------------
cu1.cb1.r1 | Customer 1 | 12.01.a1 | 2012-01-02 | 00:01:01 | 4.51 | 229.32 | 1.03 | 87 | 1.03 | 0.85 | 15
cu1.cb1.r1 | Customer 1 | 12.01.a1 | 2012-01-02 | 01:01:01 | 4.18 | 230.3 | 0.96 | 90 | 0.96 | 0.84 | 16
cu1.cb1.s2 | Customer 2 | 10.01.a1 | 2012-01-02 | 00:01:01 | 7.34 | 228.14 | 1.67 | 179 | 1.67 | 0.88 | 24009
cu1.cb1.s2 | Customer 2 | 10.01.a1 | 2012-01-02 | 01:01:01 | 9.07 | 228.4 | 2.07 | 182 | 2.07 | 0.85 | 24010
cu1.cb1.r1 | Customer 3 | 01.01.a1 | 2012-01-02 | 00:01:01 | 7.32 | 229.01 | 1.68 | 223 | 1.68 | 0.89 | 48003
cu1.cb1.r1 | Customer 3 | 01.01.a1 | 2012-01-02 | 01:01:01 | 6.61 | 228.29 | 1.51 | 226 | 1.51 | 0.88 | 48004
我要做的是生成一个结果,该结果在该日期最早 (min(reading_time)) 为每个客户读取 KWH,该日期将由用户在网络表单中选择。
结果将/应该类似于;
Customer 1 87
Customer 2 179
Customer 3 223
这里显示的每天的行数超过了,客户也更多,客户的数量会定期变化。
我对 SQL 没有太多经验,我看过子查询等,但我没有能力通过每个客户的最早阅读来确定如何安排它,然后只输出 kwh 列。
这是在 Redhat/CentOS 上的 PostgreSQL 8.4 中运行的。
【问题讨论】:
-
请提出一个新问题。在给出好的答案之后,通常不能大幅改变问题的性质。您始终可以链接回此内容以获取上下文。我恢复了您的更改,您可以在编辑日志中找到它。
标签: sql postgresql greatest-n-per-group