Welcome Guest, Not a member yet? Register   Sign In
sub query problems SOLVED
#1

[eluser]srpurdy[/eluser]
Hi,

I have a query in sql that is grabbing a bunch of various information, some of these fields are doing calculations using sum()

I'd like to be able to limit the sum results. So that for example instead of getting the total from all records that fit the query criteria, to only get total of an X amount of records.

So for a quick example of what I'd like it to do is say I have 5 rows with
10,10,10,10,10 -> result would be 50. Id like to limit that to say 3 rows 10,10,10 -> result is 30. however I don't want to limit the rows outputed, only limit the total

I figured I could do this using a sub query but I can't seem to figure out the right syntax using the query I already have constructed below. Pretty much every single one of those sum() needs to have that limit factor. Any help on direction on how this could be done would be appreciated.

Code:
function get_champ()
    {
        $show_champ = $this->db
            ->where('ilr_champ.season_id', $this->uri->segment(4))
            ->where('ilr_season.season_id', $this->uri->segment(4))
            ->where('ilr_event_result.iracing_cust_id = ilr_champ.iracing_cust_id')
            ->where('ilr_event_result.schedule_id = ilr_champ.schedule_id')
            ->where('ilr_champ.schedule_id = ilr_events.schedule_id')
            ->where('ilr_events.track_id = ilr_tracks.track_id')
            ->select('
                ilr_event_result.iracing_cust_id,
                ilr_event_result.driver_title,
                ilr_events.track_id,
                ilr_tracks.turns,
                ilr_season.drop_races,
                sum(ilr_champ.points - ilr_champ.pen_pts + ilr_champ.bonus_pts) as champ_points,
                sum(ilr_champ.laps) as laps_total,
                sum(ilr_champ.led) as led_total,
                sum(ilr_champ.inc) as inc_total,
                sum(ilr_champ.wins) as wins_total,
                sum(ilr_champ.poles) as poles_total,
                sum(ilr_champ.top5) as top5_total,
                sum(ilr_champ.starts) as starts_total,
                avg(ilr_champ.avg_s) as avg_s_total,
                avg(ilr_champ.avg_f) as avg_f_total,
                sum(ilr_champ.km * ilr_champ.laps) as km_total,
                sum(ilr_champ.miles * ilr_champ.laps) as miles_total
            ')
            ->from('
                ilr_event_result,
                ilr_champ,
                ilr_tracks
            ')
            ->join('ilr_season','ilr_season.season_id = ilr_champ.season_id')
            ->join('ilr_events', 'ilr_events.track_id = ilr_tracks.track_id')
            ->group_by('ilr_event_result.iracing_cust_id')
            ->order_by('champ_points desc, driver_title asc')
            ->get();
        return $show_champ->result();


Messages In This Thread
sub query problems SOLVED - by El Forum - 04-27-2010, 10:48 PM
sub query problems SOLVED - by El Forum - 04-28-2010, 02:49 AM
sub query problems SOLVED - by El Forum - 04-28-2010, 08:48 AM
sub query problems SOLVED - by El Forum - 04-28-2010, 03:18 PM
sub query problems SOLVED - by El Forum - 05-01-2010, 06:00 PM
sub query problems SOLVED - by El Forum - 05-02-2010, 06:10 PM
sub query problems SOLVED - by El Forum - 05-04-2010, 09:28 AM
sub query problems SOLVED - by El Forum - 05-08-2010, 01:45 PM
sub query problems SOLVED - by El Forum - 05-10-2010, 11:28 AM
sub query problems SOLVED - by El Forum - 05-10-2010, 11:37 AM
sub query problems SOLVED - by El Forum - 05-10-2010, 11:57 AM
sub query problems SOLVED - by El Forum - 06-07-2010, 06:06 AM
sub query problems SOLVED - by El Forum - 06-07-2010, 06:11 AM
sub query problems SOLVED - by El Forum - 06-07-2010, 06:11 AM
sub query problems SOLVED - by El Forum - 06-07-2010, 07:15 AM



Theme © iAndrew 2016 - Forum software by © MyBB