首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > oracle >

急请教怎么修改表的结构

2012-03-27 
急!!!请问如何修改表的结构本来有这么一个表DateCountLogCountIndividual02-Jun-065103-Jun-06112........

急!!!请问如何修改表的结构
本来有这么一个表
Date                     CountLog               CountIndividual
02-Jun-06                 5                                   1
03-Jun-06               11                                   2
.....
...
.
12-Dec-06                 23                               5

我想修改成这样的结构
Day       JunCountLog   JunCountInd   JulCountLog   JulCountInd   SepCountLog......
1                   5                           1                       23                     3                     15     ....
2                 .............................................
3                 ......................
..
31                 ....

在建立新表结构以后,请问我如何把数据从第一个表转移到第二个表。请给出相关的SQL语句,谢谢!!!

[解决办法]
先分列(每列中是自己月的取实际值不是的取0),然后求和
selelct date,sum(JunCountLog) as JunCountLog, sum(JunCountInd) as JunCountInd
sum(JulCountLog) as JulCountLog, sum(JulCountInd) as JulCountInd
...
from
(
select to_number(to_char(date, 'dd ')) as Day,
decode(to_char(date, 'mm '), '06 ',CountLog ,0) as JunCountLog ,
decode(to_char(date, 'mm '), '06 ',CountIndividual ,0) as JunCountInd ,
decode(to_char(date, 'mm '), '07 ',CountLog ,0) as JulCountLog ,
decode(to_char(date, 'mm '), '06 ',CountIndividual ,0) as JulCountLog ,
...
from table
)
group by date
[解决办法]
打错了一个字段名,应该这样:
selelct Day,sum(JunCountLog) as JunCountLog, sum(JunCountInd) as JunCountInd
sum(JulCountLog) as JulCountLog, sum(JulCountInd) as JulCountInd
...
from
(
select to_number(to_char(date, 'dd ')) as Day,
decode(to_char(date, 'mm '), '06 ',CountLog ,0) as JunCountLog ,
decode(to_char(date, 'mm '), '06 ',CountIndividual ,0) as JunCountInd ,
decode(to_char(date, 'mm '), '07 ',CountLog ,0) as JulCountLog ,
decode(to_char(date, 'mm '), '06 ',CountIndividual ,0) as JulCountLog ,
...
from table
)
group by date

[解决办法]
倒,上万个记录是小记录集,不上10w一般都不算大;
decode的目的是先把每行仅有的几列分解为按月份区分的多列,符合的列取本月值,不符合本行信息的列取值为0
decode()过滤后的结果集:
Day JunCountLog JunCountInd JulCountLog JulCountInd SepCountLog......
6 5 1 0 0 0 ....
6 0      0     23    3     0 ...
7 ......................
.....
31 ....
求和是为了把本来应该在一行但是被分在了多行的值合并在一行上,如从1月1日到12月1日都有记录,那么上一流程会生成12行信息,但每行只有对应月份字段有值其他字段为0,分组求和后也就把12个月所有1号行都合成了一行,而且每个月的值都是准确的,如:


求和后结果
Day JunCountLog JunCountInd JulCountLog JulCountInd SepCountLog......
6 5 1 23 3 0 ....
7 ......................
.....
31 ....
如果再不理解,建议找本离散数学或者算法方面的基础书籍去看一看

热点排行