<wbr></wbr>
如果您立志于成为软件开发高手,或者想通过本文来检验一下您是否为软件开发高手。请阅读下文,否则请绕行。
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
软件开发高手必须掌握的4大SQL精髓语句(一)
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>马根峰
( 广东联合电子收费股份有限公司, 广州 510300)
<wbr></wbr>
作者博客:
CSDN博客:http://blog.csdn.net/magenfeng
新浪博客:<wbr><a href="http://blog.sina.com.cn/magenfeng"><span style="font-size:12px; color:#96b0af">http://blog.sina.com.cn/magenfeng</span></a></wbr>
QQ空间:<wbr><a href="http://user.qzone.qq.com/630414817/main"><span style="font-size:12px; color:#96b0af">http://user.qzone.qq.com/630414817/main#</span></a>!</wbr>
<wbr></wbr>
<wbr></wbr>
0<wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>引言 <wbr></wbr>
随着计算机在社会各领域应用的深入,软件开发人员不得不面临着或多或少的数据处理、数据库访问。
在SQL(Structured Query Language)语言DML中,有四大语句堪称其精髓部分。下面就讲述一下其中之一, Case When 语句的用法。本文介绍用它实现数据仓库中的维度转换,来将数据展现给分析人员来进行决策支持与分析。
<wbr><wbr><wbr><wbr><wbr><wbr> CASE WHEN<wbr> Conition <wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
THEN <wbr><wbr><wbr>。。。</wbr></wbr></wbr>
ELSE<wbr><wbr><wbr><wbr> 。。。</wbr></wbr></wbr></wbr>
END
<wbr></wbr>
<wbr></wbr>
1<wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>数据仓库中的维度转换
在数据仓库数据展现中,我们通常要用到将数据表进行维度转换。如数据仓库中某一关系模式
Vehsm (Roadno, Year , Fluxcount,。。。)
对于数据分析人员来说,他们更想了解某一条高速公路在一段时间内车流量的变化情况,那最好就是将关系表 TB_ADFLUXCOLT 以下面的关系模式来展现
Vehcount_Trans (Roadno, Year Fluxcount 1, .。。。, Year Fluxcount n)
<wbr></wbr>
下面笔者就分别以Case When语句、BrioQuery来分别演示一下如何实现维度转换。
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
2<wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>Case When语句来实现维度转换
下面就以DB2数据库为例,以“万能数据库查询分析器” 中文版本《DB 查询分析器》、英文版本《DB Query Analyzer》作为客户端工具来来演示一下Case When 语句如何实现维度转换。
<wbr></wbr>
<wbr></wbr>
selectuu.roadno,rr.roadname,
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>sum(casewhen uu.year=2009then uu.FLUXCOUNTelse 0 end)<wbr></wbr> asyear_2009,
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>sum(casewhen uu.year=2010then uu.FLUXCOUNTelse 0 end)<wbr></wbr> asyear_2010,
<wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr>sum(casewhen uu.year=2011then uu.FLUXCOUNTelse 0 end)<wbr></wbr> asyear_2011
from
(
<wbr><wbr><wbr></wbr></wbr></wbr>select roadno,year(squaddate)as year,sum(FLUXCOUNT)as FLUXCOUNT
<wbr><wbr><wbr></wbr></wbr></wbr>from TB_ADFLUXCOLT<wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr>where<wbr></wbr> squaddatebetween '2009-01-01'and '2011-12-31'and oiflag=0
<wbr><wbr><wbr></wbr></wbr></wbr>group by roadno,year(squaddate)
) uu,<wbr></wbr>
(
<wbr><wbr><wbr></wbr></wbr></wbr>select distinct<wbr></wbr> roadno,roadname
<wbr><wbr><wbr></wbr></wbr></wbr>from tb_road<wbr><wbr></wbr></wbr>
<wbr><wbr><wbr></wbr></wbr></wbr>where status=0and level in (0,1)<wbr></wbr> androadname not like '%南部快速%'
) rr
whereuu.roadno=rr.roadno<wbr></wbr>
groupby uu.roadno,rr.roadname
withur;
<wbr></wbr>
<wbr></wbr>
<wbr><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b70463f01012x9x&url=http://s13.sinaimg.cn/orignal/4b70463fgc967aa33b67c" target="_blank"></a><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr> 图1<wbr><wbr><wbr> DB 查询分析器中输入要执行的Case When语句</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>图2<wbr><wbr><wbr> DB 查询分析器<wbr> 执行Case When语句</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b70463f01012x9x&url=http://s3.sinaimg.cn/orignal/4b70463fgc967b2bcc592" target="_blank"></a><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>图3<wbr><wbr><wbr> DB Query Analyzer 中输入要执行的Case When语句</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b70463f01012x9x&url=http://s1.sinaimg.cn/orignal/4b70463fgc967b37fdda0" target="_blank"></a><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>图4<wbr><wbr><wbr> DB Query Analyzer 执行Case When语句</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
3<wbr><wbr><wbr><wbr><wbr><wbr><wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>BrioQuery来实现维度转换
在数据仓库工具中,BrioQuery提供了比较强大的数据展现功能,能方便地实现上面的维度转换。
<wbr></wbr>
<wbr><a href="http://photo.blog.sina.com.cn/showpic.html#blogid=4b70463f01012x9x&url=http://s1.sinaimg.cn/orignal/4b70463fgc967b7192230" target="_blank"></a><br><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>图5<wbr><wbr><wbr> BrioQuery<wbr><wbr> 数据表 Vehsm</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>图6<wbr><wbr><wbr> BrioQuery<wbr><wbr> 执行查询后的结果</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr><wbr>图7<wbr><wbr><wbr> BrioQuery<wbr><wbr> 维度转换后的结果</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
<wbr></wbr>
但对于大数据量表的维度转换来说,则几乎无法完成,笔者在中文核心刊物《电信科学》2003年11期《广东电信公话200专用话机话务动态分析系统的构建》文章中,详细地介绍了“广东电信公话200专用话机话务动态分析系统”中是如何实现这一点的,这个话务动态系统是自己以前在广东省电信有限公司工作的时候独自开发的,有兴趣的话,可以看
<wbr></wbr>
本人的CSDN博客的博文:
“广东电信公话200专用话机话务动态分析系统的构建”
<wbr></wbr>http://blog.csdn.net/magenfeng/article/details/7691403