COALESCE 无法与 join 一起使用
我确信有人对此有更好的想法,所以这里是 - 我有一个表,其中包含一堆 id (@tbLink),它们代表其他表中的行。我试图在这里表达
declare @tbLink table (linkid int, identitytypeid int,
itemid int, categoryid int,parentid int)
declare @tbCat table (categoryid int, name varchar(20))
declare @tbId table (typeid int, typename varchar(20))
declare @tbDomain table (domainid int, domainname varchar(20))
--
declare @tbModule table (moduleid int, modulename varchar(20))
declare @tbProgram table (programid int, programname varchar(20))
INSERT INTO @tbLink VALUES (1, 1, 1, 1, 1)
INSERT INTO @tbLink VALUES (2, 1, 1, 2, 1)
INSERT INTO @tbCat VALUES (1, 'Program')
INSERT INTO @tbCat VALUES (2, 'Module')
INSERT INTO @tbId VALUES (1, 'Domain')
INSERT INTO @tbId VALUES (2, 'Group')
INSERT INTO @tbDomain VALUES (1, 'DEV')
INSERT INTO @tbModule VALUES (1, 'Module1')
INSERT INTO @tbProgram VALUES (2, 'ProgramA')
select t.*, i.typename, c.name, d.domainname, COALESCE(m.modulename, p.programname)
as objectname from @tbLink t
inner join
@tbId i on t.identitytypeid = i.typeid
inner join
@tbCat c on t.categoryid = c.categoryid
inner join
@tbDomain d on t.parentid = d.domainid
left join
@tbModule m on m.moduleid = t.itemid
left join
@tbProgram p on p.programid = t.itemid
我的结果是:
1 1 1 1 1 Domain Program DEV Module1
2 1 1 2 1 Domain Module DEV Module1
但我期望第 1 行是“ProgramA”而不是“Module1”——我在这里遗漏了什么吗?这也是 COALESCE 的正确使用吗?
干杯
迈克
I'm sure someone has a better idea about this so here goes -
I have a table with a bunch of ids (@tbLink) that represent rows in other tables. I'm trying to express here
declare @tbLink table (linkid int, identitytypeid int,
itemid int, categoryid int,parentid int)
declare @tbCat table (categoryid int, name varchar(20))
declare @tbId table (typeid int, typename varchar(20))
declare @tbDomain table (domainid int, domainname varchar(20))
--
declare @tbModule table (moduleid int, modulename varchar(20))
declare @tbProgram table (programid int, programname varchar(20))
INSERT INTO @tbLink VALUES (1, 1, 1, 1, 1)
INSERT INTO @tbLink VALUES (2, 1, 1, 2, 1)
INSERT INTO @tbCat VALUES (1, 'Program')
INSERT INTO @tbCat VALUES (2, 'Module')
INSERT INTO @tbId VALUES (1, 'Domain')
INSERT INTO @tbId VALUES (2, 'Group')
INSERT INTO @tbDomain VALUES (1, 'DEV')
INSERT INTO @tbModule VALUES (1, 'Module1')
INSERT INTO @tbProgram VALUES (2, 'ProgramA')
select t.*, i.typename, c.name, d.domainname, COALESCE(m.modulename, p.programname)
as objectname from @tbLink t
inner join
@tbId i on t.identitytypeid = i.typeid
inner join
@tbCat c on t.categoryid = c.categoryid
inner join
@tbDomain d on t.parentid = d.domainid
left join
@tbModule m on m.moduleid = t.itemid
left join
@tbProgram p on p.programid = t.itemid
My results are:
1 1 1 1 1 Domain Program DEV Module1
2 1 1 2 1 Domain Module DEV Module1
But I was expecting row 1 to be 'ProgramA' not 'Module1' - am I missing something here? Is this the proper use of COALESCE also?
Cheers
Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
两行的项目 ID 均为 1。
我认为您可能希望将它们加入到类别 id 和项目 id 上。将 ProgramA 更改为 programid 为 1,然后在连接中:
both rows have a item ID of 1.
i think you may want to join them on the category id AND the item id., change ProgramA to have a programid of 1, and in your join:
也许你应该考虑不同的设计。我猜你可以尝试这些改变:
maybe you should consider a different design. Fo what i can guess you could try these changes: