Forum Moderators: coopster
select site_id, page_id, count(points) as SScore from search_database group by concat(site_id,page_id) order by SScore DESC
How can i alter this query to give me a maximum of 2 results per site_id, now it returns unlimited results per site_id. It only should return the first 2 page_id 's from the site_id it encounters after grouping and ordering because they are ranking the higest.
Thanks for any help.
Dooing the with a query would be "the bomb"...
Basicly it needs to count the site_id's in the result set and use something like "where count(site_id) < 3"
but i don't know how to count the site_id's in the result set.
Like i said, i am searching for this for a couple of months now and also trying to create a query myself, so i know it is not easy...
Thanks for taking the time to post an answer.
No point in giving you all that since i still use url_id , i will make the page_id and site_id after i have a working query for limiting to the top 2 page_id's of every site_id.
But thanks for reading
<nevermind doesn't seem to work>
You said it all right there. You are trying to push report level-breaking logic back into the result set returned by your query. If it can be done in a single statement, it is beyond my current level of knowledge. It can be done using a temporary table and a couple user variables:
CREATE TEMPORARY TABLE tmp(
site_id SMALLINT UNSIGNED NOT NULL,
page_id SMALLINT UNSIGNED NOT NULL,
points SMALLINT UNSIGNED NOT NULL,
counter SMALLINT UNSIGNED NOT NULL,
prev_site_id SMALLINT UNSIGNED
);
--
--
INSERT INTO tmp
SELECT
site_id,
page_id,
SUM(points) AS SScore,
0,
NULL
FROM search_database
GROUP BY site_id, page_id
ORDER BY site_id, SScore DESC, page_id
;
--
-- SELECT * FROM tmp;
--
SET @prev_site_id = 0;
SET @counter = 0;
--
--
UPDATE tmp
SET
counter = @counter:= IF(@prev_site_id <> site_id, 1, @counter + 1),
prev_site_id = @prev_site_id:= IF(site_id <> @prev_site_id, site_id, @prev_site_id)
;
--
-- SELECT * FROM tmp;
--
SELECT site_id, page_id, points FROM tmp WHERE counter <= 2;
--
--
DROP TABLE tmp;
while experimenting i got this error message:
INSERT TABLE 'temp' isn't allowed in FROM table list
i searched and then found this:
Mysql prior ver. 4.0.15 does not support insertion with subselect taken from the same table.
This suggest it is possible after that version.
I have 4.0.17 but i get a error. Maybe mean 4.1 have you got any idea?
create temp
select site_id , count( (select site_id from temp) ) from search_database
or:
create temp
select site_id , (select count(site_id) from temp) from search_database
Thank you.
Currently i am using:
$site_id=$get_info['site_id'];
$$site_id++;
if (${$site_id}<3)
{$id[$nummer]=$get_info['id'];
$nummer++;}
1137 ERROR -- Can't reopen table: temp. That's why you need to set a user-defined variable and increment/reset it yourself as I described in my post earlier -- I couldn't think of any other way to get around it.
insert into test SELECT a.id, a.site_id, count( c.page_id )
FROM id_super_fast_database a, test c
GROUP BY a.id
HAVING count( c.page_id ) < 3
LIMIT 10
CREATE TABLE `test` (
`ID` int(5) NOT NULL default '0',
`page_id` int(11) NOT NULL default '0',
`int` int(5) NOT NULL default '0'
) TYPE=MyISAM;
the table test already needs to already exist and have al least 1 record in it or else it wont select or insert anything.
tested on mysql 4.0.17 .
Dont ask me how...