简体   繁体   English

使用按日期分组的 sql 查询

[英]using sql query with group by date

I'm trying to write this query to retrieve No. of records grouped by date:我正在尝试编写此查询以检索按日期分组的记录数:

SELECT system_date,count (is_paid)
  FROM TPWEB.TP_CLIENT_TENDER_TRANS
  where system_date between '1-DEC-12' and '31-DEC-12'
  group by system_date

But, I got result without grouping such as:但是,我得到了没有分组的结果,例如:

01-DEC-12   1
01-DEC-12   1
01-DEC-12   1
01-DEC-12   1

what is wrong...怎么了……

As is documented the DATE datatype is a datetime and thus stores the hour, minute and second in addition to the attributes you'd, more reasonably expect, in a datatype with this name.正如所记录的那样,DATE 数据类型是一个日期时间,因此除了您更合理地期望的属性之外,还以具有此名称的数据类型存储小时、分钟和秒。 If you want to count over a day you need to remove the time portion of your date.如果您想计算一天,您需要删除日期的时间部分。 It is the default behaviour of TRUNC() , so to do this trunc(<date>) is all you need.这是TRUNC()的默认行为,因此执行此操作只需trunc(<date>)

It's worth noting two things at this point:在这一点上有两点值得注意:

  1. I'm assuming system_date is a column in your table and not a misunderstanding of SYSDATE我假设system_date是您表中的一列,而不是对SYSDATE的误解

  2. Your between clause is completely incorrect, dangerously so.您的 between 子句完全不正确,非常危险。

    By the way your dates have been represented it appears as though your NLS_DATE_FORMAT is DD-MON-YYYY (see another answer of mine for more details).通过表示您的日期的方式,您的 NLS_DATE_FORMAT 似乎是 DD-MON-YYYY(有关更多详细信息,请参阅我的另一个答案)。 This means that when you implicitly convert a date into a character it is converted in this format.这意味着当您将日期隐式转换为字符时,它会以这种格式转换。

    You're not using either a datetime literal or an explicit conversion of the values you're comparing to, which means your date is being implicitly converted to a character.您没有使用日期时间文字或您要比较的值的显式转换,这意味着您的日期被隐式转换为字符。 However, when you do the comparison you'll find that things aren't always as they seem.但是,当您进行比较时,您会发现事情并不总是像看起来的那样。 Character comparison is, normally, binary;字符比较通常是二进制的; this means that the 10 th of February is not between the 10 th January and the 10 th March;这意味着 2 月 10不在 1 月 10和 3 月 10日之间 "March" is smaller than "January". “三月”比“一月”小。

    Always explicitly convert dates and always use dates when doing date comparisons.在进行日期比较时,始终显式转换日期并始终使用日期。

Putting all of this together your query becomes:将所有这些放在一起,您的查询变为:

select trunc(system_date), count(is_paid)`
  from TPWEB.TP_CLIENT_TENDER_TRANS
 where system_date between date '2012-12-01' and date '2012-12-31'
 group by trunc(system_date)

Use trunc function, the DATE datatype holds the time with it, so using trunc we can truncate time.使用 trunc 函数,DATE 数据类型用它保存时间,所以使用 trunc 我们可以截断时间。 Below query works!下面的查询有效!

SELECT trunc(system_date),count (is_paid)
  FROM TPWEB.TP_CLIENT_TENDER_TRANS
  where system_date between '1-DEC-12' and '31-DEC-12'
  group by trunc(system_date);

DATE columns in Oracle contain precision up to milliseconds, not just the date. Oracle 中的DATE列包含高达毫秒的精度,而不仅仅是日期。 If you're only interested in the date itself, you should TRUNC it to remove hours, minutes, etc.:如果您只对日期本身感兴趣,您应该TRUNC以删除小时、分钟等:

SELECT   system_date,COUNT (is_paid)
FROM     TPWEB.TP_CLIENT_TENDER_TRANS
WHERE    system_date BETWEEN '1-DEC-12' AND '31-DEC-12'
GROUP BY TRUNC(system_date, 'DD-MON-YY')

This works for me:这对我有用:

select DT_FECHA_DESDE,DT_FECHA_HASTA
from (SELECT TRUNC(DT_FECHA_DESDE)as DT_FECHA_DESDE,TRUNC(DT_FECHA_HASTA) as DT_FECHA_HASTA
FROM TBL1
WHERE run='3456'
order by DT_FECHA_DESDE)  
group by DT_FECHA_DESDE,DT_FECHA_HASTA

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM