SELECT
a.alloc_date,
p.plan,
p.emp_id,
a.veh,
a.contri_type,
a.amount,
SUM (a.alloc_qty) AS sum_alloc_qty, -- 1000 funds distributed
SUM (a.alloc_qty * a.amount) AS sum_alloc_value, -- 1000*2 = 2000
COUNT (DISTINCT part_id) AS sum_emp_count, -- 4 employees
MAX (a.alloc_qty * a.amount) AS max_value_to_one_emp, -- 600
null as "emp_count_with_max_value" -- Unable to retrieve - idealy answer should be 3 in this example
FROM
alloc a, emp p
WHERE
A.alloc_date >= TO_DATE ('20111001', 'YYYYMMDD')
AND a.emp_id = p.emp_id
GROUP BY
a.alloc_date,
p.plan,
p.emp_id,
a.veh,
a.contri_type,
a.amount
ORDER BY
alloc_date, emp_id, amount
此处,现有查询的工作方式如下。
假设,该公司正在分配1000只基金,其中每只基金的价格为2。
现在这1000个基金分配给4名员工。
基本问题是检索一名员工的最大资金价值。假设,资金分配是:
emp1=600 (300*2), emp2=600 (300*2), emp3=600 (300*2), emp4=300 (300*1)
所以,这里一个雇员的资金最大值= 600。
我现在可以通过查询检索。
但是,现在下一个问题是检索另一列(emp_count_with_max_value
),该列应该具有在每个组下获得此最大值的员工数量。
在我们的例子中,结果是3名员工。但我无法检索相同的
这里我只给出了一组的数据。结果查询输出应如下所示:
'11/12/86','abc','E25','pqr','qvr',2,1000,2000,4,600,3
答案 0 :(得分:1)
基本上,如果您对员工的分配进行排名,很容易确定谁获得了最大的金额。然后你需要有一个外部查询来计算幸运狗的数量。
select alloc_date,
plan,
emp_id,
veh,
contri_type,
amount,
sum_alloc_qty,
sum_alloc_value,
sum_emp_count,
max_value_to_one_emp,
sum ( case when rnk = 1 then 1 else 0 end ) as emp_count_with_max_value
from (
SELECT a.alloc_date,
p.plan,
p.emp_id,
a.veh,
a.contri_type,
a.amount,
SUM (a.alloc_qty) AS sum_alloc_qty, -- 1000 funds distributed
SUM (a.alloc_qty * a.amount) AS sum_alloc_value, -- 1000*2 = 2000
COUNT (DISTINCT part_id) AS sum_emp_count, -- 4 employees
MAX (a.alloc_qty * a.amount) AS max_value_to_one_emp, -- 600
dense_rank() over (order by a.alloc_qty desc) rnk -- rank allocation in descending order
FROM alloc a, emp p
WHERE A.alloc_date >= TO_DATE ('20111001', 'YYYYMMDD')
AND a.emp_id = p.emp_id
GROUP BY a.alloc_date,
p.plan,
p.emp_id,
a.veh,
a.contri_type,
a.amount
)
group by alloc_date,
plan,
emp_id,
veh,
contri_type,
amount,
sum_alloc_qty,
sum_alloc_value,
sum_emp_count,
max_value_to_one_emp
ORDER BY alloc_date,
emp_id,
amount
注意:在没有测试数据的情况下,我没有测试过这段代码,我也不保证它没有错误。但原则是合理的。
答案 1 :(得分:1)
你也必须通过alloc_qty进行分组,但是如果你使用它:
count(*) keep (dense_rank last order by A.ALLOC_QTY * A.AMOUNT) over () as "emp_count_with_max_value"
那应该返回与最大值匹配的记录数。因此,集成到您的代码中应该是:
SELECT a.alloc_date,
p.plan,
p.emp_id,
a.veh,
a.contri_type,
a.amount,
SUM (a.alloc_qty) AS sum_alloc_qty, -- 1000 funds distributed
SUM (a.alloc_qty * a.amount) AS sum_alloc_value, -- 1000*2 = 2000
COUNT (DISTINCT part_id) AS sum_emp_count, -- 4 employees
MAX (a.alloc_qty * a.amount) AS max_value_to_one_emp, -- 600
count(*) keep (dense_rank last order by A.ALLOC_QTY * A.AMOUNT) over () as "emp_count_with_max_value" -- Unable to retrieve - idealy answer should be 3 in this example
FROM alloc a, emp p
WHERE A.alloc_date >= TO_DATE ('20111001', 'YYYYMMDD')
AND a.emp_id = p.emp_id
GROUP BY a.alloc_qty,
a.alloc_date,
p.plan,
p.emp_id,
a.veh,
a.contri_type,
a.amount
ORDER BY alloc_date,
emp_id,
amount
您可能还想将sum_emp_count更改为
COUNT (DISTINCT part_id) over ()
和max_value_to_one_emp到
MAX (a.alloc_qty * a.amount) over ()
否则,您将无法获得所有数据集的值。