2012-01-09 85 views
0
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 

這裏,現有的查詢工作方式如下。無法從sql查詢中檢索想要的數據

假設,公司發行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

你嘗試了什麼? – 2012-01-09 13:00:40

+0

您的結果中每組有多少行?每個員工有一行嗎? – 2012-01-09 13:05:12

+0

@Scott:我試圖使用count(*)(由MAX分區(a.alloc_qty * a.amount))作爲「emp_count_with_max_value」.....但它給了我錯誤的輸出 – 2012-01-09 13:05:33

回答

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 

注:在沒有測試數據我沒有測試此代碼,我不能保證它是無缺陷。然而,原則是健全的。

+0

:謝謝atleast我知道我該怎麼發起.... ur天才 – 2012-01-10 06:57:24

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() 

否則你不會獲得所有數據集的價值。