SQL 中的多个 WHERE 函数

发布于 2025-01-21 02:51:56 字数 189 浏览 3 评论 0原文

我正在尝试编写一个SQL查询,以返回信息,包括产品代码,每年的销售收入,成本,销售人员来自两个不同表的信息。我需要返回“北方”地区的产品ID信息的最低登机成本。

我使用其中region ='north'只是获取北部地区的产品信息,而 将其排序至高点并找到产品成本最低。是否有一种方法只是返回北部地区的登机成本最低的产品?

I'm trying to write an SQL query to return information including product code, yearly sales revenues, costs, sales people information from two different tables. I need to return the product ID information for the product with the lowest'onboarding cost' for the 'north' region.

I have used WHERE Region = 'North' to just get the product info for the North region, and ORDER BY onboarding cost; to sort this low to high and find the product with the lowest cost. Is there a way of just returning the product with the lowest onboarding cost for the north region?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

柒夜笙歌凉 2025-01-28 02:51:56

在 WHERE 子句中,您可以添加:

... AND onboarding cost=(SELECT MAX(onboarding cost) FROM ... WHERE region='North' ...)

 SELECT 
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North" 
AND ProductRevenueAndCosts.OnboardingCost=(
   SELECT MAX(ProductRevenueAndCosts.OnboardingCost)
   FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
   SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
   WHERE SalesPeople.Region = "North"
)
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC -- this is obsolete
;

但这最终会返回多个结果 - 以防出现多个结果比一种产品具有相同的最高入职成本。

In the WHERE clause you could add:

... AND onboarding cost=(SELECT MAX(onboarding cost) FROM ... WHERE region='North' ...)

 SELECT 
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North" 
AND ProductRevenueAndCosts.OnboardingCost=(
   SELECT MAX(ProductRevenueAndCosts.OnboardingCost)
   FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
   SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
   WHERE SalesPeople.Region = "North"
)
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC -- this is obsolete
;

But this will eventually return more than one result - in case that more than one product has the same highest onboaring cost.

有深☉意 2025-01-28 02:51:56

是的,您需要限制记录数,如下所示:

MySQL、PostgreSQL

SELECT 
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North"
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC
LIMIT 0, 1;

其中 0 是起始索引(第一行),1 是您想要获取的记录数。

SQL Server

SELECT TOP 1
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North"
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC;

其中 TOP 1 告诉 RDBMS 您只对第一行感兴趣。

甲骨文

SELECT 
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North"
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

Yes, you need to limit the number of records, like this:

MySQL, PostgreSQL

SELECT 
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North"
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC
LIMIT 0, 1;

Where 0 is the starting index (first row) and 1 is the number of records you want to get.

SQL Server

SELECT TOP 1
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North"
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC;

Where TOP 1 tells the RDBMS that you are interested only in the very first row.

Oracle

SELECT 
 ProductRevenueAndCosts.ProductID, 
 ProductRevenueAndCosts.SalesRevenueYear1, 
 ProductRevenueAndCosts.SalesRevenueYear2,  
 ProductRevenueAndCosts.OperationalCostsYear1, 
 ProductRevenueAndCosts.OperationalCostsYear2, 
 ProductRevenueAndCosts.OnboardingCost, 
 SalesPeople.FirstName, 
 SalesPeople.LastName, 
 SalesPeople.Region
FROM SalesPeople INNER JOIN ProductRevenueAndCosts ON 
SalesPeople.SalesPersonID = ProductRevenueAndCosts.SalesPersonID
WHERE SalesPeople.Region = "North"
ORDER BY ProductRevenueAndCosts.Onboardingcost DESC
OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文