计算查询中的正值数

时间:2014-10-12 08:39:45

标签: sql oracle

我正在处理以下查询和表格

SELECT dd.actual_date, dd.week_number_overall, sf.branch_id, AVG(sf.overtarget_qnt) AS targetreach
FROM sales_fact sf, date_dim dd 
WHERE dd.date_id = sf.date_id
AND dd.week_number_overall BETWEEN 88-2 AND 88
AND sf.branch_id = 1
GROUP BY dd.actual_date, branch_id, dd.week_number_overall
ORDER BY dd.actual_date ASC;

ACTUAL_DATE WEEK_NUMBER_OVERALL  BRANCH_ID TARGETREACH
----------- ------------------- ---------- -----------
13/08/14                     86          1         -11 
14/08/14                     86          1          12 
15/08/14                     86          1        11.8 
16/08/14                     86          1         1.4 
17/08/14                     86          1        -0.2 
19/08/14                     86          1         7.2 
20/08/14                     87          1        16.6 
21/08/14                     87          1        -1.4 
22/08/14                     87          1        14.4 
23/08/14                     87          1         2.8 
24/08/14                     87          1          18 
26/08/14                     87          1        13.4 
27/08/14                     88          1        -1.8 
28/08/14                     88          1        10.6 
29/08/14                     88          1         7.2 
30/08/14                     88          1          14 
31/08/14                     88          1         9.6 
02/09/14                     88          1        -3.2 

“TargetReach”列显示目标是否已到达。 负值表示当天未达到目标。 如何计算此查询的正值ROW数?

将显示如下内容:

TOTAL_POSITIVE_TARGET_REACH WEEK_NUMBER_OVERALL
--------------------------- ------------------
13                          88

我试过使用CASE但仍然无法正常工作。 非常感谢。

5 个答案:

答案 0 :(得分:3)

您想使用条件聚合:

with t as (
      <your query here>
     )
select week_number_overall, sum(case when targetreach > 0 then 1 else 0 end)
from t
group by week_number_overall;

但是,我会重写您的原始查询以使用正确的join语法。然后查询看起来像:

SELECT week_number_overall,
       SUM(CASE WHEN targetreach > 0 THEN 1 ELSE 0 END)
FROM (SELECT dd.actual_date, dd.week_number_overall, sf.branch_id, AVG(sf.overtarget_qnt) AS targetreach
      FROM sales_fact sf JOIN
           date_dim dd 
           ON dd.date_id = sf.date_id
      WHERE dd.week_number_overall BETWEEN 88-2 AND 88 AND sf.branch_id = 1
      GROUP BY dd.actual_date, branch_id, dd.week_number_overall
     ) t
GROUP BY week_number_overall
ORDER BY week_number_overall;

CTE(第一个解决方案)和子查询之间的差异(在这种情况下)只是一个偏好问题。

答案 1 :(得分:1)

SELECT WEEK_NUMBER_OVERALL, COUNT(*) TOTAL_POSITIVE_TARGET_REACH
FROM (your original query)
WHERE TARGETREACH >= 0
GROUP BY WEEK_NUMBER_OVERALL

答案 2 :(得分:0)

select sum( decode( sign( TARGETREACH ) , -1 , 0 , 0 , 0 , 1 , 1 ) ) 
from ( "your query here" ); 

答案 3 :(得分:0)

使用HAVING条款

SELECT dd.actual_date, dd.week_number_overall, sf.branch_id, AVG(sf.overtarget_qnt) AS targetreach
FROM sales_fact sf, date_dim dd 
WHERE dd.date_id = sf.date_id
AND dd.week_number_overall BETWEEN 88-2 AND 88
AND sf.branch_id = 1
GROUP BY dd.actual_date, branch_id, dd.week_number_overall
HAVING AVG(sf.overtarget_qnt)>0
ORDER BY dd.actual_date ASC;

答案 4 :(得分:0)

使用decode(),sign()获得正数和&amp;负数。

drop table test;

create table test (
key number(5),
value number(5));

insert into test values ( 1, -9 );
insert into test values ( 2, -8 );

insert into test values ( 3, 10 );
insert into test values ( 4, 12 );

insert into test values ( 5, -9 );
insert into test values ( 6, 8 );
insert into test values ( 7, 51 );

commit;

select sig , count ( sig ) from
(
select key,  ( (decode( sign( value ) , -1 , '-ve' , 0 , 'zero' , 1 , '+ve' ) ) ) sig
from test 
)
group by sig



SIG  COUNT(SIG)             
---- ---------------------- 
+ve  4                      
-ve  3