简体   繁体   中英

Select highest and lowest date

I now have a query like this:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, tag AS `TAG`, value AS `VALUE`, addedTs as `addedTs`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"

I know get a result like this:

DATE        TAG             VALUE                   addedTs 
4 April     calls           3                       2014-04-04 20:32:39
4 April     endTS           2014-04-04 22:40:55     2014-04-04 22:40:55
4 April     kmstart         32                      2014-04-04 18:34:37
4 April     kmstop          34434334                2014-04-04 18:35:30
4 April     startTS         2014-04-04 18:35:00     2014-04-04 18:35:00
4 April     visits          6                       2014-04-04 22:40:53
5 April     endTS           2014-04-05 01:37:41     2014-04-05 01:37:41
5 April     kmstart         30                      2014-04-05 01:37:41
5 April     startTS         2014-04-05 01:13:25     2014-04-05 01:13:25
5 April     visits          1                       2014-04-05 01:13:23
7 April     authorizations  4                       2014-04-07 22:34:37
7 April     calls           9                       2014-04-07 23:18:28
7 April     endTS           2014-04-07 23:35:32     2014-04-07 23:35:32
7 April     kmstart         42                      2014-04-07 18:10:43
7 April     kmstop          55                      2014-04-07 19:37:42
7 April     startTS         2014-04-07 10:45:18     2014-04-07 10:45:18
7 April     visits          6                       2014-04-07 23:35:31
8 April     endTS           2014-04-08 11:24:28     2014-04-08 11:24:28
8 April     startTS         2014-04-08 11:24:28     2014-04-08 11:24:28

Now I would also like to select highest date from the psttodo-uit table. This is what I want to do:

In the psttodo-uit table there is a Hostess Code ( == Code in my WHERE clausule ). There is also a afgewerkt tablet datum field in the table of datetype datetime .

The dates variate like so:

2014-04-04 11:41:22
2014-04-03 17:20:01
2014-04-01 15:12:08
2014-04-07 22:20:47
2014-04-08 11:21:18

Now when I do a query I would like to select the max time of the specific day. I tried it like this:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, tag AS `TAG`, value AS `VALUE`, addedTs as `addedTs`, MAX(`afgewerkt tablet datum`) AS `TimeSorE`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"

But then I got this:

4 April calls   3   2014-04-04 20:32:39 2014-04-09 11:49:42

Two problems:

  • I only get one result back
  • The date is not 4 April

Is there someone who can help me with this?

UPDATE:

What I've tried:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, tag AS `TAG`, value AS `VALUE`, addedTs as `addedTs`, MAX(`afgewerkt tablet datum`) AS `TimeSorE`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"
GROUP BY DATE_FORMAT(`Datum`, '%e %M')

Gave me this:

4 April calls   3   2014-04-04 20:32:39 2014-04-09 15:46:04
5 April endTS   2014-04-05 01:37:41 2014-04-05 01:37:41 2014-04-09 15:46:04
7 April authorizations  4   2014-04-07 22:34:37 2014-04-09 15:46:04
8 April endTS   2014-04-08 11:24:28 2014-04-08 11:24:28 2014-04-09 15:46:04

But I need them all (with all tags).

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`, GROUP_CONCAT(tag) AS `TAG`, value AS `VALUE`, addedTs as `addedTs`, MAX(`afgewerkt tablet datum`) AS `TimeSorE`
FROM dailyMessage
INNER JOIN `psttodo-uit`
ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
WHERE Code = "010"
GROUP BY DATE_FORMAT(`Datum`, '%e %M')

IF you need the result set as seperate rows try this

SELECT A.DATE ,A.CODE,TAG,addedTs,TimeSorE
(
SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`,dailyMessage.Code AS CODE
  MAX(`afgewerkt tablet datum`) AS `TimeSorE`
    FROM dailyMessage
    INNER JOIN `psttodo-uit`
    ON dailyMessage.Code = `psttodo-uit`.`Hostess Code`
    WHERE Code = "010"
    GROUP BY DATE_FORMAT(`Datum`, '%e %M')
) A , dailyMessage B

WHERE
A.CODE = B.CODE AND
A.DATE = B. DATE_FORMAT(`Datum`, '%e %M')

This is absolutely works for me:

SELECT 
  DATE_FORMAT(`Date`, '%e %M') AS `DATE`, 
  tag AS `TAG`, 
  value AS `VALUE`, 
  addedTs as `addedTs`, 
  MAX(`afgewerkttabletdatum`) AS `TimeSorE`
FROM Test
GROUP BY DATE_FORMAT(`Date`, '%e %M')

Here is an example:

SQLFiddle Demo

I think this gonna work:

SELECT DATE_FORMAT(`Datum`, '%e %M') AS `DATE`,
       tag AS `TAG`,
       value AS `VALUE`,
       addedTs AS `addedTs`,
       t2.`TimeSorE`
FROM dailyMessage
INNER JOIN (SELECT `Hostess Code`,
                   MAX(`afgewerkt tablet datum`) AS `TimeSorE`
            FROM `psttodo-uit`
            GROUP BY `Hostess Code`) t2 ON dailyMessage.Code = t2.`Hostess Code`
WHERE Code = "010"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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