Jump to content

[SOLVED] help need to tweak this query...


jasonc

Recommended Posts

i have the following query which works but only for one type of date range.

 

i have my datesent like this in other entries...

 

'2008-10-12'

'2007-11-06'

'2007-06-27'

 

and so on

 

but would like to have just the entries of the most hits in each month.

 

but all on one query instead of using the for/next for each possible month that has gone by!

 

could someone kindly advise what the best query would be to do this.

 

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2008-10%' ORDER BY 'hits' DESC LIMIT 1

 

my table looks like this...

 

CREATE TABLE IF NOT EXISTS `entries` (
  `id` int(11) NOT NULL auto_increment,
  `datesent` datetime NOT NULL default '0000-00-00 00:00:00',
  `hits` int(11) NOT NULL default '0',
  `user` text NOT NULL,
  KEY `id` (`id`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=6086 

 

 

thanks

Link to comment
Share on other sites

hi

 

the hits part works ok, it is the datesent part that i need help with

 

like this...

 

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2006-01%' ORDER BY 'hits' DESC LIMIT 1

...

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2006-08%' ORDER BY 'hits' DESC LIMIT 1

...

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2006-10%' ORDER BY 'hits' DESC LIMIT 1

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2006-12%' ORDER BY 'hits' DESC LIMIT 1

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2007-03%' ORDER BY 'hits' DESC LIMIT 1

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2007-06%' ORDER BY 'hits' DESC LIMIT 1

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2007-07%' ORDER BY 'hits' DESC LIMIT 1

....

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2008-10%' ORDER BY 'hits' DESC LIMIT 1

SELECT id, hits, user FROM `entires` WHERE `datesent` LIKE '2008-11%' ORDER BY 'hits' DESC LIMIT 1

 

 

 

but in one query for all months that are in the DB

Link to comment
Share on other sites

SELECT entries.id, entries.hits, entries.user, DATE_FORMAT(entries.datesent, '%Y-%m) as yrmonth
FROM entries
INNER JOIN (
        SELECT DATE_FORMAT(entries.datesent, '%Y-%m) as yrmonth, MAX(hits) as hits
        FROM entries
        GROUP BY yrmonth
    ) as x USING (yrmonth, hits)

 

Not sure if this will work but give it a try. I used Barands code and got rid of the table alias.

Link to comment
Share on other sites

i tried that and also noticed if i am correct missing '  in the code?

 

but now i get the error..

 

Column 'hits' in field list is ambiguous

 

 

SELECT entries.id, entries.hits, entries.user, DATE_FORMAT(entries.datesent, '%Y-%m') as yrmonth

FROM entries

INNER JOIN (

        SELECT DATE_FORMAT(entries.datesent, '%Y-%m') as yrmonth, MAX(hits) as hits

        FROM entries

        GROUP BY yrmonth

    ) as x USING (yrmonth, hits)

 

 

also when i use the above i get this too

 

 

'database_name.entries.yrmonth' in 'on clause'

 

 

i get the Column 'hits' in field list is ambiguous error when i take out the 'entries.' from the code

 

 

using the code and not adding the ' to the two places i get this error...

 

near '%m) as yrmonth, MAX.....

Link to comment
Share on other sites

SELECT entries.id, entries.hits, entries.user, DATE_FORMAT(entries.datesent, '%Y-%m') as yrmonth
FROM entries
INNER JOIN (
        SELECT DATE_FORMAT(entries.datesent, '%Y-%m') as yrmonth, MAX(entries.hits) as max_hits
        FROM entries
        GROUP BY yrmonth
    ) as x USING (yrmonth, max_hits)

Link to comment
Share on other sites

added the '  after both of the %m

 

 

Unknown column 'DB_name.e.yrmonth' in 'on clause'

 

using

SELECT e.id, e.hits, e.user, DATE_FORMAT(e.datesent, '%Y-%m') as yrmonth
FROM entires e
INNER JOIN (
        SELECT DATE_FORMAT(datesent, '%Y-%m') as yrmonth, MAX(hits) as hits
        FROM entires
        GROUP BY yrmonth
    ) as x USING (yrmonth, hits)

Link to comment
Share on other sites

plan B

SELECT e.id, e.hits, e.user, DATE_FORMAT(e.datesent, '%Y-%m') as `yrmonth`
FROM entires e
INNER JOIN (
        SELECT DATE_FORMAT(datesent, '%Y-%m') as `yrmonth`, MAX(hits) as hits
        FROM entires
        GROUP BY `yrmonth`
    ) as x ON DATE_FORMAT(e.datesent, '%Y-%m') = x.`yrmonth`  AND e.hits = x.hits

Link to comment
Share on other sites

plan B

SELECT e.id, e.hits, e.user, DATE_FORMAT(e.datesent, '%Y-%m') as `yrmonth`
FROM entires e
INNER JOIN (
        SELECT DATE_FORMAT(datesent, '%Y-%m') as `yrmonth`, MAX(hits) as hits
        FROM entires
        GROUP BY `yrmonth`
    ) as x ON DATE_FORMAT(e.datesent, '%Y-%m') = x.`yrmonth`  AND e.hits = x.hits

 

thats the one,  cheers, thanks so much for your help

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.