SQL服务器:自连接查询; select 仅记录具有匹配名字且没有 where 语句的记录
SQL Server: Self Join query; select only records with matching first name WITHOUT a where statement
我是 SQL 服务器的新手。我被困在一个实验室问题上。我不能使用 WHERE 语句来限制结果。我附上了下面给出的指示。预期结果应该 return 6 行。我目前 returning 122 rows.We 正在使用 Microsoft SQL Server Management Studio。我们正在从一个包含数千条记录的大型预配置数据库中提取数据。
这是实验室引用的文本。
Write a SELECT statement that returns three columns:
- VendorID From the Vendors table
- VendorName From the Vendors table
- Contact Name Alias for VendorContactFName and VendorContactLName, with a space in between.
Write a SELECT statement which compares each vendor whose VendorContactFName has the same first name as another VendorContactFName. In other words, find all the different Vendors whose VendorContactFName have the same first name.
Compound JOIN Condition.
No WHERE condition. Sort the final result set by Contact Name (6 rows returned)
Hint: Use a self-join & correlation names; Ex: The Vendor table is both V1 & V2. QUALIFY ALL COLUMN NAMES in the query including those in the SELECT statement
这是我到目前为止的想法,但无法弄清楚如何在没有 WHERE 语句的情况下限制记录。我可能有多余的代码在这里不需要,或者缺少我确实需要的代码。
这是我想出的开始代码。
SELECT
V1.VendorID AS VendorID, V1.VendorName AS VendorName,
V1.VendorContactFName + ' ' + V1.VendorContactLName AS [Contact Name]
FROM
Vendors AS V1
JOIN
Vendors AS V2 ON (V1.VendorContactFName = V2.VendorContactFName)
AND (V1.VendorID = V2.VendorID)
ORDER BY
[Contact Name];
Query Result
DB Diagram
您只需更新 JOIN 条件,名字应该在 V1 和 V2 之间匹配,但供应商 ID 应该不同。还使用 CONCAT
函数获取联系人姓名
SELECT DISTINCT V1.VendorID AS VendorID,
V1.VendorName AS VendorName,
CONCAT(V1.VendorContactFName, ' ', V1.VendorContactLName)
AS [Contact Name]
FROM Vendors AS V1 JOIN Vendors AS V2
ON (V1.VendorContactFName = V2.VendorContactFName) AND
(V1.VendorID <> V2.VendorID)
ORDER BY [Contact Name]
我是 SQL 服务器的新手。我被困在一个实验室问题上。我不能使用 WHERE 语句来限制结果。我附上了下面给出的指示。预期结果应该 return 6 行。我目前 returning 122 rows.We 正在使用 Microsoft SQL Server Management Studio。我们正在从一个包含数千条记录的大型预配置数据库中提取数据。
这是实验室引用的文本。
Write a SELECT statement that returns three columns:
- VendorID From the Vendors table
- VendorName From the Vendors table
- Contact Name Alias for VendorContactFName and VendorContactLName, with a space in between.
Write a SELECT statement which compares each vendor whose VendorContactFName has the same first name as another VendorContactFName. In other words, find all the different Vendors whose VendorContactFName have the same first name. Compound JOIN Condition.
No WHERE condition. Sort the final result set by Contact Name (6 rows returned)
Hint: Use a self-join & correlation names; Ex: The Vendor table is both V1 & V2. QUALIFY ALL COLUMN NAMES in the query including those in the SELECT statement
这是我到目前为止的想法,但无法弄清楚如何在没有 WHERE 语句的情况下限制记录。我可能有多余的代码在这里不需要,或者缺少我确实需要的代码。
这是我想出的开始代码。
SELECT
V1.VendorID AS VendorID, V1.VendorName AS VendorName,
V1.VendorContactFName + ' ' + V1.VendorContactLName AS [Contact Name]
FROM
Vendors AS V1
JOIN
Vendors AS V2 ON (V1.VendorContactFName = V2.VendorContactFName)
AND (V1.VendorID = V2.VendorID)
ORDER BY
[Contact Name];
Query Result
DB Diagram
您只需更新 JOIN 条件,名字应该在 V1 和 V2 之间匹配,但供应商 ID 应该不同。还使用 CONCAT
函数获取联系人姓名
SELECT DISTINCT V1.VendorID AS VendorID,
V1.VendorName AS VendorName,
CONCAT(V1.VendorContactFName, ' ', V1.VendorContactLName)
AS [Contact Name]
FROM Vendors AS V1 JOIN Vendors AS V2
ON (V1.VendorContactFName = V2.VendorContactFName) AND
(V1.VendorID <> V2.VendorID)
ORDER BY [Contact Name]