Nick
Nick

Reputation: 783

MySQL Correlated Subqueries

I've come to find out that the following SQL is not possible due to correlated subqueries within MySQL not having access to the outer query, when I run the below query I get the error of "player.playerid not found"

SELECT player.position, player.playerid, adp.average_draft_position FROM player, (
    SELECT ((ad_p.mult + ad_p.pick_sum) / ad_p.total_mocks) AS average_draft_position FROM (
        SELECT draft_c.total_mocks as total_mocks, 
               ((draft_c.total_mocks - player_c.total_picks) * 252) AS mult, 
               SUM(overall_pick) AS pick_sum FROM draft_history JOIN drafts ON drafts.id = draft_history.draftid, 
            (
                SELECT COUNT(drafts.id) AS total_mocks FROM drafts WHERE type = 3
            ) AS draft_c, (
                SELECT COUNT(draft_history.playerid) AS total_picks FROM draft_history WHERE draft_history.playerid = player.playerid
            ) AS player_c WHERE draft_history.playerid = player.playerid
    ) AS ad_p
) AS adp WHERE player.sportid = 1 AND player.position IN ('x', 'y', 'z') ORDER BY adp.average_draft_position ASC

I am attempting to use the following as a subquery in the above,

SELECT ((adp.mult + adp.pick_sum) / adp.total_mocks) AS average_draft_position FROM (
            SELECT draft_c.total_mocks as total_mocks, ((draft_c.total_mocks - player_c.total_picks) * 252) AS mult, SUM(overall_pick) AS pick_sum FROM draft_history JOIN drafts ON drafts.id = draft_history.draftid, 
            (
                SELECT COUNT(id) AS total_mocks FROM drafts WHERE type = :type
            ) AS draft_c, (
                SELECT COUNT(playerid) AS total_picks FROM draft_history WHERE playerid = :playerid
            ) AS player_c WHERE playerid = :playerid
        ) as adp

This will determine the average position a user has been picked throughout history, including cases when the user was not selected by applying a weight to their total sum, this one works.

The first query above is attempting to take that and select all users that currently exist in the given positions get the average position and sort by it.

I'm not so great at SQL so I could probably be doing this better and If I could explain this better please let me know.

Problem solved with the given query,

SELECT position, 
       playerid,
       ((((adp.total_mocks - adp.participated_mocks) * 252) + adp.pick_sum) / adp.total_mocks) AS average_draft_position
FROM 
(      
    SELECT player.position, 
           player.playerid,
           (SELECT COUNT(drafts.id) FROM drafts WHERE type = 3) as total_mocks,
           COUNT(draft_history.playerid) as participated_mocks,
           SUM(overall_pick) AS pick_sum 
    FROM draft_history 
    INNER JOIN drafts 
      ON drafts.id = draft_history.draftid
    INNER JOIN player
      ON player.sportid = 1
     AND player.position IN ('QB') 
     AND draft_history.playerid = player.playerid
    GROUP BY player.position, player.playerid
) adp ORDER BY average_draft_position ASC 

Upvotes: 0

Views: 246

Answers (1)

carl
carl

Reputation: 188

I think I can understand this. try this:

select position, 
       playerid,
       ((adp.mult + adp.pick_sum) / adp.total_mocks) AS average_draft_position
from 
(      
    SELECT player.position, 
           player.playerid,
           (SELECT COUNT(drafts.id) FROM drafts WHERE type = 3) as total_mocks
           (
                (SELECT COUNT(drafts.id) FROM drafts WHERE type = 3) 
              - COUNT(draft_history.playerid)
           )*252 as mult,
           SUM(overall_pick) AS pick_sum 
    FROM draft_history 
    INNER JOIN drafts 
      ON drafts.id = draft_history.draftid
    INNER JOIN player
      ON player.sportid = 1
     AND player.position IN ('x', 'y', 'z') 
     AND draft_history.playerid = player.playerid
    group by player.position, player.playerid
) adp
order by 3 asc

if "(SELECT COUNT(drafts.id) FROM drafts WHERE type = 3)" is a constant ,you can :

SELECT COUNT(drafts.id) FROM drafts WHERE type = 3 into @total_mocks;

then use @total_mocks replace the expression "(SELECT COUNT(drafts.id) FROM drafts WHERE type = 3)"

just try it

Upvotes: 1

Related Questions