问大家一个问题啊``~!
有什么查询句可以做到.按年查询,然后每个月的数据是根据`date1 与date2 来查询
这里有两个条件比如查2009年:1月份 date1``1~30号的数据为1月.之间的数据;
1月份 date2``为20081221至20090120为1月.之间的数据..
如此类推 问有什么方法怎么判断这数据是属于哪个月的`
有什么查询句可以做到.按年查询,然后每个月的数据是根据`date1 与date2 来查询
这里有两个条件比如查2009年:1月份 date1``1~30号的数据为1月.之间的数据;
1月份 date2``为20081221至20090120为1月.之间的数据..
如此类推 问有什么方法怎么判断这数据是属于哪个月的`
1 353 6 7
2 56475 7 7
3 280610 7 7
4 12261 7 8
5 140027 7 7这里查出来的是2009年数据,这里有两个日期字段.
我要的是数据是date1(1~30数据) date2(上月21~此月20)数据.
上面的数据(6`7`8)是属于一个7月的数据如果我现在查2009年的数据`这样会乱了``我现在按哪个字段都会出现差值..
就以第一列月份,date1,date2分别为两种月份算法的统计结果为例
楼主可以进行修改,得到自己想要的结果
select nvl(a.month,b.month)month,a.date1,b.date2 from
(select to_char(datetime,'yyyymm')month,
count(1)date1
from tt
group by to_char(datetime,'yyyymm'))a full join
(select case when to_char(datetime,'dd')<='20' then to_char(datetime,'yyyymm')
else to_char(add_months(datetime,1),'yyyymm') end month,
count(1)date2
from tt
group by case when to_char(datetime,'dd')<='20' then to_char(datetime,'yyyymm')
else to_char(add_months(datetime,1),'yyyymm') end)b
on a.month=b.month
就用我上面的代码进行修改就行了
按年度的话,最外层加上group by substr(month,1,4)
(select to_char(datetime,'yyyymm')month,
count(1)date1
from tt
group by to_char(datetime,'yyyymm'))a
full join
(select case when to_char(datetime,'dd') <='20' then to_char(datetime,'yyyymm')
else to_char(add_months(datetime,1),'yyyymm') end month,
count(1)date2
from tt
group by case when to_char(datetime,'dd') <='20' then to_char(datetime,'yyyymm')
else to_char(add_months(datetime,1),'yyyymm') end)b
on a.month=b.month
用date1的标准对表进行分组--a表
date2的标准对表进行分组--b表
然后对两表根据分别由date1,date2定义的月份进行连接
就能得到你想要的结果
你测试下