sql - How does one query for count() on a series of ranges at once? -
I am trying to break forum rank on a phpBB forum. Forum ranks are defined by your postcount: if you have between 1-9 positions, then you are level 1; 10-24 and you level 2; And so on. (Maximum post is anything above 2000 posts.)
Right now, my forum rank statistics page is just a bunch of dumb queries:
SELECT COUNT (*) _Users From where the postcount & gt; = 1 and postcount & lt; 10; Select the Count (*) after WHOE from _USERS, Postcount & gt; = 10 & postcount & lt; 25; ... and so on.
Questions run in a few seconds, so I'm really only curious (for future reference) A better way to solve this problem - It seems like there should be a query that I can do Which will return its results:
rank_id | Count 1 5000 2 2000 ... I have another table in which there is a list of ranks and the minimum number of posts required to get that rank are sure that if it is necessary or If there is one more clever way to write my questions. I am using a MySQL DB if it does any help, but I am more interested in cross-database reply.
Paste a union between them.
SELECT COUNT (*) _Users where postcount & gt; = 1 and postcount & lt; 10; Select the union COUNT (*) _users Postcount from WHERE & gt; = 10 and post_count & lt; 25;
Alternatively, you can use a lawsuit to stack them and get results in one line.
between SELECT SUM (case when post_count> = 1 and postcount <10 THEN 1 ELSE 0 END) as_1_10 SUM (case when postcount = 10 and postcount & lt ; 25 THEN 1 ELSE 0 END) as between 270000 and FROM _users between the_10_25 SUM (in the case of postcode> = 25 and postcount & lt; 100 THEN 1 ELSE 0 END)
or another way to do this ...
SELECT Count (*), range FROM (select when post_count> = 1 and postcode & lt; 10 THEN '1_To_10' WHEN Postcount & gt; = 10 & Postcount & lt; 25 THEN '10_To_25' WHEN Stkount & gt; = 25 and Postkount & lt; 100 THEN '25_To_100' ELSE 'Over_100' END of _Users InnerTable as category) GROUP BY category
Comments
Post a Comment