1、to_char()
select * from
(select
to_char(t.column,'FM99999990.99'), ....
from table t ) s
order by s.column
先to_char 后再排序,它是按字符排序。99> 199 因为9先和1比较。
所以应该先排序后to_char()
select to_char(s.column,'FM99999990.99'), ....
from
(select *
from table t
order by t.column
) s
to_char(s.punctualnum * 100 /s.declarablenum, 'FM990.00')||'%'
如查小于1%,0.72%
to_char(s.punctualnum * 100 /s.declarablenum, 'FM999.00')||'%'
如查小于1%,.72%
to_char(s.declarablenum,'FM099990.00'),
如查位数不够 加0 例如 030466.00, 001238.00
2、to_date()
select * from tb_bil_xxxx t
where t.pcdate >=to_date('2005-04-29','yyyy-mm-dd')
and t.pcdate <to_date('2005-04-30','yyyy-mm-dd')+1
查出一个月的数据。
若t.pcdate <=to_date('2005-04-30','yyyy-mm-dd')
2005-04-30:00:00:01以后的数据都查不出。
select to_date('200505','yyyy-mm')+1 from dual 它的值是 2005-5-2 它是加一天而不是加一个月
3、 decode() 与 case when
select case when r.taxpayerxx = '10' then r.taxpayerxx end from
tb_reg_taxpayerxxxx r
where r.taxpayerxx in ('10','20')
and decode({&fundTaxCode},0,decode(t.fundflag,0,2,3),1,2) = 2
group by rollup(substr(t.orgdeptcode, 1, (3 + 2 * {&orglevel})))
fundTaxCode 它传的值是0或1.
只要能用DECODE的語句都可以轉換為CASE WHEN,而CASE WHEN 語句則不一定可以轉換為DECODE
但是如果当比较条件不固定时,对区间0-10,10-50,50以上,显然只能用 case when。
case when 中不写esle 将设为空。
group by rollup 它是增加合计行。
4、count() 和 sum()
select t6.orgdeptcode,
sum(t1.totalamount),
count(decode(t1.tempinvoicexxx, 3, t1.carriertaxregcodexxx)),
count(decode(t1.tempinvoicexxx, 3, t1.invoicexxx)),
sum(decode(t1.tempinvoicexxx, 3, t1.totalaxxx)),
from TB_Lvy_TempxxxData t1
where t1.opdate >= to_date('2008-03-01', 'yyyy-mm-dd')
and t1.opdate <= to_date('2008-04-01', 'yyyy-mm-dd')
group by t6.orgdeptcode;
若把 count(decode(t1.tempinvoicexxx, 3, t1.carriertaxregcodexxx))改 为
count(decode(t1.tempinvoicexxx, 3, t1.carriertaxregcodexxx,0))
没有tempinvoicexxx=3 也会统计记录。
count 是统计记录,tempinvoicexxx!=3 把它变成 0,就也成了统计全部记录
也可以改为
sum(decode(t1.tempinvoicexxx, 3, 1,0))
select count(t.taxtypexxx || t.budgetitemxxx) from tc_pub_taxxxx t
where t.taxtypexxx like '03'
group by t.taxtypexxx
order by t.taxtypexxx
统计 taxtypexxx='03'的记录。
select count( distint t.taxtypexxx || t.budgetitemxxx) from tc_pub_taxxxx t
where t.taxtypexxx like '03'
group by t.taxtypexxx
order by t.taxtypexxx
统计 t.taxtypexxx || t.budgetitemxxx 不相等的记录。