【问题标题】:How do I select min/max dates from table 2 based on date in table 1 (without getting too much data from sums)如何根据表 1 中的日期从表 2 中选择最小/最大日期(不会从总和中获取太多数据)
【发布时间】:2010-12-14 13:56:09
【问题描述】:

与我之前问过的一个问题 here 相关,我发现了一个问题,我(显然)一直在想办法。

最初的问题是如何根据可能缺少某些日表日期的月表从日表中选择最小和最大日期。基本上我需要的是包含月份日期(总是第一个)、日表中该月的最早日期和日表中该月的最晚日期的列。

所以,如果 1 月的最后一周和 2 月的第一周从日表中丢失(否则我们有 1 月和 2 月的所有日期,但没有更多),我需要:

MonthStart  DayFirst    DayLast
----------  ----------  ----------
2009-01-01  2009-01-01  2009-01-24
2009-02-01  2009-02-08  2009-02-28

答案是:

select
    m.date as m1,
    min(d.date) as m2,
    max(d.date) as m3
from monthly m
join daily d
    on month(d.date) = month(m.date)
    and year(d.date) = year(m.date)
group by m.date
order by m.date

这适用于我给出的规格。

不幸的是,现实咬住了,月表(和日表)中有多个相同日期的记录。具体来说:

  • 日期为2007-10-162007-10-30(15 天)、2007-11-012007-11-30(30 天)和2007-12-012007-12-15(15 天)。
  • 每个日期在两个表中都有六行(因为它们每行都有三个系统名称和两个句点。

问题是我 sum() 月度表中的一个字段,而新查询获取的值太大(与没有连接的前一个查询相比)。

聚合将查询更改为:

select
    m.date as m1,
    sum(m.other_field),  -- added this
    min(d.date) as m2,
    max(d.date) as m3
from monthly m
join daily d
    on month(d.date) = month(m.date)
    and year(d.date) = year(m.date)
group by m.date
order by m.date

我认为由于交叉连接的进行,这些值太高了,因为每个月的数字都超出了一个常数因子,具体取决于该月日表中的天数。

我的问题是:如何在不影响该因素的情况下聚合月度表中的字段并且仍然从该月的日表中获取最小/最大日期?

【问题讨论】:

    标签: sql database select db2


    【解决方案1】:

    如果monthly 表包含每个月的单个条目,您可以这样做:

    select
        m.date as m1,
        m.other_field,
        min(d.date) as m2,
        max(d.date) as m3
    from monthly m
    join daily d
        on month(d.date) = month(m.date)
        and year(d.date) = year(m.date)
    group by m.date, m.other_field
    order by m.date
    

    否则:

    select m1, sum(other_field), m2, m3
    from (
            select
            m.date as m1,
            m.other_field,
            min(d.date) as m2,
            max(d.date) as m3
        from monthly m
        join daily d
            on month(d.date) = month(m.date)
            and year(d.date) = year(m.date)
        group by m.date, m.other_field) A
    group by A.m1, A.m2, A.m3
    order by A.m1
    

    来自 pax 的更新: 尽我所能,我无法让 join 解决方案正常工作 - 它们似乎都返回与原始数据相同的错误数据。最后,我选择了非join 解决方案,因为它可以工作并且性能不是大问题,因为这些表通常有 24 行(每月)和 700 行(每天)。我正在编辑这个答案并接受它,因为(1)它实际上对我获得正确的解决方案有很大帮助; (2) 我不愿意自己写答案并为自己争取荣耀。

    感谢您的所有帮助。以下是对我有用的:

    select
        m.date as p1,
        m.grouping_field as p2,
        sum(m.aggregating_field) as p3,
        (select min(date) from daily
            where month(date) = month(m.date)
            and year(date) = year(m.date)) as p4,
        (select max(date) from daily
            where month(date) = month(m.date)
            and year(date) = year(m.date)) as p5
    from
        monthly m
    group by
        m.date, m.grouping_field
    

    这给了我想要的东西:

        P1       P2    P3       P4         P5
    ----------  ----  ----  ----------  ----------
    2007-10-01  BoxA  12.3  2007-10-16  2007-10-30
    2007-10-01  BoxB  13.6  2007-10-16  2007-10-30
    2007-10-01  BoxC   7.4  2007-10-16  2007-10-30
    2007-11-01  BoxA  20.3  2007-11-01  2007-11-30
    2007-11-01  BoxB  24.2  2007-11-01  2007-11-30
    2007-11-01  BoxC  21.7  2007-11-01  2007-11-30
    2007-12-01  BoxA   6.9  2007-12-01  2007-12-15
    2007-12-01  BoxB   6.4  2007-12-01  2007-12-15
    2007-12-01  BoxC   6.9  2007-12-01  2007-12-15
    

    【讨论】:

      【解决方案2】:

      您可以在子查询中对月份进行分组:

      select
          m.mindate as m1,
          m.sum_other_field,
          min(d.date) as m2,
          max(d.date) as m3
      from (
          select 
               month(date) as month,
               year(date) as year,
               sum(other_field) sum_other_field,
               min(date) mindate
          from monthly
          group by month(date), year(date)
      ) m
      join daily d
          on month(d.date) = m.month
          and year(d.date) = m.year
      group by m.month
      order by m.year
      

      【讨论】:

        猜你喜欢
        • 2010-12-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-24
        • 2018-11-18
        • 2019-02-07
        • 1970-01-01
        • 2014-06-04
        相关资源
        最近更新 更多