从下面的sql中,我能够将输出作为状态和AVG_PROD_COUNT。
我的问题是,当account_num =' A128'或者' A126',我需要使用
" ( dpc.product_id = cpp.product_id或者dpc.product_id = 999)"。我尝试使用案例陈述。我是新来的sql无法获得output.can任何一个请帮助此
account_num
pakage_id
PRODUCT_ID
SELECT avg.status,
SUM(avg.AVG_PROD_COUNT) AVG_PROD_COUNT
FROM
(SELECT
DECODE(dpc.product_id,999,'SUSPEND','ACTIVE') status,
dpc.product_id,
dpc.package_id,
ROUND(SUM(dpc.product_count)/ (to_date( '$billDate' ,'YYYYMMDD') - add_months(to_date( '$billDate' ,'YYYYMMDD'), -1)), 2) AVG_PROD_COUNT
FROM dailyproductcount dpc,
cust_pkg_prod cpp
WHERE dpc.rating_acct_nbr = cpp.account_num
AND dpc.rating_acct_nbr not in('A128','A126')
AND dpc.activity_dat BETWEEN add_months(to_date( '$billDate' ,'YYYYMMDD'), -1) AND to_date( '$billDate' ,'YYYYMMDD')-1
AND (dpc.product_id = cpp.package_id
OR dpc.product_id =999)
AND dpc.package_id = cpp.package_id
GROUP BY dpc.product_id,
dpc.package_id
) AVG
答案 0 :(得分:0)
您的逻辑错误或可能是拼写错误 -
(dpc.product_id = cpp.package_id
OR dpc.product_id =999)
AND dpc.package_id = cpp.package_id
在功能上等同于
dpc.package_id = cpp.package_id
这可以防止dpc.product_id =999
导致您的where
子句评估为真。
答案 1 :(得分:0)
上面的sql不能正常工作,因为没有" GROUP BY avg.status"在父查询中
据我所知,你在WHERE条件下需要这样的东西(我假设你使用的是Oracle):
...
在哪里dpc.rating_acct_nbr = cpp.account_num
AND dpc.activity_dat BETWEEN add_months(to_date(' $ billDate',' YYYYMMDD'), - 1)和to_date(' $ billDate',' YYYYMMDD& #39;) - 1个
AND dpc.package_id = cpp.package_id
AND(dpc.product_id = 999 OR dpc.product_id = decode(dpc.rating_acct_nbr,' A126',cpp.product_id,' A128',cpp.product_id,cpp.package_id ))
...
如果你想使用CASE,条件
" product_id = decode(dpc.rating_acct_nbr,' A126',cpp.product_id,' A128',cpp.product_id,cpp.package_id)"看起来像是:
product_id = case when dpc.rating_acct_nbr in ('A126', 'A128') then cpp.product_id else cpp.package_id end