简体   繁体   English

SQL 查询按天分组

[英]SQL query to group by day

I want to list all sales, and group the sum by day.我想列出所有销售额,并按天对总和进行分组。

Sales (saleID INT, amount INT, created DATETIME)

NOTE: I am using SQL Server 2005.注意:我使用的是 SQL Server 2005。

if you're using SQL Server,如果您使用的是 SQL Server,

dateadd(DAY,0, datediff(day,0, created)) will return the day created dateadd(DAY,0, datediff(day,0, created))将返回创建的日期

for example, if the sale created on '2009-11-02 06:12:55.000', dateadd(DAY,0, datediff(day,0, created)) return '2009-11-02 00:00:00.000'例如,如果销售创建于 '2009-11-02 06:12:55.000',则dateadd(DAY,0, datediff(day,0, created))返回 '​​2009-11-02 00:00:00.000'

select sum(amount) as total, dateadd(DAY,0, datediff(day,0, created)) as created
from sales
group by dateadd(DAY,0, datediff(day,0, created))

For SQL Server:对于 SQL Server:

GROUP BY datepart(year,datefield), 
    datepart(month,datefield), 
    datepart(day,datefield)

or faster (from Q8-Coder):或更快(来自 Q8-Coder):

GROUP BY dateadd(DAY,0, datediff(day,0, created))

For MySQL:对于 MySQL:

GROUP BY year(datefield), month(datefield), day(datefield)

or better (from Jon Bright):或更好(来自 Jon Bright):

GROUP BY date(datefield)

For Oracle:对于甲骨文:

GROUP BY to_char(datefield, 'yyyy-mm-dd')

or faster (from IronGoofy):或更快(来自 IronGoofy):

GROUP BY trunc(created);

For Informix (by Jonathan Leffler):对于 Informix(由 Jonathan Leffler):

GROUP BY date_column
GROUP BY EXTEND(datetime_column, YEAR TO DAY)

If you're using MySQL:如果您使用的是 MySQL:

SELECT
    DATE(created) AS saledate,
    SUM(amount)
FROM
    Sales
GROUP BY
    saledate

If you're using MS SQL 2008:如果您使用的是 MS SQL 2008:

SELECT
    CAST(created AS date) AS saledate,
    SUM(amount)
FROM
    Sales
GROUP BY
    CAST(created AS date)

actually this depends on what DBMS you are using but in regular SQL convert(varchar,DateColumn,101) will change the DATETIME format to date (one day)实际上,这取决于您使用的是什么 DBMS,但在常规 SQL convert(varchar,DateColumn,101)会将 DATETIME 格式更改为日期(一天)

so:所以:

SELECT 
    sum(amount) 
FROM 
    sales 
GROUP BY 
    convert(varchar,created,101)

the magix number 101 is what date format it is converted to magix 数字101是它转换成的日期格式

If you're using SQL Server, you could add three calculated fields to your table:如果您使用的是 SQL Server,则可以向表中添加三个计算字段:

Sales (saleID INT, amount INT, created DATETIME)

ALTER TABLE dbo.Sales
  ADD SaleYear AS YEAR(Created) PERSISTED
ALTER TABLE dbo.Sales
  ADD SaleMonth AS MONTH(Created) PERSISTED
ALTER TABLE dbo.Sales
  ADD SaleDay AS DAY(Created) PERSISTED

and now you could easily group by, order by etc. by day, month or year of the sale:现在您可以按销售的日期、月份或年份轻松分组、订购等:

SELECT SaleDay, SUM(Amount)
FROM dbo.Sales
GROUP BY SaleDay

Those calculated fields will always be kept up to date (when your "Created" date changes), they're part of your table, they can be used just like regular fields, and can even be indexed (if they're "PERSISTED") - great feature that's totally underused, IMHO.这些计算字段将始终保持最新(当您的“创建”日期更改时),它们是您的表的一部分,它们可以像常规字段一样使用,甚至可以编入索引(如果它们是“PERSISTED”) ) - 很棒的功能,完全没有得到充分利用,恕我直言。

Marc马克

For PostgreSQL:对于 PostgreSQL:

GROUP BY to_char(timestampfield, 'yyyy-mm-dd')

or using cast:或使用演员表:

GROUP BY timestampfield::date

if you want speed, use the second option and add an index:如果您想要速度,请使用第二个选项并添加索引:

CREATE INDEX tablename_timestampfield_date_idx ON  tablename(date(timestampfield));

For oracle you can对于oracle,您可以

group by trunc(created);

as this truncates the created datetime to the previous midnight.因为这会将创建的日期时间截断到前一个午夜。

Another option is to另一种选择是

group by to_char(created, 'DD.MM.YYYY');

which achieves the same result, but may be slower as it requires a type conversion.实现相同的结果,但可能会更慢,因为它需要类型转换。

The simplest and intuitive solution for MySQL is: MySQL 最简单直观的解决方案是:

GROUP BY day(datefield)

use linq使用 linq

from c in Customers
group c by DbFunctions.TruncateTime(c.CreateTime) into date
orderby date.Key descending
select new  
{
    Value = date.Count().ToString(),
    Name = date.Key.ToString().Substring(0, 10)
}

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

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