select a.riqi,
a.yinghuisum,
a.yinghuiMoney,
b.shishousum,
b.shishouMoney,
c.notReturnsum,
c.notReturnMoney
from (select TO_DATE(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') as riqi,
count(t.flowid) as yinghuisum /*应回款订单数*/,
sum(o.ordersum) as yinghuiMoney
from ordersinfo o, postdealinfo t
where o.orderid = t.orderid
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') >=
TO_DATE('2009-01-01 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') <=
TO_DATE('2009-08-29 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and not exists
(select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and (sta.lststatu = '11' or sta.lststatu = '19'))
group by substr(t.postdealtime, 0, 8)) a
left join (select TO_DATE(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') as riqi,
count(t.flowid) as shishousum /*实回款单数*/,
sum(o.ordersum) as shishouMoney
from ordersinfo o, postdealinfo t
where o.orderid = t.orderid
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') >=
TO_DATE('2009-01-01 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') <=
TO_DATE('2009-08-29 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and exists (select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and sta.lststatu = '17')
group by substr(t.postdealtime, 0, 8)) b on b.riqi = a.riqi
left join (select TO_DATE(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') as riqi,
count(t.flowid) as notReturnsum /*未回款单数*/,
sum(o.ordersum) as notReturnMoney
from ordersinfo o, postdealinfo t
where o.orderid = t.orderid
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') >=
TO_DATE('2009-01-01 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') <=
TO_DATE('2009-08-29 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and not exists
(select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and (sta.lststatu = '11' or sta.lststatu = '19' or
sta.lststatu = '17'))
group by substr(t.postdealtime, 0, 8)) c on a.riqi = c.riqi
解决方案 »
- oracle的监听小问题,求解!
- oracle10G一个用户指向了两个表空间。
- 【求ORACLE版本下载】
- 为什么SYSAUX可以offline不能read only?
- 如何实现高级复制的单向同步
- Forms里如何读写multi-line的text item
- 求个Oralce 10G的下载地址能用的。。。
- 想问个表中数据更新的问题
- 小弟初学oracle,在win2000下装好后,启动Enterprise Manager,连接时总是说“没有监听器”是怎么会是??
- 我想找关于JOB的详细讲述,但为什么我找了好多电子书都没有阐述,还是我没找到?能给我讲讲吗?
- 求一条 sql语句 急急!
- RAC机器自动重启
count(case when not exists(
select * from orderstatuhisinfo sta
where t.orderid = sta.orderid
and (sta.lststatu = '11' or sta.lststatu = '19'))
then t.flowid end)yinghuisum,
sum(case when not exists(
select * from orderstatuhisinfo sta
where t.orderid = sta.orderid
and (sta.lststatu = '11' or sta.lststatu = '19'))
then o.ordersum end)yinghuiMoney,
count(case when exists (select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and sta.lststatu = '17')
then t.flowid end)shishousum,
sum(case when exists (select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and sta.lststatu = '17')
then o.ordersum end)shishouMoney,
count(case when not exists(select 1
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and sta.lststatu in ('11','19','17'))
then t.flowid end)notReturnsum,
sum(case when not exists(select 1
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and sta.lststatu in ('11','19','17'))
then o.ordersum end)notReturnMoney
from ordersinfo o, postdealinfo t
where o.orderid = t.orderid
and t.postdealtime>=to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime <to_char(to_date('2009-08-29 16:46:15','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
group by substr(t.postdealtime, 0, 8) 先试试这个代码,运行一下比较效率
count和sum里面的条件重复了,还没想到更好的办法
COUNT(CASE WHEN sta.lststatu NOT IN ('11','19') THEN 1 END) AS yinghuisum /*应回款订单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') THEN o.ordersum END) AS AS shishousum,
COUNT(CASE WHEN sta.lststatu = '17' THEN 1 END) AS shishoumoney /*实回款单数*/,
SUM(CASE WHEN sta.lststatu = '17' THEN o.ordersum END) AS AS yinghuimoney,
COUNT(CASE WHEN sta.lststatu NOT IN ('11','19','17') THEN 1 END) AS notreturnsum /*未回款单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') THEN o.ordersum END) AS AS notreturnmoney,
FROM ordersinfo o, postdealinfo t,orderstatuhisinfo sta
WHERE o.orderid = t.orderid
AND t.orderid = sta.orderid
and t.postdealtime>=to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime < to_char(to_date('2009-08-29 16:46:15','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
GROUP BY to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD')
把连接条件改下t.orderid = sta.orderid改成
t.orderid = sta.orderid(+)
再看看
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') THEN 1 ELSE 0 END) AS yinghuisum /*应回款订单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') THEN o.ordersum ELSE 0 END) AS yinghuiMoney,
SUM(CASE WHEN sta.lststatu = '17' THEN 1 ELSE 0 END) AS shishousum /*实回款单数*/,
SUM(CASE WHEN sta.lststatu = '17' THEN o.ordersum ELSE 0 END) AS shishouMoney,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') THEN 1 ELSE 0 END) AS notReturnsum /*未回款单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') THEN o.ordersum ELSE 0 END) AS notReturnMoney,
FROM ordersinfo o, postdealinfo t,orderstatuhisinfo sta
WHERE o.orderid = t.orderid
AND t.orderid = sta.orderid
and t.postdealtime >= to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime <= to_char(to_date('2009-08-29 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
GROUP BY to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD');改了下9楼的,试试吧
SELECT to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') AS riqi,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') AND t.flowid IS NOT NULL THEN 1 ELSE 0 END) AS yinghuisum /*应回款订单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') THEN o.ordersum ELSE 0 END) AS yinghuiMoney,
SUM(CASE WHEN sta.lststatu = '17' AND t.flowid IS NOT NULL THEN 1 ELSE 0 END) AS shishousum /*实回款单数*/,
SUM(CASE WHEN sta.lststatu = '17' THEN o.ordersum ELSE 0 END) AS shishouMoney,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') AND t.flowid IS NOT NULL THEN 1 ELSE 0 END) AS notReturnsum /*未回款单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') THEN o.ordersum ELSE 0 END) AS notReturnMoney
FROM ordersinfo o, postdealinfo t,orderstatuhisinfo sta
WHERE o.orderid = t.orderid
AND t.orderid = sta.orderid
and t.postdealtime >= to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime <= to_char(to_date('2009-08-29 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
GROUP BY to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD');
count(t.flowid) as yinghuisum /*应回款订单数*/,
sum(o.ordersum) as yinghuiMoney
from ordersinfo o, postdealinfo t
where o.orderid = t.orderid
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') >=
TO_DATE('2009-01-01 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') <=
TO_DATE('2009-08-29 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and not exists
(select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and (sta.lststatu = '11' or sta.lststatu = '19'))
group by substr(t.postdealtime, 0, 8);
SELECT to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') AS riqi,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') AND t.flowid IS NOT NULL THEN 1 ELSE 0 END) AS yinghuisum /*应回款订单数*/,
SUM(CASE WHEN sta.lststatu NOT IN ('11','19') THEN o.ordersum ELSE 0 END) AS yinghuiMoney
--SUM(CASE WHEN sta.lststatu = '17' AND t.flowid IS NOT NULL THEN 1 ELSE 0 END) AS shishousum /*实回款单数*/,
--SUM(CASE WHEN sta.lststatu = '17' THEN o.ordersum ELSE 0 END) AS shishouMoney,
--SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') AND t.flowid IS NOT NULL THEN 1 ELSE 0 END) AS notReturnsum /*未回款单数*/,
--SUM(CASE WHEN sta.lststatu NOT IN ('11','19','17') THEN o.ordersum ELSE 0 END) AS notReturnMoney
FROM ordersinfo o, postdealinfo t,orderstatuhisinfo sta
WHERE o.orderid = t.orderid
AND t.orderid = sta.orderid
and t.postdealtime >= to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime <= to_char(to_date('2009-08-29 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
GROUP BY to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD');把你的sql拆开分成3部分,和这部分执行,然后单条查看是哪些数据不一致。列出3个表的主键和表结构
SELECT to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') AS riqi,
SUM(CASE WHEN sta.lststatu IN ('11','19') THEN 0 ELSE 1 END) AS yinghuisum /*应回款订单数*/,
SUM(CASE WHEN sta.lststatu IN ('11','19') THEN 0 ELSE o.ordersum END) AS yinghuiMoney,
SUM(CASE WHEN sta.lststatu = '17' THEN 1 ELSE 0 END) AS shishousum /*实回款单数*/,
SUM(CASE WHEN sta.lststatu = '17' THEN o.ordersum ELSE 0 END) AS shishouMoney,
SUM(CASE WHEN sta.lststatu IN ('11','19','17') THEN 0 ELSE 1 END) AS notReturnsum /*未回款单数*/,
SUM(CASE WHEN sta.lststatu IN ('11','19','17') THEN 0 ELSE o.ordersum END) AS notReturnMoney
FROM ordersinfo o, postdealinfo t,orderstatuhisinfo sta
WHERE o.orderid = t.orderid
AND t.orderid = sta.orderid(+)
and t.postdealtime >= to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime <= to_char(to_date('2009-08-29 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
GROUP BY to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD');
count(t.flowid) as yinghuisum /*应回款订单数*/
from orderstatuhisinfo sta,ordersinfo o,postdealinfo t
where o.orderid=sta.orderid and t.orderid=o.orderid and
sta.lststatu not in('11','19')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') >=
TO_DATE('2009-01-01 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') <=
TO_DATE('2009-08-29 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
group by substr(t.postdealtime, 0, 8)select TO_DATE(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') as riqi,
count(t.flowid) as yinghuisum /*应回款订单数*/,
sum(o.ordersum) as yinghuiMoney
from ordersinfo o, postdealinfo t
where o.orderid = t.orderid
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') >=
TO_DATE('2009-01-01 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and TO_DATE(substr(t.postdealtime, 0, 14),
'YYYY-MM-DD HH24:MI:SS') <=
TO_DATE('2009-08-29 16:46:14', 'YYYY-MM-DD HH24:MI:SS')
and not exists
(select *
from orderstatuhisinfo sta
where t.orderid = sta.orderid
and (sta.lststatu = '11' or sta.lststatu = '19'))
group by substr(t.postdealtime, 0, 8)
where t.orderid = sta.orderid
and(sta.lststatu = '11' or sta.lststatu = '19'))对于这句来说,满足其要求的可能有:
1.t的orderid在sta表中出现且sta.lststatu != 11 或者sta.lststatu != 19
2.t的orderid没有在sta表出现当修改用上左连接后,第2种情况下查询出来的sta.lststatu就是null另:
23楼的结果是对的吗?
CASE WHEN sta.lststatu IN ('11','19') THEN 0 ELSE 1 END
等价于:
CASE WHEN sta.lststatu NOT IN ('11','19') OR sta.lststatu IS NULL THEN 1 ELSE 0 END
select count(distinct orderid) from sta;
看结果是否相同“sta.lststatu != 11 或者sta.lststatu != 19”应该是:
“sta.lststatu != 11 且sta.lststatu != 19”
COUNT(t.flowid) yinghuisum /*应回款订单数*/,
SUM(o.ordersum) shishousum,
COUNT(CASE WHEN sta.lststatu = '17' THEN t.flowid END) shishoumoney /*实回款单数*/,
SUM(CASE WHEN sta.lststatu = '17' THEN o.ordersum END) yinghuimoney,
COUNT(CASE WHEN sta.lststatu<>'17' THEN t.flowid END) notreturnsum /*未回款单数*/,
SUM(CASE WHEN sta.lststatu<>'17' THEN o.ordersum END) notreturnmoney,
FROM ordersinfo o inner join postdealinfo t
on o.orderid = t.orderid
left join orderstatuhisinfo sta
on t.orderid = sta.orderid
where t.postdealtime>=to_char(to_date('2009-01-01 16:46:14','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and t.postdealtime < to_char(to_date('2009-08-29 16:46:15','YYYY-MM-DD HH24:MI:SS'),'YYYYMMDDHH24MISS')
and (sta.lststatu NOT IN ('11','19') or sta.lststatu is null)
GROUP BY to_date(substr(t.postdealtime, 0, 8), 'YYYY-MM-DD') 这个怎么样
shiyiwan也发现了,嘿嘿
我有事,闪人