COALESCE 无法与 join 一起使用

发布于 2024-10-11 17:38:40 字数 1502 浏览 5 评论 0原文

我确信有人对此有更好的想法,所以这里是 - 我有一个表,其中包含一堆 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绮烟 2024-10-18 17:38:40

两行的项目 ID 均为 1。

我认为您可能希望将它们加入到类别 id 和项目 id 上。将 ProgramA 更改为 programid 为 1,然后在连接中:

left join
@tbModule m on t.categoryid  = 2 AND m.moduleid = t.itemid
left join
@tbProgram p on t.categoryid  = 1 AND p.programid = t.itemid

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:

left join
@tbModule m on t.categoryid  = 2 AND m.moduleid = t.itemid
left join
@tbProgram p on t.categoryid  = 1 AND p.programid = t.itemid
原谅过去的我 2024-10-18 17:38:40

也许你应该考虑不同的设计。我猜你可以尝试这些改变:

--declare @tbModule table (moduleid int, modulename varchar(20))
--declare @tbProgram table (programid int, programname varchar(20))
declare @tbItems table (itemid int, categoryid int, itemname varchar(20)) 

... 
--INSERT INTO @tbModule VALUES (1, 'Module1')
--INSERT INTO @tbProgram VALUES (2, 'ProgramA')
INSERT INTO @tbItems VALUES (1, 1, 'Module1')
INSERT INTO @tbItems VALUES (1, 2, 'ProgramA')

select t.*, i.typename, c.name, d.domainname, its.itemname 
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 @tbItems its on its.categoryid = t.categoryid AND its.itemid = t.itemid 
--@tbModule m on m.moduleid = t.itemid
--left join
--@tbProgram p on p.programid = t.itemid

maybe you should consider a different design. Fo what i can guess you could try these changes:

--declare @tbModule table (moduleid int, modulename varchar(20))
--declare @tbProgram table (programid int, programname varchar(20))
declare @tbItems table (itemid int, categoryid int, itemname varchar(20)) 

... 
--INSERT INTO @tbModule VALUES (1, 'Module1')
--INSERT INTO @tbProgram VALUES (2, 'ProgramA')
INSERT INTO @tbItems VALUES (1, 1, 'Module1')
INSERT INTO @tbItems VALUES (1, 2, 'ProgramA')

select t.*, i.typename, c.name, d.domainname, its.itemname 
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 @tbItems its on its.categoryid = t.categoryid AND its.itemid = t.itemid 
--@tbModule m on m.moduleid = t.itemid
--left join
--@tbProgram p on p.programid = t.itemid
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文