Home

Awesome

sql-query

Advanced relational model and sql-query example with Ruby on Rails

We want to get a list of the team that scores higher in the first half than the second half.

Models

Example

    # SELECT COUNT(*) FROM "goals" 
    # INNER JOIN "players" ON "players"."id" = "goals"."player_id" 
    # INNER JOIN "matches" ON "matches"."id" = "goals"."match_id" 
    # WHERE (matches.home_id = 1 OR matches.guest_id = 1) 
    #        AND "players"."team_id" = 1 
    #        AND (goals.minute <= '45')
    def first_half
    	team = self
    	Goal.joins(:player).joins(:match)
            .where("matches.home_id = :id OR matches.guest_id = :id", :id => team.id)
            .where( { players: { team_id: team.id } } )
            .where("goals.minute <= ?", "45").count
    end
select * from teams t where 
( 
  select count( distinct g.id) from goals g 
  inner join matches m on m.id  = g.match_id 
  inner join players p on p.team_id = t.id 
  where ( m.home_id = t.id or m.guest_id = t.id ) 
        and g.player_id = p.id
        and g.minute <= 45     
) > ( 
  select count( distinct g.id) from goals g 
  inner join matches m on m.id  = g.match_id 
  inner join players p on p.team_id = t.id 
  where ( m.home_id = t.id or m.guest_id = t.id ) 
        and g.player_id = p.id
        and g.minute > 45  
 )

Usage

Set your database

Run command

Open browser

I am using faker gem for some data

Also look :

If you want to fill your database with simple data you can run this commands