2

I am not sure if this is doable, i have a table of data with timestamp

e.g.

apple    2013-01-02 06:24:22
banana    2013-01-12 11:08:22
apple    2013-01-02 15:24:22
apple    2013-01-02 12:30:16
banana    2013-01-14 18:34:22

i want to:

  1. rearrange the data base on distinct date not timetamp, means all same date will group together
  2. if possible i hope to achieve the below in final result

    apple     2013-01-02 06:24:22  2013-01-02 15:24:22  // get first and last data
    banana    2013-01-12 11:08:22  2013-01-12 11:08:22  // only one data in the group
    banana    2013-01-14 18:34:22  2013-01-14 18:34:22  // only one data in the group
    

How can i do this with minimum sql queries instead of using a loop to determine?

Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
onegun
  • 803
  • 1
  • 10
  • 27

3 Answers3

0

Try this:

SELECT fruitName, MIN(a.timestamp), MAX(a.timestamp)
FROM tableA a 
GROUP BY DATE(a.timestamp), fruitName;
Saharsh Shah
  • 28,687
  • 8
  • 48
  • 83
0

Use MySQL DATE() function.

SELECT FRUIT_NAME, DATE(mydate) AS dateStamp FROM TABLE 
GROUP BY DATE(TABLE.mydate);

Where:

FRUIT_NAME: your field for name

mydate your date field.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

This query should get you what you need. (Question's 2nd Part)

SELECT Fruit, Min(DateTime) AS FirstDateTime, Max(DateTime) AS LastDateTime FROM Table1 
GROUP BY DATE(DateTime);

This would give you the output

FRUIT      FIRSTDATETIME           LASTDATETTIME
apple     2013-01-02 06:24:22  2013-01-02 15:24:22
banana    2013-01-12 11:08:22  2013-01-12 11:08:22 
banana    2013-01-14 18:34:22  2013-01-14 18:34:22
Venkata Krishna
  • 1,768
  • 2
  • 14
  • 21