Reputation: 2461
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
Upvotes: 0
Views: 1090
Reputation: 361
If you need to have unique values in both columns (exclude rows with dateTime or value returned in previous rows) try this query.
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)
)
Upvotes: 1
Reputation: 4158
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. You can use alternatives to max() such as min() or sum().
Without knowing precisely what you are trying to achieve, it is hard to give a more precise answer.
Upvotes: 0
Reputation: 48139
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. 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.
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
( select YT.dateTime, min(YT.Value) MinValue
and
ON PreQuery.MinValue = YT2.Value
Upvotes: 0
Reputation: 361
If you want both columns to be unique, you can use GROUP or DISTINCT:
DISTINCT
SELECT DISTINCT dateTime, value FROM table
GROUP BY
SELECT dateTime, value FROM table GROUP BY dateTime, value
Upvotes: 0
Reputation: 24885
When using grouping predicates, the values selected must be either
a) values used to group the results
b) group functions (like SUM
, AVG
, COUNT
..)
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. And that is not a table, so the SQL would be invalid.
Upvotes: 0