【问题标题】:SQL Server Sub-query with min and Max values具有最小值和最大值的 SQL Server 子查询
【发布时间】:2018-09-18 14:14:19
【问题描述】:

我在理解和使用 T-SQL (SQL Server 2012) 中的嵌套子查询时遇到了相当大的困难:

  • 客户名称
  • 客户编号
  • 首次销售日期
  • 最后销售日期

我可以使用以下查询创建显示 Min(sale_date) 或 Max(Sale_date) 的表,但不能同时使用两者。

select 
    cm_number, cm_name, dim_invoice_date
from 
    fact_invoice_history
where 
    fact_invoice_history.dim_invoice_date = (select MAX(fact_invoice_history.dim_invoice_date) 
                                             from fact_invoice_history) 

请帮忙。我知道这是一个初学者的问题。

【问题讨论】:

  • 我怀疑您希望每个 cm_number 都有这个? (而不是整体 fact_invoice_history)。你看过 Group By 吗?
  • 是的,我绝对需要这个是单独的 cm_number,而不是整个历史的最大值。感谢您的澄清。怎么会靠群帮忙? Max date of sale 仍然是一个独特的单一值,而不是一个聚合值。与最小销售日期相同如果我想添加每个 cm_number 的总销售价值,我会看到需要,否则我不确定价值。你能详细说明一下吗?
  • 还是从 fact_invoice_history 中选择 cm_number, cm_name, min(dim_invoice_date), max(dim_invoice_date) 这么简单
  • 当您尝试使用 Group By cm_number 时会发生什么?然后我们还需要 max(cm_name)。
  • 所以您想要为每个客户提供两条记录,一条用于第一次销售日期,一条用于最后一次销售日期,或者您希望为每个客户提供一条记录,同时包含第一次和最后一次销售日期?

标签: sql-server tsql sql-server-2012


【解决方案1】:
select * into #fact_invoice_history from
    (select 1 as cm_number, 'one' as cm_name, '2018-09-01 10:15:00' as dim_invoice_date
    union all select 1, 'one', '2018-09-02 10:15:00'
    union all select 1, 'one', '2018-09-03 10:15:00'
    union all select 2, 'two', '2018-09-06 10:15:00'
    union all select 2, 'two', '2018-09-07 10:15:00'
    union all select 3, 'three', '2018-09-08 10:15:00') data

select * from #fact_invoice_history
-- ----------------------------------------
--  cm_number   cm_name dim_invoice_date
--  ----------- ------- -------------------
--  1           one     2018-09-01 10:15:00
--  1           one     2018-09-02 10:15:00
--  1           one     2018-09-03 10:15:00
--  2           two     2018-09-06 10:15:00
--  2           two     2018-09-07 10:15:00
--  3           three   2018-09-08 10:15:00
-- ----------------------------------------

select 
    cm_number,
    cm_name,
    min(dim_invoice_date) min_sale_date,
    max(dim_invoice_date) max_sale_date
from 
    #fact_invoice_history
group by
    cm_number,
    cm_name
-- ------------------------------------------------------------
--  cm_number   cm_name min_sale_date       max_sale_date
--  ----------- ------- ------------------- -------------------
--  1           one     2018-09-01 10:15:00 2018-09-03 10:15:00
--  3           three   2018-09-08 10:15:00 2018-09-08 10:15:00
--  2           two     2018-09-06 10:15:00 2018-09-07 10:15:00
-- ------------------------------------------------------------

drop table #fact_invoice_history

【讨论】:

    【解决方案2】:

    以下解决方案运行良好。

        select 
        cm_number,
        cm_name,
        min(dim_invoice_date) min_sale_date,
        max(dim_invoice_date) max_sale_date
    from 
        #fact_invoice_history
    group by
        cm_number,
        cm_name
    

    我可能在这里问错了问题,但感谢您的帮助

    【讨论】:

      猜你喜欢
      • 2021-01-03
      • 1970-01-01
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2022-11-25
      • 1970-01-01
      • 2022-09-27
      • 1970-01-01
      相关资源
      最近更新 更多