【问题标题】:SQL First transaction of each daySQL 每天的第一笔交易
【发布时间】:2014-03-29 19:50:26
【问题描述】:

我有一个包含许多交易数据的表,我一直在尝试获取每个客户每天最早的记录,调整我在这个网站上看到的其他解决方案(例如this one),但他们对我没用。

表格transactions

时间 ID 客户价格 数量 2013 年 1 月 2 日 09:33:20 AM 1 阿尔伯特 100.00 5,300 2013 年 1 月 2 日上午 10:34:20 2 阿尔伯特 100.90 4,800 2013 年 1 月 2 日上午 10:34:20 3 刘易斯 80.00 25,987 2013 年 1 月 2 日上午 11:35:23 4 本森 251.00 700 1/2/2013 14:36:20 AM 5 阿尔伯特 100.00 2,250 1/2/2013 15:31:12 AM 6 阿尔伯特 99.50 1,340 2013 年 1 月 3 日 09:33:20 AM 7 本森 250.00 900 2013 年 1 月 3 日上午 15:13:12 8 班森 250.00 800 1/3/2013 16:03:55 AM 9 刘易斯 80.00 18,890 1/4/2013 09:01:01 AM 10 阿尔伯特 101.00 1,190 1/4/2013 09:01:01 AM 11 阿尔伯特 100.99 98,890 1/4/2013 09:01:01 AM 12 刘易斯 80.98 6,890 2013 年 1 月 4 日上午 10:51:00 13 本森 279.18 190 2013 年 1 月 4 日上午 10:51:00 14 阿尔伯特 99.36 78,053 ...

Id 是唯一的,并且也按定义按时间顺序排序。时间不是唯一的,这意味着可能有 2 笔交易恰好同时发生。

sql 查询需要提取每个客户每天进行的第一笔交易,以及价格和数量,例如:

日期 客户 价格 数量 1/2/2013 阿尔伯特 100.00 5,300 1/2/2013 本森 251.00 700 1/2/2013 刘易斯 80.00 25,987 1/3/2013 本森 250.00 900 1/3/2013 刘易斯 80.00 18,890 2013 年 1 月 4 日 阿尔伯特 101.00 1,190 1/4/2013 刘易斯 80.98 6,890 1/4/2013 本森 279.18 190

谁能帮助我如何在 SQL 中做到这一点?

【问题讨论】:

  • 你用的是什么数据库?
  • 数据库在 MS Access 中,但我可能会在 MySQL 中进行查询

标签: sql date ms-access greatest-n-per-group


【解决方案1】:

在 SQL Server 中,这样的事情应该可以工作:

select cast(Time as date), Client, Price, Quantity
from (
 select *, row_number() 
           over (partition by Client, cast(Time as Date) order by Id) [rn] 
 from transactions
) x where x.rn = 1

这是一个 sqlfiddle:http://sqlfiddle.com/#!6/0725d/1

【讨论】:

    【解决方案2】:

    您没有指定数据库。所以这里是一个通用的方法。这个想法适用于大多数数据库,但有些功能是不同的。

    select cast(t.time as date) as "date", t.*
    from transactions t
    where not exists (select 1
                      from transactions t2
                      where cast(t2.time as date) = cast(t.time as date) and
                            t2.client = t.client and
                            t2.id < t.id
                     );
    

    从时间获取日期的表达式各不相同。在某些数据库中,这可能是 date(time) (MySQL) 或 trunc(time) (Oracle) 或其他。

    编辑:

    在 Access 中,这将是:

    select CDATE(t.time) as [date], t.*
    from transactions t
    where not exists (select 1
                      from transactions t2
                      where CDATE(t2.time) = CDATE(t.time) and
                            t2.client = t.client and
                            t2.id < t.id
                     );
    

    在 MySQL 中:

    select date(t.time) as "date", t.*
    from transactions t
    where not exists (select 1
                      from transactions t2
                      where date(t2.time) = date(t.time) and
                            t2.client = t.client and
                            t2.id < t.id
                     );
    

    【讨论】:

    • 不过,这也不会按客户端进行分区。
    • @Blorgbeard 。 . .谢谢你。我错过了那个条件。
    • 谢谢,由于某种原因,CDATE 在 MS Access 中无法正常工作,但在 MySQL 中运行良好..
    猜你喜欢
    • 2019-07-16
    • 1970-01-01
    • 2022-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-03-04
    • 2013-08-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多