日期列的 SQL 计数
我有一个表,其中包含一组列,其中一个是日期列。
我需要计算该列的值引用同一个月的次数。如果一个月的计数总和超过 3,则返回。
例如:
____________________
| DATE | .... |
---------------------
1998-09-02
1998-09-03
1998-10-03
1998-10-04
这必须不返回任何值。因为它没有必要的重复次数。
但它确实是这样的:
____________________
| DATE | .... |
---------------------
1998-09-02
1998-09-03
1998-09-12
1998-09-14
1998-10-02
1998-11-21
11 月份。
适用于 Oracle DB。
I have a table that containts a set of columns one of it is a Date column.
I need to count how many occurrences of the values of that column refer to the same month. And return if for one month, that count sums more than 3.
For example:
____________________
| DATE | .... |
---------------------
1998-09-02
1998-09-03
1998-10-03
1998-10-04
This must return no value. Because it doesn't have the necessary number of repetitions.
But this it does:
____________________
| DATE | .... |
---------------------
1998-09-02
1998-09-03
1998-09-12
1998-09-14
1998-10-02
1998-11-21
For the november month.
Is for an Oracle DB.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
那么,如果 3 哥伦布包含 1999-01-xx 您想获取它吗?
如果您需要包含上方结果的所有行,它应该看起来像这样
So if 3 coloums contain 1999-01-xx you want to get that fetched ?
If you need all the rows that contain the upper result it should look something like that
此示例将有所帮助:
您可以看到 Nov-09 是唯一有超过 3 个事件的月份。
回到您最初的问题,即如果一个月内的计数总和超过 3,则返回。以下 SQL 聚合将起作用。
或者,使用 to_char 将 Date 类型转换为带有 MON-YYYY 图片的 Char,如下所示:
This example will help :
You can see that Nov-09 is the only month which more than 3 events.
Referring back to your original question, which was And return if for one month, that count sums more than 3. The following SQL aggregate will work.
Alternatively, use to_char to convert the Date type to a Char with a MON-YYYY picture as follows :
理想情况下,您应该创建一个接受您需要的两个条件的存储过程,月份(整数)和限制(整数)
在执行以下命令的参数化过程中
要还输出相关月份,您可以使用以下命令
Ideally you should create a stored procedure that accepts the two criteria you need, Month(integer) and limit(integer)
In a parameterized procedure that executes the following
To also output the relevant month you could use the following
这应该适用于 mysql 和 mssql:
This should work for mysql and mssql:
我不确定您使用的是哪个数据库。
在 MySQL 中,查询将类似于
@THEn
提出的方法。在 SQL Server 上,您还有其他有趣的可能性。
阅读这篇文章了解更多详情。
I am not sure which database you are using.
In MySQL query will be similar to the method proposed by
@THEn
On SQL server you have other interesting possibilities.
Read the this article for more details.
您可以使用 Oracle 的 EXTRACT 方法:
我手头目前没有 Oracle 数据库,因此此代码可能无法按原样工作 - 对此我深表歉意。
You could use Oracle's EXTRACT method :
I don't have an Oracle database at hand at the moment, so this code may not work as is - I apologize for this.
可能是错误的,但猜测:
Could be wrong but a guess: