简体   繁体   English

MySQL按两列分组

[英]MySQL group by on two columns

I have a table like this 我有这样的桌子

+---------------------+-------+-----+
|       dateTime      | value |  id |
+---------------------+-------+-----+
| 2013-04-06 06:54:00 |  2.5  |  1  |
| 2013-04-06 06:55:00 |  2.9  |  2  |
| 2013-04-06 06:56:00 |  2.4  |  3  |
| 2013-04-06 06:57:00 |  2.6  |  4  |
| 2013-04-06 06:58:00 |  2.5  |  5  |
| 2013-04-06 06:59:00 |  2.6  |  6  |
| 2013-04-06 06:54:00 |  2.8  |  7  |
| 2013-04-06 06:55:00 |  2.5  |  8  |
| 2013-04-06 06:56:00 |  2.1  |  9  |
+---------------------+-------+-----+

I want to apply group by on two columns but query below does not work for me 我想在两列上应用分组依据,但以下查询对我不起作用

SELECT * FROM table GROUP BY dateTime, value

I want distinct values from both columns. 我希望这两列都具有不同的值。 I have tried this one too but this one is not working either 我也尝试过这个,但是这个也不起作用

SELECT distinct(dateTime),value,id FROM table GROUP BY value

I want each column to be unique. 我希望每一列都是唯一的。 My Desired output is 我的期望输出是

+---------------------+-------+-----+
|       dateTime      | value |  id |
+---------------------+-------+-----+
| 2013-04-06 06:54:00 |  2.5  |  1  |
| 2013-04-06 06:55:00 |  2.9  |  2  |
| 2013-04-06 06:56:00 |  2.4  |  3  |
| 2013-04-06 06:57:00 |  2.6  |  4  |
+---------------------+-------+-----+

Can anyone help me with this? 谁能帮我这个? Thanks 谢谢

If you need to have unique values in both columns (exclude rows with dateTime or value returned in previous rows) try this query. 如果您需要在两列中都具有唯一值(排除具有dateTime的行或先前行中返回的值),请尝试执行此查询。

SELECT a.datetime, a.value, a.id FROM test a
WHERE NOT EXISTS
  (
     SELECT 1 FROM test b 
     WHERE b.id < a.id
       AND (a.datetime = b.datetime OR b.value = a.value)
  )

SQLFiddle SQLFiddle

When using grouping predicates, the values selected must be either 使用分组谓词时,选择的值必须是

a) values used to group the results a)用于将结果分组的值

b) group functions (like SUM , AVG , COUNT ..) b)组函数(如SUMAVGCOUNT ..)

do something like 做类似的事情

SELECT dateTime, value FROM table GROUP BY dateTime, value  (order is not needed to be kept).

if you did something like 如果你做了类似的事情

SELECT dateTime, value FROM table GROUP BY value

You could end with a table where, to the row with value = 1 , you would have 3 or 4 dateTime values that are different. 您可以以一个表结尾,在该表中,到value = 1的行,您将有3或4个不同的dateTime值。 And that is not a table, so the SQL would be invalid. 那不是表,因此SQL将无效。

If you want both columns to be unique, you can use GROUP or DISTINCT: 如果希望两列都唯一,则可以使用GROUP或DISTINCT:

DISTINCT 不同

SELECT DISTINCT dateTime, value FROM table

GROUP BY 通过...分组

SELECT dateTime, value FROM table GROUP BY dateTime, value

Your question is lacking clarity, so let me ask it this way. 您的问题不够清楚,所以让我这样问。

For any given date/time, that multiple entries exist, you want the value and ID of the first time it occurred. 对于任何给定的日期/时间,存在多个条目,则需要其首次出现的值和ID。 Your sample output does not have any for the :58 and :59 minute entry, so I assume you do not want any having only a single record. 您的示例输出在:58和:59分钟的输入中没有任何内容,因此我假设您不希望任何内容仅包含一条记录。

select
      YT2.dateTime,
      YT2.value,
      YT2.ID
   from
      ( select YT.dateTime, min(YT.id) MinID
           From YourTable YT
           group by YT.dateTime
           having count(*) > 1 ) as PreQuery
      JOIN YourTable YT2
         ON PreQuery.MinID = YT2.ID
        AND PreQuery.dateTime = YT2.dateTime

Now, if you wanted it based on the date/time that has the lowest VALUE, the above query would only need to be altered to 现在,如果您希望根据VALUE值最低的日期/时间进行更改,则只需将上述查询更改为

  ( select YT.dateTime, min(YT.Value) MinValue

and

     ON PreQuery.MinValue = YT2.Value

You could try this 你可以试试这个

SELECT dateTime, max(value) FROM table GROUP BY dateTime

This will give you one row for each different dateTime with the maximum value for that group of dateTime. 这将为您为每个不同的dateTime提供一行,并为该组dateTime提供最大值。 You can use alternatives to max() such as min() or sum(). 您可以使用max()的替代项,例如min()或sum()。

Without knowing precisely what you are trying to achieve, it is hard to give a more precise answer. 如果不确切知道您要实现的目标,很难给出更准确的答案。

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

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