【发布时间】: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