oracle查询余额
oracle query balance
我想select最近两天(今天和昨天)的余额,代码看起来像这样
select a.balance as "today",b.balance as "yesterday"
from account a,account b
where a.id='xxx'
and a.id=b.id
and a.dates=to_date(sysdate, 'DD-MM-YYYY') --today
and b.dates=to_date(sysdate-1, 'DD-MM-YYYY') --yesterday
今天的数据还没有输入,问题就来了。
即使昨天的数据可用,这个结果还是两个余额为空
注意:我目前的解决方案是将查询拆分为 2 个。但我希望有任何方法可以只使用 1 个查询
预期输出
-----------------
|today|yesterday|
-----------------
|null |9000 |
-----------------
数据
--------------------------
|id |balance |dates |
--------------------------
|1 |9000 |6/5/2015|
--------------------------
我想你是在要求昨天 OUTER JOINED 到今天。
select a.balance as "today",b.balance as "yesterday"
from account a,account b
where b.id='xxx'
and a.id(+) =b.id -- <----Here
and a.dates(+)=to_date(sysdate, 'DD-MM-YYYY') --today
and b.dates=to_date(sysdate-1, 'DD-MM-YYYY') --yesterday
用更现代的说法,我对那种旧语法有点生疏
select a.balance as "today",b.balance as "yesterday"
from account b LEFT OUTER JOIN account a on
a.id=b.id and a.dates=to_date(sysdate, 'DD-MM-YYYY') --today
where b.id='xxx'
and b.dates=to_date(sysdate-1, 'DD-MM-YYYY') --yesterday
请注意我是如何将可选 table (a) 的检查移动到连接条件中的,所以无论如何它都会遇到。
不需要连接两个表,如果我们允许硬编码两个日期
--code with sysdate
with tab as --dummy data
(
select 1 id,sysdate -level+1 dat,level*1000 balance
from dual
connect by level <=10
)
--main query
select max(decode(trunc(dat),trunc(SYSDATE),balance)) "today"
,max(decode(trunc(dat),trunc(SYSDATE-1),balance)) "yesterday"
from tab t where TRUNC(t.dat) IN (TRUNC(SYSDATE),TRUNC(SYSDATE-1));
--code without sysdate
with tab as
(
select 1 id,sysdate -level dat,level*1000 balance
from dual
connect by level <=10
)
--main query
select max(decode(trunc(dat),trunc(SYSDATE),balance)) "today"
,max(decode(trunc(dat),trunc(SYSDATE-1),balance)) "yesterday"
from tab t where TRUNC(t.dat) IN (TRUNC(SYSDATE),TRUNC(SYSDATE-1));
select
max(decode(trunc(dates),trunc(SYSDATE),balance)) "today"
,max(decode(trunc(dates),trunc(SYSDATE-1),balance)) "yesterday"
from account a
where a.id='xxx'
and trunc(a.dates) IN (trunc(sysdate),trunc(sysdate-1));
无需加入,使用LAG
功能追踪上一个
如果您想了解滞后功能。请访问下方link。
http://www.techonthenet.com/oracle/functions/lag.php。
我已将以下内容作为输入。
并使用自动跟踪前一行的滞后执行下面的查询。
SELECT * FROM(
SELECT ID,LAG(BALANCE) OVER (ORDER BY DATES) AS YESTERDAY_BALANCE,BALANCE AS TODAYS_BALANCE
FROM ACCOUNTS)
WHERE YESTERDAY_BALANCE IS NOT NULL;
我得到的输出如下。如果您仍然无法获取今天的数据,它将显示该行。
我想select最近两天(今天和昨天)的余额,代码看起来像这样
select a.balance as "today",b.balance as "yesterday"
from account a,account b
where a.id='xxx'
and a.id=b.id
and a.dates=to_date(sysdate, 'DD-MM-YYYY') --today
and b.dates=to_date(sysdate-1, 'DD-MM-YYYY') --yesterday
今天的数据还没有输入,问题就来了。 即使昨天的数据可用,这个结果还是两个余额为空
注意:我目前的解决方案是将查询拆分为 2 个。但我希望有任何方法可以只使用 1 个查询
预期输出
-----------------
|today|yesterday|
-----------------
|null |9000 |
-----------------
数据
--------------------------
|id |balance |dates |
--------------------------
|1 |9000 |6/5/2015|
--------------------------
我想你是在要求昨天 OUTER JOINED 到今天。
select a.balance as "today",b.balance as "yesterday"
from account a,account b
where b.id='xxx'
and a.id(+) =b.id -- <----Here
and a.dates(+)=to_date(sysdate, 'DD-MM-YYYY') --today
and b.dates=to_date(sysdate-1, 'DD-MM-YYYY') --yesterday
用更现代的说法,我对那种旧语法有点生疏
select a.balance as "today",b.balance as "yesterday"
from account b LEFT OUTER JOIN account a on
a.id=b.id and a.dates=to_date(sysdate, 'DD-MM-YYYY') --today
where b.id='xxx'
and b.dates=to_date(sysdate-1, 'DD-MM-YYYY') --yesterday
请注意我是如何将可选 table (a) 的检查移动到连接条件中的,所以无论如何它都会遇到。
不需要连接两个表,如果我们允许硬编码两个日期
--code with sysdate
with tab as --dummy data
(
select 1 id,sysdate -level+1 dat,level*1000 balance
from dual
connect by level <=10
)
--main query
select max(decode(trunc(dat),trunc(SYSDATE),balance)) "today"
,max(decode(trunc(dat),trunc(SYSDATE-1),balance)) "yesterday"
from tab t where TRUNC(t.dat) IN (TRUNC(SYSDATE),TRUNC(SYSDATE-1));
--code without sysdate
with tab as
(
select 1 id,sysdate -level dat,level*1000 balance
from dual
connect by level <=10
)
--main query
select max(decode(trunc(dat),trunc(SYSDATE),balance)) "today"
,max(decode(trunc(dat),trunc(SYSDATE-1),balance)) "yesterday"
from tab t where TRUNC(t.dat) IN (TRUNC(SYSDATE),TRUNC(SYSDATE-1));
select
max(decode(trunc(dates),trunc(SYSDATE),balance)) "today"
,max(decode(trunc(dates),trunc(SYSDATE-1),balance)) "yesterday"
from account a
where a.id='xxx'
and trunc(a.dates) IN (trunc(sysdate),trunc(sysdate-1));
无需加入,使用LAG
功能追踪上一个
如果您想了解滞后功能。请访问下方link。
http://www.techonthenet.com/oracle/functions/lag.php。
我已将以下内容作为输入。
并使用自动跟踪前一行的滞后执行下面的查询。
SELECT * FROM(
SELECT ID,LAG(BALANCE) OVER (ORDER BY DATES) AS YESTERDAY_BALANCE,BALANCE AS TODAYS_BALANCE
FROM ACCOUNTS)
WHERE YESTERDAY_BALANCE IS NOT NULL;
我得到的输出如下。如果您仍然无法获取今天的数据,它将显示该行。