1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Mysql order by date but group names

Discussion in 'MySQL' started by Jason7972, Jan 11, 2017.

  1. #1
    Hello i could need some help ;D. I tried on stackoverflow already, but there i did'nt get helpful/friendly answers :(.

    I have joined 2 tables and would order all results by the date, but grouped by the names (in my case clientid). So just the youngest date for each clientid is importand for the ORDER BY.

    It's a hard for me to explain it well so i will show you an example how it should look:
    name | datum
    -------+------------
    zebra |2017-01-09
    zebra |2017-01-10
    zebra |2017-01-11
    anton |2017-01-08
    anton |2017-01-09
    anton |2017-01-09
    maier |2017-01-09
    maier |2017-01-10
    maier |2017-01-11

    I tried already with (how it not should look):

    ORDER BY clientid, date
    Result:

    anton |2017-01-08
    anton |2017-01-09
    maier |2017-01-07
    maier |2017-01-10
    and i tried with:

    ORDER BY date, clientid
    Result:

    anton |2017-01-08
    zebra |2017-01-09
    anton |2017-01-10
    SEMrush
    I prepared a sqlliteonline with my current code and tables:
    https://sqliteonline.com/#fiddle-5875ebfcc9e621ae6b14e9ed5d1f48df91d5abd06f41d74206
     
    Jason7972, Jan 11, 2017 IP
    SEMrush
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,626
    Likes Received:
    719
    Best Answers:
    151
    Trophy Points:
    420
    #2
    What is wrong with the second query? It does what you want, unless you want something else entirely. The first example, the one you want, is not possible. You can't get the Z before the A, then followed by M. You can sort ascending or descending, per column.
     
    PoPSiCLe, Jan 11, 2017 IP
  3. Jason7972

    Jason7972 Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    1
    #3
    @PoPSiCLe because i would make a site for representative where they can see the next client they have to go for and if it doesn't show the first client with the next date it is useless. And if i sort all results by dateneed only it doesn't show all products they need (maybe if the representative bring one product he could bring the other too, so he hasn't go for twice).

    Edit: It doesn't exist a possability somehow ? I found a function wich isn't working for me but the first example is made with it.
    MD5(name)
     
    Last edited: Jan 11, 2017
    Jason7972, Jan 11, 2017 IP
  4. pmf123

    pmf123 Notable Member

    Messages:
    1,413
    Likes Received:
    75
    Best Answers:
    0
    Trophy Points:
    215
    #4
    How you word it, you only want the earliest date for teach clientid?

    ORDER BY clientid, min(date)
     
    pmf123, Feb 7, 2017 IP