select views by max and created_at column

Discussion in 'Databases' started by bumbar, Jan 11, 2013.

  1. #1
    Hello,

    I have this table:

    id created_at title views
    ---|-------------------|--------|---------
    1 | 2012-12-25 | john | 399
    2 | 2012-11-23 | juliet | 244
    5 | 2012-11-12 | borat | 555
    7 | 2012-07-05 | osama | 600
    8 | 2012-06-25 | borat | 345
    9 | 2012-04-25 | borat | 403
    10 | 2012-02-25 | gorat | 123
    11 | 2012-03-22 | obama | 675

    How do I select so as to get the first 5 highest titles ordered by the most recent date?

    I use this query, but not work...

    
    SELECT id, title, created_at, MAX( views )
    FROM article
    GROUP BY created_at
    LIMIT 5 
    
    PHP:
     
    bumbar, Jan 11, 2013 IP
  2. king_cantona

    king_cantona Greenhorn

    Messages:
    17
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Try this
    SELECT id, title, created_at, MAX( views )
    FROM article
    GROUP BY id, title, created_at
    LIMIT 5
    Code (markup):
     
    king_cantona, Jan 14, 2013 IP
  3. c4ntaloop

    c4ntaloop Greenhorn

    Messages:
    14
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    13
    #3
    In T-SQL

    
    SELECT TOP 5 id, title, created_at, MAX( views )
    FROM article
    GROUP BY id, title, created_at
    ORDER BY Max( views) desc, created_at
    
    Code (markup):
     
    c4ntaloop, Jan 15, 2013 IP