SQL:如何在sql中找到每组的最小值?

时间:2017-09-22 12:10:34

标签: mysql sql

我有下表snapshots

domain      year  month  day
---         ---    ---   ---
google      2007   04    15
google      2005   08    31
google      2005   12    01
facebook    2006   04    15
facebook    2006   02    25
facebook    2008   01    01

我想要检索的是每个域的第一个(最早的)日期。

所以输出应该是:

google    2005  08  31
facebook  2006  02  25  

我尝试了以下查询,但它检索每列的最小值:

select domain, min(year), min(month), min(day) from snapshots group by domain

6 个答案:

答案 0 :(得分:2)

如上所述,您应该使用串联来创建单个日期,然后选择最低值。

select domain, MIN(CAST(CONCAT(`year`, '-'`,month`,'-',`day`) AS DATE)) from snapshots group by domain

Haven没有对此进行测试,但这应该会给你一个想法。

答案 1 :(得分:1)

您可以连接日期字段中的值,将它们转换为日期并选择最小日期(我希望在这种情况下值为varchar):

SELECT domain, 
MIN(CAST(CONCAT(year,'-',month,'-',day) AS date)) 
FROM snapshots 
GROUP BY domain;

答案 2 :(得分:1)

在MySQL中: SELECT domain, FROM_UNIXTIME(UNIX_TIMESTAMP(MIN(CONCAT(year,'-',month,'-',day))), '%Y') as y, FROM_UNIXTIME(UNIX_TIMESTAMP(MIN(CONCAT(year,'-',month,'-',day))), '%m') as m, FROM_UNIXTIME(UNIX_TIMESTAMP(MIN(CONCAT(year,'-',month,'-',day))), '%d') as d FROM snapshots GROUP BY domain ;

答案 3 :(得分:0)

可能有更简单的解决方案,但您可以从年,月,日三列创建新的日期类型列。然后得到如下的最短日期:

SELECT DISTINCT s.domain, s.year, s.month, s.day
FROM
(
  SELECT domain, year,month,day,
       STR_TO_DATE(CONCAT(`year`,'-',LPAD(`month`,2,'00'),'-',LPAD(`day`,2,'00')) ,'%Y-%m-%d') AS FullDate
    FROM snapshots
) AS s
INNER JOIN
(
  SELECT domain, MIN(Fulldate) MinDate
  FROM
  (
    SELECT domain, year,month,day,
       STR_TO_DATE(CONCAT(`year`,'-',LPAD(`month`,2,'00'),'-',LPAD(`day`,2,'00')) ,'%Y-%m-%d') AS FullDate
    FROM snapshots
  ) AS t
  GROUP BY domain
) AS t  ON t.MinDate = s.FullDate 
       AND t.Domain = s.Domain;

demo

这将为您提供所需的确切结果:

|   domain | year | month | day |    MinDate |
|----------|------|-------|-----|------------|
|   google | 2005 |     8 |  31 | 2005-08-31 |
| facebook | 2006 |     2 |  25 | 2006-02-25 |

答案 4 :(得分:0)

请你试试这件事,让我知道它是否解决了你的问题而没有连接?如有必要,可以使用子查询使其更加健壮。

CREATE TABLE domainDate(domain CHAR(25), `year` INT, `month` INT, `day` INT);

INSERT INTO domainDate VALUES
    ('google', 2007, 04, 15),
    ('google', 2005, 08, 31),
    ('google', 2005, 12, 01),
    ('facebook', 2006, 04, 15),
    ('facebook', 2006, 02, 25),
    ('facebook', 2008, 01, 01);

SET @VDomain := '';
SELECT domain, `year`, `month`, `day` FROM domainDate HAVING @VDomain != @VDomain := domain ORDER BY domain, `year` * 10000 + `month` * 100 + `day`;

谢谢,

詹姆斯

答案 5 :(得分:0)

您可以尝试排名函数ROW_NUMBER()

CREATE TABLE domainDate(domain CHAR(25), [year] INT, [month] INT, [day] INT);

INSERT INTO domainDate VALUES
    ('google', 2007, 04, 15),
    ('google', 2005, 08, 31),
    ('google', 2005, 12, 01),
    ('facebook', 2006, 04, 15),
    ('facebook', 2006, 02, 25),
    ('facebook', 2008, 01, 01);

SELECT domain
    ,[year]
    ,[month]
    ,[day]
FROM 
(
    SELECT domain
        ,[year]
        ,[month]
        ,[day]
        ,ROW_NUMBER() OVER(PARTITION BY domain ORDER BY [year], [month], [day]) AS RN
    FROM domainDate
) t
WHERE RN = 1