【问题标题】:Query with aggregation聚合查询
【发布时间】:2014-11-26 05:24:13
【问题描述】:

我正在使用 Oracle SQL,我需要查询方面的帮助。

我有下表 (table_A):

Account_ID (int)
Product_Name (varchar) 
A_Value (int)

产品有 7 种类型,我不应该提前知道它们的名称。 Account_ID 可以有多个 Product,同一账号下 Product 可以有多个。

我还有一张桌子 (table_B):

Account_ID (int)
B_Value (int)

我需要编写一个带有以下输出的查询:A_Value 对每种类型的Product_Name 的总和,除以B_Value

table_A 例如:

Account_ID | Product_Name | A_Value
   111     |     A        |   5
   111     |     B        |   8
   111     |     D        |   2
   222     |     A        |   3
   222     |     A        |   10
   333     |     E        |   5
   333     |     E        |   8
   333     |     A        |   1

table_B 例如:

Account_ID |   Value_B
     111   |    3
     222   |    2
     333   |    1

输出:

Account_ID |  Product_A       |  Product_B    |  Product_C |  Product_D    |  Product_E     | Product_F
   111     |    5/3 = 1.66    |    8/3 = 2.66 |    null    |   2/3 = 0.66  |    null        |   null
   222     |   (3+10)/2 = 6.5 |   null        |    null    |   null        |    null        |   null
   333     |   1/1=1          |   null        |    null    |   null        |  (8+5)/1 = 13  |    null

有人知道怎么做吗?我可以进行计算,但我不知道如何按产品将其分布在列中。可能我不得不用另一种方式来做,但我不知道怎么做。

【问题讨论】:

标签: sql oracle aggregation


【解决方案1】:

您可以通过枢轴获取它,然后是连接:

select b.account_id,
       product_a / b.value_b product_a,
       product_b / b.value_b product_b,
       product_c / b.value_b product_c,
       product_d / b.value_b product_d,
       product_e / b.value_b product_e,
       product_f / b.value_b product_f
from
(
  select *
  from
  (
    select *
    from table_a a
  )
  pivot (sum(a_value) for product_name in ('A' as product_a, 'B' as product_b, 'C' as product_c, 'D' as product_d, 'E' as product_e, 'F' as product_f))
) x, table_b b
where x.account_id = b.account_id
order by 1
;

这给出了:

ACCOUNT_ID  PRODUCT_A   PRODUCT_B   PRODUCT_C   PRODUCT_D   PRODUCT_E   PRODUCT_F
    111         1,666...    2,666...    null        0,666...    null        null        
    222         6,5         null        null        null        null        null
    333         1           null        null        null        13          null

【讨论】:

  • 谢谢。我也试试这个。
【解决方案2】:

您可以使用条件聚合来做到这一点:

select a.account_id,
       sum(case when product_name = 'A' then a_value end) / max(value_b) as product_A,
       sum(case when product_name = 'B' then a_value end) / max(value_b) as product_B,
       . . .
       sum(case when product_name = 'F' then a_value end) / max(value_b) as product_F,
from table_a a join
     table_b b
     on a.account_id = b.account_id
group by a.account_id;

【讨论】:

  • 谢谢。在此查询中,我需要提前知道产品名称。看来我需要给现有产品编号,不是吗?
  • @user3928712 。 . .嗯,是。您的问题表明您有 7 种产品,所以我假设您知道它们是什么。否则,您需要进行动态支点。
  • @user3928712 。 . .你应该问另一个问题。更改问题会使已经存在的答案无效。
  • 对不起,我不知道。我稍后会尝试做。首先,我需要实现您的查询。 BWT,为什么 Value_B 是单个值时使用 max() 函数?
  • 嗯,我猜答案是 max() 可以聚合。
猜你喜欢
  • 2013-12-06
  • 2021-09-27
  • 2014-11-26
  • 2019-05-07
  • 2018-12-18
  • 2012-04-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多