Jump to content

[SOLVED] Some fun MySQL


gevans

Recommended Posts

I'm doing some reporting for a company, one of the reports is 'Sessions attended ranking list (Top 50)'. My query for this is;

 

SELECT `sessionname`, COUNT(`tabbookings`.`id`) AS book_count
FROM `tabsessions`, `tabbookings`
WHERE `tabsessions`.`id`=`tabbookings`.`sessionid` AND `tabbookings`.`attended` = 1
GROUP BY `sessionname`
ORDER BY `book_count` DESC, `sessionname`
LIMIT 50

 

This works beautifully (I'm hoping not to disclose the table structures), but I want to work out percentages for each group as well. Can anyone see a way of doing this within the single query? (remembering that there is more than 50 sessions, and they all need to be involved in the percentage calculation).

 

My thought is to count all bookings that have been attended in a different query and just do a simple equation from that.

 

<?php
$attended_sessions = 2500; //from database
$first_result = 20; //from database

$percentages = (100/2500)*20;//run this for all 50

 

 

anybody got a better solution?

Link to comment
Share on other sites

Well, I think I've got it in a single query, though I still need to do the;

 

$percentages = (100/2500)*20;//run this for all 50

 

For anyone that's interested here's the query;

 

SELECT `sessionname`, COUNT(`tabbookings`.`id`) AS book_count,
(SELECT COUNT(`tabbookings`.`id`) 
FROM tabbookings, tabsessions 
WHERE `tabsessions`.`id`=`tabbookings`.`sessionid` 
AND `tabbookings`.`attended` = 1) AS tot
FROM `tabsessions`, `tabbookings`
WHERE `tabsessions`.`id`=`tabbookings`.`sessionid` AND `tabbookings`.`attended`=1
GROUP BY `sessionname`
ORDER BY `book_count` DESC, `sessionname`
LIMIT 50

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.