查询给出零输出

query giving zero output

我使用的表格是 retail_str_sales_detail,retail_store_prod,retail_store 实际上是主要查询 此查询正在运行,但执行起来需要更多时间,因此我尝试将该查询更改为更有效的方式

SELECT SET2.PROD_NM,SET2.TherapeuticClass,set2.TOTAL,SET2.QTY as QUANTITY,
set2.MFG as MFG,set2.monthname as MONTHNAME,set2.year as YEAR,
ROUND(((set2.TOTAL/SET3.TOTAL)*100),2) as SHARE
FROM (select
set1.PROD_NM AS PROD_NM,
set1.MFG AS MFG,
set1.monthname AS monthname,
set1.year AS year,
sum(set1.TOTAL) AS TOTAL,
sum(set1.qty) as QTY,
set1.TH_CLASS_1 AS TH_1,
set1.TH_CLASS_2 AS TH_2,
set1.TH_CLASS_3 AS TH_3,
set1.TH_CLASS_4 AS TH_4,
CONCAT(CONCAT(CONCAT(CONCAT( set1.TH_CLASS_1, ','),set1.TH_CLASS_2),','),CONCAT(CONCAT( set1.TH_CLASS_3, ','),set1.TH_CLASS_4) ) as TherapeuticClass
from
(select 
retail_store_prod.TH_CLASS_4 as TH_CLASS_4,
retail_store_prod.TH_CLASS_3 as TH_CLASS_3,
retail_store_prod.MFG as MFG,
retail_store_prod.TH_CLASS_2 as TH_CLASS_2,
retail_store_prod.TH_CLASS_1 as TH_CLASS_1,
retail_store_prod.store_id as store_id ,
retail_store.str_nm,
sum(retail_Str_sales_detail.qty) as qty,
retail_Str_sales_detail.prod_nm  as PROD_NM,
monthname(retail_str_sales_detail.sale_date) as monthname,
year(retail_str_sales_detail.sale_date) as year,
round(sum (retail_Str_sales_detail.total),2) AS TOTAL
from
retail_str_sales_detail ,
retail_store_prod,retail_store
where
retail_store_prod.prod_nm = retail_str_sales_detail.prod_nm and
retail_store_prod.store_id=retail_str_sales_detail.store_id  and
retail_store.store_id = retail_store_prod.store_id
AND retail_store_prod.TH_CLASS_4 != 'NULL' AND retail_store_prod.TH_CLASS_3 != 'NULL'
AND retail_store_prod.TH_CLASS_2 != 'NULL' AND retail_store_prod.TH_CLASS_1 != 'NULL'
AND retail_store_prod.TH_CLASS_4 != '' AND retail_store_prod.TH_CLASS_3 != ''
AND retail_store_prod.TH_CLASS_2 != '' AND retail_store_prod.TH_CLASS_1 != ''
GROUP BY
retail_store_prod.TH_CLASS_4 ,retail_store_prod.TH_CLASS_3 ,retail_store_prod.MFG,retail_str_sales_detail.sale_date,
retail_store_prod.TH_CLASS_2 ,retail_store_prod.TH_CLASS_1,
retail_Str_sales_detail.prod_nm ,retail_store.str_nm,
retail_store_prod.store_id order by retail_Str_sales_detail.prod_nm,
retail_store_prod.TH_CLASS_4 ,retail_store_prod.TH_CLASS_3 ,
retail_store_prod.TH_CLASS_2 ,retail_store_prod.TH_CLASS_1 ,retail_store.str_nm,
round(sum (retail_Str_sales_detail.total),2) desc) as set1
group by set1.PROD_NM,set1.TH_CLASS_1,set1.TH_CLASS_2,set1.TH_CLASS_3,set1.TH_CLASS_4,set1.year,set1.monthname,set1.mfg,
CONCAT(CONCAT(CONCAT(CONCAT( set1.TH_CLASS_1, ','),set1.TH_CLASS_2),','),CONCAT(CONCAT( set1.TH_CLASS_3, ','),set1.TH_CLASS_4) )
order by set1.PROD_NM)  as SET2
FULL OUTER JOIN
(select
sum(set1.TOTAL) AS TOTAL,sum(set1.qty) as QTY,
set1.TH_CLASS_1 AS TH_1,set1.TH_CLASS_2 AS TH_2,
set1.TH_CLASS_3 AS TH_3,set1.TH_CLASS_4 AS TH_4,
CONCAT(CONCAT(CONCAT(CONCAT( set1.TH_CLASS_1, ','),set1.TH_CLASS_2),','),CONCAT(CONCAT( set1.TH_CLASS_3, ','),set1.TH_CLASS_4) ) as TherapeuticClass
from
(select  retail_store_prod.TH_CLASS_4 as TH_CLASS_4,retail_store_prod.TH_CLASS_3 as TH_CLASS_3,
retail_store_prod.TH_CLASS_2 as TH_CLASS_2,retail_store_prod.TH_CLASS_1 as TH_CLASS_1,
retail_store_prod.store_id as store_id ,retail_store.str_nm,sum(retail_Str_sales_detail.qty) as qty,
retail_Str_sales_detail.prod_nm  as PROD_NM,round(sum (retail_Str_sales_detail.total),2) AS TOTAL
from
retail_str_sales_detail ,retail_store_prod,retail_store
where
retail_store_prod.prod_nm = retail_str_sales_detail.prod_nm and
retail_store_prod.store_id=retail_str_sales_detail.store_id  and
retail_store.store_id = retail_store_prod.store_id
AND retail_store_prod.TH_CLASS_4 != 'NULL' AND retail_store_prod.TH_CLASS_3 != 'NULL'
AND retail_store_prod.TH_CLASS_2 != 'NULL' AND retail_store_prod.TH_CLASS_1 != 'NULL'
AND retail_store_prod.TH_CLASS_4 != '' AND retail_store_prod.TH_CLASS_3 != ''
AND retail_store_prod.TH_CLASS_2 != '' AND retail_store_prod.TH_CLASS_1 != ''
GROUP BY
retail_store_prod.TH_CLASS_4 ,retail_store_prod.TH_CLASS_3 ,
retail_store_prod.TH_CLASS_2 ,retail_store_prod.TH_CLASS_1 ,
retail_Str_sales_detail.prod_nm ,retail_store.str_nm,
retail_store_prod.store_id order by retail_Str_sales_detail.prod_nm,
retail_store_prod.TH_CLASS_4 ,retail_store_prod.TH_CLASS_3 ,
retail_store_prod.TH_CLASS_2 ,retail_store_prod.TH_CLASS_1 ,retail_store.str_nm,
round(sum (retail_Str_sales_detail.total),2) desc) as set1
group by set1.TH_CLASS_1,set1.TH_CLASS_2,set1.TH_CLASS_3,set1.TH_CLASS_4,
CONCAT(CONCAT(CONCAT(CONCAT( set1.TH_CLASS_1, ','),set1.TH_CLASS_2),','),CONCAT(CONCAT( set1.TH_CLASS_3, ','),set1.TH_CLASS_4) )
) AS SET3
ON
SET3.TH_1= SET2.TH_1 AND SET3.TH_2 = SET2.TH_2 AND
SET3.TH_3= SET2.TH_3 AND SET3.TH_4 = SET2.TH_4

我把它改成了 在这个查询中,我试图在联合治疗的基础上获得产品的份额 class

SELECT prod_nm
,      th_class_1 || ',' || th_class_2 || ',' ||
       th_class_3 || ',' || th_class_4              AS therapeuticclass
,      total
,      qty
,      mfg
,      yearmonth/100 as year
,      MONTHNAME(TO_DATE(yearmonth*100+1,'YYYYMMDD'))      as monthname 
,      Round(((
        total / SUM(total) OVER(PARTITION BY 
                       th_class_4, th_class_3, th_class_2, th_class_1 )
       )*100),2) AS share 
FROM
(   SELECT
           sd.prod_nm
    ,      sp.mfg    
    ,      sp.th_class_4
    ,      sp.th_class_3
    ,      sp.th_class_2
    ,      sp.th_class_1 
--  ,      sp.store_id      
--  ,      rs.str_nm 
    ,      INTEGER(sd.sale_date)/100  AS yearmonth
    ,      SUM(sd.qty)                AS qty
    ,      SUM(sd.total)              AS total
    FROM        
                retail_str_sales_detail sd 
    INNER JOIN  retail_store_prod       sp  ON  sd.prod_nm  = sp.prod_nm 
                                            AND sd.store_id = sd.store_id
    INNER JOIN  retail_store            rs  ON  rs.store_id = sp.store_id
    WHERE
            sp.th_class_4 NOT IN ('NULL','') 
    AND     sp.th_class_3 NOT IN ('NULL','') 
    AND     sp.th_class_2 NOT IN ('NULL','') 
    AND     sp.th_class_1 NOT IN ('NULL','') 
    GROUP BY
            sd.prod_nm 
    ,       sp.th_class_4
    ,       sp.th_class_3
    ,       sp.th_class_2
    ,       sp.th_class_1 
    ,       sp.mfg
--  ,       sp.store_id 
--  ,       rs.str_nm 
    ,       INTEGER(sd.sale_date)/100 
) s

"zero result" 是指共享列的正确值为 0(而不是返回零行)?好的,所以我猜你的 "total" 列是一个 INTEGER 或 BIGINT,所以我们正在进行整数除法。您的原始代码包含 ROUND() 函数,该函数会将内容转换为 DECIMAL。好的,所以把它放回去。ROUND(SUM(sd.total),2) AS total

有了这些表和数据,查询returns两行,所以查询就OK了。一定是你的数据有问题。

create table retail_str_sales_detail (prod_nm int, store_id int, sale_date date, qty int, total int); create table retail_store_prod (mfg int, prod_nm int, store_id int, th_class_1 char(4), th_class_2 char(4), th_class_3 char(4), th_class_4 char(4)); create table retail_store (store_id int, str_nm varchar(20)); insert into retail_str_sales_detail values (1,1,current date,1,1),(2,1,current date,5,5); insert into retail_store_prod values (1,1,1,1,1,1,1),(1,2,1,1,1,1,1); insert into retail_store values (1,1); 尽管如果您的总计列是一个整数,您将需要将其转换为小数以获得 non-zero 份额计算。例如。 DECIMAL(SUM(sd.total),19,2) AS total