请教sql语句

仙侣步惊云 2014-04-28 11:31:37
tbMain:
xm xh gzdw
张三 A011 市招商局
李四 A012 市招商局
王五 A002 市教育局
张小兵 B019 市教育局
tbKh:
xh khnd khdc
A011 2012 优秀
A012 2012 合格
A011 2013 优秀
A012 2013 合格
A002 2013 合格
B019 2013 优秀
要查询这种结果:
xm xh gzdw khnd khdc
张三 A011 市招商局 2014 Null
李四 A012 市招商局 2014 NULL

...全文
192 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
HelloWordGirl 2014-05-04
  • 打赏
  • 举报
回复
declare @tb table( khnd int ) insert @tb select 2014 select A.xm, A.xh, A.gzdw, isnull(cast(B.khnd as varchar(20)),'2014')khnd, isnull(cast(B.khdc as varchar(20)),'NULL')khdc from TB_C A cross join @tb T left join TB_D B on A.xh=B.xh and T.khnd=B.khnd where A.gzdw='市招商局'and T.khnd=2014 可以根据自己的需要修改NuLL值
hmm2005 2014-04-28
  • 打赏
  • 举报
回复
那为什么结果只有两条记录,而不是四条?A002,B019这两个人 2014年度也没有考核,测试数据有没有问题?
山寨DBA 2014-04-28
  • 打赏
  • 举报
回复
引用 2 楼 sxqwhxq 的回复:
没错啊,我要的地明确地显示未考核的年度则显示为空
好,那我怎么去判断某个年份是考核了还是没有考核?
仙侣步惊云 2014-04-28
  • 打赏
  • 举报
回复
没错啊,我要的地明确地显示未考核的年度则显示为空
山寨DBA 2014-04-28
  • 打赏
  • 举报
回复
xm xh gzdw khnd khdc 张三 A011 市招商局 2014 Null 李四 A012 市招商局 2014 NULL 你的这个结果khnd应该不是2014吧。。。猜测你的意思是要做一个leftjoin的,但是那个应该是null呀。
IEEE_China 2014-04-28
  • 打赏
  • 举报
回复



 ---建临时表
if object_id('Tempdb..#tbMain') is not null drop table #tbMain
if object_id('Tempdb..#tbKh') is not null drop table #tbKh
create table #tbMain(
aid int identity(1,1) not null,
xm nvarchar(100) null,
xh nvarchar(100) null,
gzdw nvarchar(100) null
)
create table #tbKh(
bid  int identity(1,1) not null,
xh nvarchar(100) null,
khnd nvarchar(10) null,
khdc nvarchar(10) null
)
---插入测试数据
Insert Into #tbMain
select '张三','A001','市招商局' union all
select '李四','A002','市招商局' union all
select '王五','B001','市教育局' union all
select '张兵','B002','市教育局'

Insert Into #tbKh
select 'A001','2012','优秀' union all
select 'A002','2012','合格' union all
select 'A001','2013','优秀' union all
select 'B001','2012','优秀' union all
select 'B002','2012','合格' union all
select 'B001','2013','优秀' union all
select 'B002','2013','合格'

----开始查询
;with cte(khnd) as(
select '2012'   union all
select '2013'
) 
select s.xm,s.xh,s.gzdw,z.khnd,z.khdc
from  #tbMain s cross join cte t        
left join #tbKh z on s.xh=z.xh and t.khnd=z.khnd
where s.gzdw='市招商局' and t.khnd='2013'


---------------------------------
--结果

(4 行受影响)

(7 行受影响)
xm                                                                                                   xh                                                                                                   gzdw                                                                                                 khnd       khdc
---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------------------------------------------------------------------------------------------------- ---------- ----------
张三                                                                                                   A001                                                                                                 市招商局                                                                                                 2013       优秀
李四                                                                                                   A002                                                                                                 市招商局                                                                                                 NULL       NULL

(2 行受影响)

仙侣步惊云 2014-04-28
  • 打赏
  • 举报
回复
楼上的兄弟,我没看懂,是声明了表变量?
  • 打赏
  • 举报
回复
--> 测试数据: [tbMain]
if object_id('[tbMain]') is not null drop table [tbMain]
 go
 create table [tbMain] ([xm] varchar(6),[xh] varchar(4),[gzdw] varchar(8))
insert into [tbMain]
select '张三','A011','市招商局' union all
select '李四','A012','市招商局' union all
select '王五','A002','市教育局' union all
select '张小兵','B019','市教育局'


--> 测试数据: [tbKh]
if object_id('[tbKh]') is not null drop table [tbKh]
 go
 create table [tbKh] ([xh] varchar(4),[khnd] int,[khdc] varchar(4))
insert into [tbKh]
select 'A011',2012,'优秀' union all
select 'A012',2012,'合格' union all
select 'A011',2013,'优秀' union all
select 'A012',2013,'合格' union all
select 'A002',2013,'合格' union all
select 'B019',2013,'优秀'



declare @t table(khnd int)
insert @t
select 2013 union
select 2014

select *
from  tbMain s cross join @t t
       left join tbKh on s.xh=tbKh.xh and t.khnd=tbKh.khnd
张三	A011	市招商局	2013	A011	2013	优秀
李四	A012	市招商局	2013	A012	2013	合格
王五	A002	市教育局	2013	A002	2013	合格
张小兵	B019	市教育局	2013	B019	2013	优秀
张三	A011	市招商局	2014	NULL	NULL	NULL
李四	A012	市招商局	2014	NULL	NULL	NULL
王五	A002	市教育局	2014	NULL	NULL	NULL
张小兵	B019	市教育局	2014	NULL	NULL	NULL
KeepSayingNo 2014-04-28
  • 打赏
  • 举报
回复

select * from tbKh a
left join tbMain b
on a.xh=b.xh
where gzdw ='市招商局' and khnd=2014

34,593

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧