如何按年份分组,年份只显示一次
How to group by year with the year showing only once
我试过使用以下查询
select distinct Year (SaleDate) AS SaleYear,Max(SalePrice)
from Sale
group by SaleDate
2010 年和 2014 年显示了两次,尽管我使用了 distinct 和 group by。 Maxprice 中的金额也不同。我是不是做错了什么?
您需要在 group by
中重复 year()
:
select Year(SaleDate) AS SaleYear, Max(SalePrice)
from Sale
group by year(SaleDate);
SELECT DISTINCT
和 GROUP BY
几乎永远不会正确。您的查询所做的一切都是通过 SaleDate
和 在结果集中 提取年份进行聚合。这就是您看到重复项的原因。
我试过使用以下查询
select distinct Year (SaleDate) AS SaleYear,Max(SalePrice)
from Sale
group by SaleDate
2010 年和 2014 年显示了两次,尽管我使用了 distinct 和 group by。 Maxprice 中的金额也不同。我是不是做错了什么?
您需要在 group by
中重复 year()
:
select Year(SaleDate) AS SaleYear, Max(SalePrice)
from Sale
group by year(SaleDate);
SELECT DISTINCT
和 GROUP BY
几乎永远不会正确。您的查询所做的一切都是通过 SaleDate
和 在结果集中 提取年份进行聚合。这就是您看到重复项的原因。