Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql oracle中求差求差的逻辑_Sql_Oracle - Fatal编程技术网

Sql oracle中求差求差的逻辑

Sql oracle中求差求差的逻辑,sql,oracle,Sql,Oracle,如何编写逻辑以获得以下输出 with b2 as ( select COUNT(SaLE) As cnt, TO_CHAR(date,'YYYY-MON') As Period from Order where date between date '2020-02-01' and date '2020-02-28' group by TO_CHAR(BATCH_DTE_CYMD,'YYYY-MON') union all select COUNT(Sale) As cnt, T

如何编写逻辑以获得以下输出

with b2 as
(
select COUNT(SaLE) As cnt, 
TO_CHAR(date,'YYYY-MON') As Period

  from Order
  where date between date '2020-02-01' and date '2020-02-28'
group by TO_CHAR(BATCH_DTE_CYMD,'YYYY-MON') 

union all
select  COUNT(Sale) As cnt,
TO_CHAR(Date,'YYYY-MON') As Period

  from Order
  where date between date '2020-01-01' and date '2020-01-31'
group by TO_CHAR(Date,'YYYY-MON') 

)
select  cnt, Period,
       100*(cnt-lag(cnt,1,cnt) over (order by period))
       /lag(cnt,1,cnt) over (order by period)
       as "variance(%)"
  from b2
 order by period 
我得到这个输出

Cnt   | period     |    variance(%)
11917 | 2020-FEB   |    0
11707 | 2020-JAN   |    -1.76218847025258034740286984979441134514
但是我想要这个输出

Cnt   | period     |    variance(%)                                               | sign                                                                                
11917 | 2020-FEB   |    JAN-FEB (Variance we get in feb in % (with no decimal))   | Increase/decrease 
11707 | 2020-JAN   |    0                                                         |  0
请尝试以下查询:

select cnt,period,
case when variance<>0 then 
'JAN-FEB(Variance we get in feb in '||to_char(round(variance,0))||')' 
else to_char(variance) end as variance,
case when SIGN (variance)<0 then 'Decrease' 
when sign(variance)=0 then 'No Change' else 'Increase' 
end as sign  from(select  cnt, Period,
       100*(cnt-lag(cnt,1,cnt) over (order by period desc ))
       /lag(cnt,1,cnt) over (order by period desc)
       as variance
  from test
)t
order by period
选择cnt,句点,
如果差异为0,则为
“1月-2月(我们在2月得到的方差为”| |到| char(四舍五入(方差,0))| |”)”
否则以字符(差异)结尾为差异,

当符号(方差)时,代码的问题是,您在
CHAR
列(
PERIOD
)上使用了
LAG
,这是不正确的,因为当比较在字符串中时,2020-FEB低于2020-JAN

必须将它们用作
LAG
函数中的
date
,如下所示:

WITH B2 AS (
    SELECT COUNT(SALE) AS CNT,
           TRUNC(DATE, 'MON') AS PERIOD
      FROM ORDER
     WHERE DATE BETWEEN DATE '2020-01-01' AND DATE '2020-02-28'
    GROUP BY TRUNC(DATE, 'MON')
)
SELECT
    CNT,
    TO_CHAR(PERIOD,'YYYY-MON') AS PERIOD,
    100 * ( CNT - LAG(CNT, 1, CNT) OVER( ORDER BY PERIOD ) ) 
    / LAG(CNT, 1, CNT) OVER(ORDER BY PERIOD) 
    AS "variance(%)"
FROM B2
ORDER BY PERIOD
干杯

请使用问题标签下方的
edit
按钮编辑您的问题,并在查询中包含表格的数据,以及您希望该数据产生的结果。谢谢