数据表tabA数据如下:
日期 数量
2010-11-12 00:01:01 1001
2010-12-12 00:01:01 1010
2010-11-13 00:01:01 1800
2009-10-12 00:01:01 1020
我想计算tabA中日期是" 周 一 "的 数量总和的平均值。
这种sql是否能写出来呢?
日期 数量
2010-11-12 00:01:01 1001
2010-12-12 00:01:01 1010
2010-11-13 00:01:01 1800
2009-10-12 00:01:01 1020
我想计算tabA中日期是" 周 一 "的 数量总和的平均值。
这种sql是否能写出来呢?
create table t1(日期 date, 数量 int);
insert into t1
select to_date('2010-11-12 00:01:01','yyyy-mm-dd hh24:mi:ss') , 1001 from dual union all
select to_date('2010-12-12 00:01:01','yyyy-mm-dd hh24:mi:ss') , 1010 from dual union all
select to_date('2010-11-13 00:01:01','yyyy-mm-dd hh24:mi:ss') , 1800 from dual union all
select to_date('2009-10-12 00:01:01','yyyy-mm-dd hh24:mi:ss') , 1020 from dual;select avg(数量) from t1 where to_char(日期,'D')=1
输入一个日期,
就返回该日期对应的数据,以及该日期是周几的最近周的周几的数值。怎么写sql呢?
比如输入一个日期:2009-01-02 xx:xx:xx
然后我发现这个日期是周1我就返回周1的数值,然后返回上一周的周1的数值。这个能写出来吗?
where to_char(日期,'D') = to_char(to_date('2009-01-12 12:12:12','yyyy-mm-dd hh24:mi:ss'),'D')
union all
select 数量 from t1
where to_char(日期,'yyyy-mm-dd') = to_char(to_date('2009-01-12 12:12:12','yyyy-mm-dd hh24:mi:ss')-7,'yyyy-mm-dd')
from tb
where to_char(日期,'day')='星期一' --去掉就是一周星期几的
group by to_char(日期,'day')
from tb
where 日期=(case
when to_char(to_date('输入日期','yyyy-mm-dd hh24:mi:ss'),'day')='星期一'
then to_date('输入日期','yyyy-mm-dd hh24:mi:ss') end)
or 日期=to_date('输入日期','yyyy-mm-dd hh24:mi:ss')-7
from tb
where 日期=(case
when to_char(to_date('输入日期','yyyy-mm-dd hh24:mi:ss'),'day')='星期一'
then to_date('输入日期','yyyy-mm-dd hh24:mi:ss') end)
or 日期=to_date('输入日期','yyyy-mm-dd hh24:mi:ss')-7