SQL 按年份分组

发布于 2024-11-06 14:57:56 字数 301 浏览 0 评论 0原文

这是我的查询。

SELECT CONVERT(varchar, cast(date as datetime), 3)
FROM shoptransfer 
GROUP BY year (date)

我想按日期 (varchar) 列的年份部分进行分组,但是出现以下错误:

列“shoptransfer.Date”在选择列表中无效,因为它未包含在聚合函数或 GROUP BY 子句中。

如何按日期列的年份部分进行分组?

This is my query.

SELECT CONVERT(varchar, cast(date as datetime), 3)
FROM shoptransfer 
GROUP BY year (date)

I want to group by the year part of the date (varchar) column, however I get the following error:

Column 'shoptransfer.Date' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.

How do I group by the year part of the date column?

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

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

发布评论

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

评论(6

坏尐絯 2024-11-13 14:57:56

怎么样:

select datepart(yyyy, [date]) as [year]
from shoptransfer 
group by datepart(yyyy, [date])

或者:

select count(*) as qty, datepart(yyyy, [date]) as [year]
from shoptransfer 
group by datepart(yyyy, [date])
order by [year]

这是基于OP的命令:“我想按日期(varchar)列的年份部分进行分组”

How about:

select datepart(yyyy, [date]) as [year]
from shoptransfer 
group by datepart(yyyy, [date])

Or:

select count(*) as qty, datepart(yyyy, [date]) as [year]
from shoptransfer 
group by datepart(yyyy, [date])
order by [year]

This is based on OP's command: "I want to group by year part of date (varchar) column"

冷情 2024-11-13 14:57:56

使用 MariaDB:

SELECT year(date) FROM cost GROUP BY year(date)

With MariaDB:

SELECT year(date) FROM cost GROUP BY year(date)
静谧幽蓝 2024-11-13 14:57:56

如果您想同时选择日期和年份,则不应使用 GROUP BY 子句,因为它将所有具有相似年份的行合并为一行。如果您想要将所有具有相似年份的日期放在一起,您可以使用 ORDER BY:

SELECT DATEPART('yyyy',date) AS Year,date
FROM shoptransfer 

OR

SELECT DATEPART('yyyy',date) AS Year,date
FROM shoptransfer
ORDER BY DATEPART('yyyy',date) desc

If you want to select both the date and the year you should not use a GROUP BY clause as it combines all of the rows with similar years into one row. If you want all of the dates with similar years together you can use an ORDER BY:

SELECT DATEPART('yyyy',date) AS Year,date
FROM shoptransfer 

OR

SELECT DATEPART('yyyy',date) AS Year,date
FROM shoptransfer
ORDER BY DATEPART('yyyy',date) desc
罪#恶を代价 2024-11-13 14:57:56

我不了解 T-SQL,但在一般 SQL 中,group by 子句中的内容必须与 select 子句中的每个非聚合函数列完全匹配。 也尝试

select CONVERT(varchar,cast(date as datetime),3)
from shoptransfer 
where 
SUBSTRING(productcode, 5, 3) like '%'  group by CONVERT(varchar,cast(date as datetime),3)

一下,其中 SUBSTRING(productcode, 5, 3) 像 '%' 没有过滤掉太多 - 也许将其删除。

I don't know about T-SQL, but in SQL in general, what is in the group by clause must exactly match each non-aggregate function column in the select clause. Try

select CONVERT(varchar,cast(date as datetime),3)
from shoptransfer 
where 
SUBSTRING(productcode, 5, 3) like '%'  group by CONVERT(varchar,cast(date as datetime),3)

also, where SUBSTRING(productcode, 5, 3) like '%' is not filtering out much - maybe remove it.

帅的被狗咬 2024-11-13 14:57:56

您应该将 shoptransfer.Date 列添加到 group by 子句中。

select CONVERT(varchar,cast(date as datetime),3)
from shoptransfer 
where 
SUBSTRING(productcode, 5, 3) like '%' 
group by CONVERT(varchar,cast(date as datetime),3)

You should add column shoptransfer.Date into group by clause.

select CONVERT(varchar,cast(date as datetime),3)
from shoptransfer 
where 
SUBSTRING(productcode, 5, 3) like '%' 
group by CONVERT(varchar,cast(date as datetime),3)
冰魂雪魄 2024-11-13 14:57:56
group by
substring(CONVERT(varchar,comuln1,101),1,4)

年份
2004年
2005年
2006年

group by
substring(CONVERT(varchar,comuln1,101),1,4)

year
2004
2005
2006

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