在日期部分上使用 MIN 且 Group BY 不起作用,会返回不同的日期
任何人都可以帮助实现聚合函数吗?
我有一个汽车表,我想返回具有相同汽车但不同年份和价格的表的最低销售价格和最低年份...
基本上,如果我从组中删除注册(包含年份)并选择查询工作但如果我把它留在里面,我会返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果我正确理解了您要查找的内容,您应该询问:
希望它有帮助。
IF I have correctly understood what you are looking for, you should query:
Hope it helps.
Turro 答案将返回最低注册年份和(型号、品牌)的最低价格,但这并不意味着最低价格将针对年份最低的汽车。
这是你需要的吗?
或者,您需要其中之一:
具有最低年份的汽车之间的最低价格
具有最低价格的汽车之间的最低价格
——编辑——
这就是我发表评论的原因。 想象一下接下来的情况,
您将得到的结果并不能真正告诉您这辆车是否会根据年份变得更便宜。
我不知道至少在不与前一年进行比较的情况下,你如何根据一排来判断明年的汽车是否会便宜。
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:
lowest price between the cars having lowest year
lowest year between the cars having lowest price
-- EDITED ---
That's why I made a comment. Imagine next situation
You'll get result that will not really tell you if this car goes cheaper based on year or not.
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
您得到了您所要求的:每当汽车的型号、品牌或年份不同时,汽车就会被放入不同的组中,并且返回每个组的(最小,即唯一)年份和最低价格。
为什么使用 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?
您应该找到每年最便宜(或平均)的品牌/型号,并与上一年最便宜(或平均)的进行比较(对于相同品牌/型号)。
然后你就可以看到明年哪些会更便宜(我想是大多数)
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)