sql的高级问题

[复制链接]
查看11 | 回复3 | 2010-10-20 09:38:00 | 显示全部楼层 |阅读模式
create table cp
(
cpmc varchar(20),
cpbh varchar(20),
cprq varchar(20),
cpsj int
)
go
insert into cp values('bike','001','20100123',120)
insert into cp values('bike','001','20100223',300)
insert into cp values('bike','001','20100323',100)
insert into cp values('bike','001','20100120',80)
insert into cp values('shoe','002','20100423',100)
insert into cp values('shoe','002','20101120',80)
go
selectcpmc,cpbh
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201001%') as '1月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201002%') as '2月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201003%') as '3月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201004%') as '4月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201005%') as '5月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201006%') as '6月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201007%') as '7月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201008%') as '8月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201009%') as '9月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201010%') as '10月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201011%') as '11月'
,(select case when sum(cpsj) is NULL then '0' else sum(cpsj) end from cp where cp.cpbh=t.cpbh and cp.cpmc=t.cpmc and cprq like '201012%') as '12月'
from cp as t
group by cpmc,cpbh
请问一下老师cp.cpbh=t.cpbh and cp.cpmc=t.cpmc取到了是什么作用呢?
既然有cp as t
那么cp.cpbh=t.cpbh and cp.cpmc=t.cpmc不是自已等于自己吗?
还有如果没有cp.cpbh=t.cpbh and cp.cpmc=t.cpmc,就会出现重复的,
创建的语句我都已在上面,希望老师也运行一下,看一下效果
至于原因,不是很明白,请高手指教。

回复

使用道具 举报

千问 | 2010-10-20 09:38:00 | 显示全部楼层
你学过表的自连接吗?就是表自己连接自己,这个好理解吧?还有你学过主键吗?又叫主关键词。主关键词可能不是由一个组成的,而是由多个,这时遇到这样的表连接时必须让主键进行全连接的,所以中间使用and,也就是说这个表可能是一个纽带表,不像常规的关键字只由一个属性组成,而由两个组成时,这样的两个表在连接时必须使用and分别进行连接。在SQL中你可以选中两个字段,然后点设置主关键字的按钮(就是那个钥匙),两个这样表连接时使用ANSI语句时就应该当在where 中用and进行分别的连接。也就是说该表的主键是(cpbh,cpmc),而不是其中的一个。使用Tran-SQL时在on后用一个and进行连接。
回复

使用道具 举报

千问 | 2010-10-20 09:38:00 | 显示全部楼层
把里面的表和外面的表当成两个表来理解等同下面语句 selectcpmc,cpbh,sum( case whencprq like '201001%' then cpsj else 0 end) as '1月',sum( case whencprq like '201002%' then cpsj else 0 end) a
回复

使用道具 举报

千问 | 2010-10-20 09:38:00 | 显示全部楼层
这是自连接,你把cp和t当成列相同的两个表进行理解,就容易明白了。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

主题

0

回帖

4882万

积分

论坛元老

Rank: 8Rank: 8

积分
48824836
热门排行