【问题标题】:SQL query aggregate function with two tables具有两个表的 SQL 查询聚合函数
【发布时间】:2016-07-11 08:27:29
【问题描述】:

我正在尝试从 SQL 中查询一些数据,以便对某些列求和,从另一个表中获取其他列的最大值和相应的值。例如,

|table1|

 |order id| |id|   |shares|  |date|      other stuff
  12345      1       100      05/13/16     XXX
  12345      2       200      05/15/16     XXX
  12345      3       300      06/12/16     XXX
  12345      4       400      02/22/16     XXX
  56789      5       1000     03/30/16     XXX
  56789      6       200      02/25/16     XXX
  22222      7       5000     01/10/16     XXX

|table2|

 |id|   |price|
  1       21.2
  2       20.2
  3       19.1
  4       21.3
  5       100.0
  6       110.0
  7       5.0

我希望我的输出是:

 |shares|  |date|      |price|   other stuff
  1000      06/12/16    19.1      max(other stuff)
  1200      03/30/16    1000.0    max(other stuff)
  5000      01/10/16    5.0       max(other stuff)

股票已经汇总,日期为max(date),价格为对应max(date)时的价格。

到目前为止,我有:

select 
    orderid, stock, side, exchange, 
    max(startdate), max(enddate),
    sum(shares), sum(execution_price * shares) / sum(shares), 
    max(limitprice), max(price)
from 
    table1 t1
inner join
    table2 t2 on t2.id = t1.id
where 
    location = 'CHICAGO' 
    and startdate > '1/1/2016' 
    and order_type = 'limit'
group by 
    orderid, stock, side, exchange

但是,这会返回:

 |shares|  |date|      |price|   |other stuff|
  1000      06/12/16    21.3      max(other stuff)
  1200      03/30/16    1100.0    max(other stuff)
  5000      01/10/16    5.0       max(other stuff)

这不是最大值(日期)的对应价格。

两个数据集之间的唯一联系是它们的 ID 号,这就是为什么

inner join
    table2 t2 on t2.id = t1.id

完成了。第二个表中根本没有日期。有什么帮助吗?

谢谢。

【问题讨论】:

  • outer apply top 1 ... order by date desc

标签: sql sql-server database


【解决方案1】:

您可以使用子查询解决此问题。您不需要在价格列上使用任何聚合函数,只需找到最大日期,然后获取该特定日期的价格。试试这样的..

select t5.*, t4.price
from 
  (select t1.order_id, sum(t1.shares) as shares, max(t1.date) as maxdate, max(other_stuff) as other_stuff
  from Table1 t1
  inner join
  Table2 t2 on t2.id = t1.id    
  group by  t1.order_id) t5

inner join Table1 t3
on t5.maxdate = t3.date and t5.order_id = t3.order_id 
inner join Table2 t4
on t3.id = t4.id;

ONLINE DEMO HERE

【讨论】:

    【解决方案2】:

    试试这个(别忘了用你自己的表名替换@table1 和@table2):

    SELECT Aggregated.shares
    , Aggregated.date
    , Aggregated.other_stuff
    , T2.price
    FROM (
        SELECT order_id
        , SUM(shares) as shares
        , MAX(date) as date
        , MAX(other_stuff) as other_stuff
        FROM @table1 AS T1
        GROUP BY order_id
    ) AS Aggregated
    INNER JOIN @table1 AS T1 ON Aggregated.order_id = T1.order_id AND Aggregated.date = T1.date
    INNER JOIN @table2 AS T2 ON T2.id = T1.id
    

    【讨论】:

      【解决方案3】:

      所以,在我给你写一个查询之前,你基本上想要那个最大日期的价格,对吗?你有 MAX 的价格,股票的总和,最高的限价,股票的总和等等。

      我的猜测是您想要基于最新日期 (Max) 的最新价格,然后运行最新日期的计算、该最大日期的最新股票数量,然后将它们加在一起?您还对 ID、Shares 和其他没有意义的事物进行分组,您似乎希望对 Shares、Side 和 Exchange 进行分组,而不是 ID。看起来你在其他事情上放了一个最大值,只是为了让它们出现而不必对它们进行分组,只要我认为我知道你在寻找什么,这不会为你想要的工作=)让我知道并且如果我知道您的最终结果“规格”是什么,我绝对可以提供帮助。

      【讨论】:

      • 嗨,是的,我确实想要基于最新日期(最大值)的最新价格。你是绝对正确的。问题是我确实想按 ID 排序,因为某些行具有需要分组的相同 ID。似乎我在最初的查询中犯了一个小错误,我认为在我编辑后现在更有意义。数据集基本上是一堆需要组合成几个父订单的子订单,因此需要对份额进行求和,而我将其他内容设置为最大值,因此我不必分组。任何帮助将不胜感激
      【解决方案4】:

      我会使用按日期排序的最大分区进行子查询以显示最后日期价格,然后在上层进行聚合,这是一个如何工作的示例。

      样本数据

      pk      id  shares   date                       id  price
      ------- --- -------- -------------------------- --- -------
      100     1   100      2016-07-08 10:40:34.707    1   50
      100     2   200      2016-07-06 10:40:34.707    2   20
      101     3   500      2016-07-09 10:40:34.707    3   70
      101     4   150      2016-07-07 10:40:34.707    4   80
      102     5   300      2016-07-10 10:40:34.707    5   40
      

      查询

      with t1 as (
      select 100 pk,1 id, 100 shares, getdate()-3 date union all
      select 100 pk,2 id, 200 shares, getdate()-5 date union all
      select 101 pk,3 id, 500 shares, getdate()-2 date union all
      select 101 pk,4 id, 150 shares, getdate()-4 date union all
      select 102 pk,5 id, 300 shares, getdate()-1 date ),
      t2 as (
      select 1 id, 50 price union all
      select 2 id, 20 price union all
      select 3 id, 70 price union all
      select 4 id, 80 price union all
      select 5 id, 40 price
      )
      SELECT pk,sum(shares) shares,max(date) date, max(price) from(
      SELECT pk,
             shares,
             date,
            MAX(price) over(partition by pk order by date desc) price
      FROM t1
      JOIN t2 ON t1.id = t2.id) a
      group by pk
      

      结果

      pk  shares  date                     Price
      --- ------- ------------------------ -----
      100 300     2016-07-08 10:51:16.023  50
      101 650     2016-07-09 10:51:16.023  80
      102 300     2016-07-10 10:51:16.023  40
      

      【讨论】:

        猜你喜欢
        • 2017-10-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-17
        • 1970-01-01
        • 2020-11-03
        • 1970-01-01
        相关资源
        最近更新 更多