在日期部分上使用 MIN 且 Group BY 不起作用,会返回不同的日期

发布于 2024-07-25 17:40:32 字数 425 浏览 4 评论 0原文

任何人都可以帮助实现聚合函数吗?

我有一个汽车表,我想返回具有相同汽车但不同年份和价格的表的最低销售价格和最低年份...

基本上,如果我从组中删除注册(包含年份)并选择查询工作但如果我把它留在里面,我会返回 3 辆车,它们的型号、制造商等完全相同,但年份不同。

但我使用的是 MIN,所以它应该返回 1 辆车,年份为 2006 年(3 辆车之间的最小年份) )

MIN(SalePrice)工作正常..它的注册器不起作用..

有什么想法吗?

选择 MIN(datepart(年份,[注册])) AS 注册年份, 最低(销售价格)、型号、品牌 从 [车辆销售] 通过...分组 datepart(年份,[注册]), 型号, 制造商

Can anyone help with an aggregate function.. MIN.

I have a car table that i want to return minimum sale price and minimum year on a tbale that has identical cars but different years and price ...

Basically if i removed Registration (contains a YEAR) from the group by and select the query works but if i leave it in then i get 3 cars returned which are exactly the same model,make etc but with different years..

But i am using MIN so it should return 1 car with the year 2006 (the minimum year between the 3 cars)

The MIN(SalePrice) is working perfectly .. its the registraton thats not owrking..

Any ideas?

SELECT
MIN(datepart(year,[Registration])) AS YearRegistered,
MIN(SalePrice), Model, Make
FROM
[VehicleSales]
GROUP BY
datepart(year,[Registration]), Model, Make

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

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

发布评论

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

评论(4

香橙ぽ 2024-08-01 17:40:32

如果我正确理解了您要查找的内容,您应该询问:

SELECT Model, Make, MIN(datepart(year,[Registration])) AS YearRegistered, MIN(SalePrice)
FROM [VehicleSales]
GROUP BY Model, Make

希望它有帮助。

IF I have correctly understood what you are looking for, you should query:

SELECT Model, Make, MIN(datepart(year,[Registration])) AS YearRegistered, MIN(SalePrice)
FROM [VehicleSales]
GROUP BY Model, Make

Hope it helps.

轮廓§ 2024-08-01 17:40:32

Turro 答案将返回最低注册年份和(型号、品牌)的最低价格,但这并不意味着最低价格将针对年份最低的汽车。
这是你需要的吗?

或者,您需要其中之一:

  1. 具有最低年份的汽车之间的最低价格

  2. 具有最低价格的汽车之间的最低价格

——编辑——

您的查询是正确的,但我想找到明年会更便宜的汽车品牌/型号;)

这就是我发表评论的原因。 想象一下接下来的情况,

Porshe 911 2004 2000
Porshe 911 2004 3000
Porshe 911 2005 1000
Porshe 911 2005 5000

您将得到的结果并不能真正告诉您这辆车是否会根据年份变得更便宜。

Porshe 911 2004 1000

我不知道至少在不与前一年进行比较的情况下,你如何根据一排来判断明年的汽车是否会便宜。

PS 我想以标价购买上述汽车中的一辆:D

Turro answer will return the lowest registration year and the lowest price for (Model, Make), but this doesn't mean that lowest price will be for the car with lowest Year.
Is it what you need?

Or, you need one of those:

  1. lowest price between the cars having lowest year

  2. lowest year between the cars having lowest price

-- EDITED ---

You are correct about the query, but I want to find the car make/model that gets cheaper the next year ;)

That's why I made a comment. Imagine next situation

Porshe 911 2004 2000
Porshe 911 2004 3000
Porshe 911 2005 1000
Porshe 911 2005 5000

You'll get result that will not really tell you if this car goes cheaper based on year or not.

Porshe 911 2004 1000

I don't know how you'll tell if car gets cheaper next year based on one row without comparison with previous year, at least.

P.S. I'd like to buy one of cars above for listed price :D

街角迷惘 2024-08-01 17:40:32

您得到了您所要求的:每当汽车的型号、品牌或年份不同时,汽车就会被放入不同的组中,并且返回每个组的(最小,即唯一)年份和最低价格。

为什么使用 GROUP BY?

You're getting what you're asking for: the cars are put into different groups whenever their model, make, or year is different, and the (minimum, i.e. only) year and minimum price for each of those groups is returned.

Why are you using GROUP BY?

嗫嚅 2024-08-01 17:40:32

您的查询是正确的,但我想找到明年会更便宜的汽车品牌/型号;)

您应该找到每年最便宜(或平均)的品牌/型号,并与上一年最便宜(或平均)的进行比较(对于相同品牌/型号)。

然后你就可以看到明年哪些会更便宜(我想是大多数)

You are correct about the query, but I want to find the car make/model that gets cheaper the next year ;)

You should find cheapest (or average) make/model per year and compare with the cheapest (or average) from previous year (for the same make/model).

Then you can see which of them gets cheaper the next year (I suppose most of them)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文