sql为多个行选择最早的日期 [英] sql select earliest date for multiple rows

查看:638
本文介绍了sql为多个行选择最早的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个如下数据库:

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)),该日期将由用户通过网络表单选择。

What I am trying to do is produce a result that has the KWH reading for each customer from the earliest (min(reading_time)) on that date, the date will be selected by the user in a web form.

结果应该是/应该类似于;

The result would be/should be similar to;

Customer 1   87
Customer 2   179
Customer 3   223

此处显示的行数超过了每天的行数,并且有更多的客户,并且客户数量会定期更改。

There are more than the number of rows per day shown here and there are more customers and the number of customers would change regularly.

我对SQL没有太多的经验,我研究过子查询等,但是我没有内容来弄清楚如何按照每个客户的最早阅读来排列它,然后输出 kwh 列。

I do not have much experience with SQL, I have looked at subqueries etc. but I do not have the chops to figure out how arrange it by the earliest reading per customer and then just output the kwh column.

这在Redhat / CentOS的PostgreSQL 8.4中运行。

This is running in PostgreSQL 8.4 on Redhat/CentOS.

推荐答案

select customer_name,
       kwh,
       reading_date, 
       reading_time
from (
   select customer_name,
          kwh,
          reading_time,
          reading_date,
          row_number() over (partition by customer_name order by reading_time) as rn
   from readings
   where reading_date = date '2012-11-17'
) t
where rn = 1

或者:

select r1.customer_name,
       r1.kwh, 
       r1.reading_date,
       r1.reading_time
from readings r1
where reading_date = date '2012-11-17'
and reading_time = (select min(r2.reading_time)
                    from readings
                    where r2.customer_name = r1.customer_name
                    and r2.read_date = r1.reading_date);

但是我希望第一个更快。

But I'd expect the first one to be faster.

顺便说一句:为什么将日期和时间存储在两个单独的列中?您是否知道使用 timestamp 列可以更好地处理此问题?

Btw: why do you store date and time in two separate columns? Are you aware that this could be handled better with a timestamp column?

这篇关于sql为多个行选择最早的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆