有没有办法使用 "As" 列名连接表
Is there a way to join tables using "As" Column Names
我正在尝试创建一个查询,该查询根据名字和姓氏将 2 个表连接在一起。我成功地让我的子字符串将一列中的名称拆分为 2 个名为 FirstName 和 LastName 的名称。这应该让我匹配这些列,以便它们可以获得正确的扩展名、phone 号码、部门和名称。但是我在这里做错了。我这样做的方法不起作用,否则会引发错误。
SELECT SUBSTRING(Users.Names, CHARINDEX(' ', Users.Names + ' ') + 1, 8000)AS LastName, SUBSTRING(Users.Names, 1, CHARINDEX(' ', Users.Names) - 1) AS FirstName,Users.Extension, GA.[First], GA.[Last], GA.Department, GA.Phone, GA.Mobile
FROM GlobalAddress AS GA
Left Join Users ON GA.[First] = substring(Users.Names,1,charindex(' ',Users.Names) ) AND SUBSTRING(Users.Names, CHARINDEX(' ', Users.Names + ' ') + 1, 8000) = Ga.[Last]
WHERE GA.[Last] IS NOT NULL
ORDER BY Users.Extension
我的想法是我可以用以下方式替换 Left Join:
Users ON GA.[First] = FirstName AND Ga.[Last] = LastName
但这会引发错误。列名称无效 'FirstName'。
An example of the format i'm following
如有任何帮助,我们将不胜感激。
编辑:The problem is that the first few people are not properly getting the extension and for some reason their names are not being split up. The issue isn't with the data on the table though
编辑 2:An Example Of Global Addresses Table
An Example of Users
在连接时使用原始值 - 不是别名
...Users ON GA.[First] = SUBSTRING(Users.Names, 1, CHARINDEX(' ', Users.Names) - 1) ...
因此,您想为列创建别名,因为它们冗长的主体出现在很多地方。交叉应用可以做到这一点:
SELECT
q.LastName,
q.FirstName,
Users.Extension, GA.[First], GA.[Last], GA.Department, GA.Phone, GA.Mobile
FROM
Users
cross apply (select
SUBSTRING(Users.Names, CHARINDEX(' ', Users.Names + ' ') + 1, 8000)AS LastName,
left(Users.Names, CHARINDEX(' ', Users.Names + ' ') - 1) AS FirstName
)q
Right Join GlobalAddress AS GA
ON GA.[First] = q.FirstName
AND q.LastName = Ga.[Last]
WHERE GA.[Last] IS NOT NULL ORDER BY Users.Extension
当 CROSS APPLY 中没有 FROM 时,它可以被视为为其列创建别名。
哇。这可能是我第一次认为 right join
是做某事的更简单方法:
SELECT v.LastName, v.FirstName, u.Extension, GA.[First], GA.[Last],
GA.Department, GA.Phone, GA.Mobile
from Users u cross apply
(values (substring(u.Names, CHARINDEX(' ', u.Names + ' ') + 1, 8000),
left(u.Names, CHARINDEX(' ', u.Names + ' ') - 1)
)
) v(LastName, Firstname) right join
GlobalAddress GA
on GA.[First] = v.firstname and
Ga.[Last] = v.lastname
where GA.[Last] is not null
order by u.Extension;
我想知道您是否真的想要外部联接。请注意,您 可以 使用 left join
和括号执行此操作。
我正在尝试创建一个查询,该查询根据名字和姓氏将 2 个表连接在一起。我成功地让我的子字符串将一列中的名称拆分为 2 个名为 FirstName 和 LastName 的名称。这应该让我匹配这些列,以便它们可以获得正确的扩展名、phone 号码、部门和名称。但是我在这里做错了。我这样做的方法不起作用,否则会引发错误。
SELECT SUBSTRING(Users.Names, CHARINDEX(' ', Users.Names + ' ') + 1, 8000)AS LastName, SUBSTRING(Users.Names, 1, CHARINDEX(' ', Users.Names) - 1) AS FirstName,Users.Extension, GA.[First], GA.[Last], GA.Department, GA.Phone, GA.Mobile
FROM GlobalAddress AS GA
Left Join Users ON GA.[First] = substring(Users.Names,1,charindex(' ',Users.Names) ) AND SUBSTRING(Users.Names, CHARINDEX(' ', Users.Names + ' ') + 1, 8000) = Ga.[Last]
WHERE GA.[Last] IS NOT NULL
ORDER BY Users.Extension
我的想法是我可以用以下方式替换 Left Join:
Users ON GA.[First] = FirstName AND Ga.[Last] = LastName
但这会引发错误。列名称无效 'FirstName'。
An example of the format i'm following
如有任何帮助,我们将不胜感激。
编辑:The problem is that the first few people are not properly getting the extension and for some reason their names are not being split up. The issue isn't with the data on the table though
编辑 2:An Example Of Global Addresses Table
An Example of Users
在连接时使用原始值 - 不是别名
...Users ON GA.[First] = SUBSTRING(Users.Names, 1, CHARINDEX(' ', Users.Names) - 1) ...
因此,您想为列创建别名,因为它们冗长的主体出现在很多地方。交叉应用可以做到这一点:
SELECT
q.LastName,
q.FirstName,
Users.Extension, GA.[First], GA.[Last], GA.Department, GA.Phone, GA.Mobile
FROM
Users
cross apply (select
SUBSTRING(Users.Names, CHARINDEX(' ', Users.Names + ' ') + 1, 8000)AS LastName,
left(Users.Names, CHARINDEX(' ', Users.Names + ' ') - 1) AS FirstName
)q
Right Join GlobalAddress AS GA
ON GA.[First] = q.FirstName
AND q.LastName = Ga.[Last]
WHERE GA.[Last] IS NOT NULL ORDER BY Users.Extension
当 CROSS APPLY 中没有 FROM 时,它可以被视为为其列创建别名。
哇。这可能是我第一次认为 right join
是做某事的更简单方法:
SELECT v.LastName, v.FirstName, u.Extension, GA.[First], GA.[Last],
GA.Department, GA.Phone, GA.Mobile
from Users u cross apply
(values (substring(u.Names, CHARINDEX(' ', u.Names + ' ') + 1, 8000),
left(u.Names, CHARINDEX(' ', u.Names + ' ') - 1)
)
) v(LastName, Firstname) right join
GlobalAddress GA
on GA.[First] = v.firstname and
Ga.[Last] = v.lastname
where GA.[Last] is not null
order by u.Extension;
我想知道您是否真的想要外部联接。请注意,您 可以 使用 left join
和括号执行此操作。