0

I have two tables in a database. One has a list of accounts with various datapoints by account (fix_cost_summary). The second table has all trades done by all accounts and the commissions associated with the trades (comm_table). I am trying to run a query to output all columns from fix_cost_summary and a sum of commissions by account from comm_table.

My subquery is causing an "Unknown Column" error. Please let me know if there is an error in my code.

SELECT 
      fix_cost_summary.*,
     comm_table.Short_Name, 
     (SELECT SUM(Commissions) 
        FROM
             comm_table     
        GROUP BY 
             comm_table.Short_Name)
FROM
     fix_cost_summary,
     comm_table
WHERE 
     fix_cost_summary.Short_Name = comm_table.Short_name
3
  • Are you sure all columns actually exist? This seems to work. Commented May 16, 2013 at 18:13
  • 1
    @JoachimIsaksson: But as soon as there are records with multiple Short_Name values in comm_table, the subquery will return multiple results which will produce ER_TOO_MANY_ROWS (albeit not the error cited by the OP). Commented May 16, 2013 at 18:16
  • @eggyal Touché, I was only attempting to reproduce the error in the question. Commented May 16, 2013 at 18:41

2 Answers 2

2

Assuming that Short_Name is unique in fix_cost_summary, MySQL will allow you to simply join the tables and then group on that column:

SELECT   fix_cost_summary.*, SUM(comm_table.Commissions)
FROM     fix_cost_summary JOIN comm_table USING (Short_Name)
GROUP BY Short_Name
Sign up to request clarification or add additional context in comments.

4 Comments

IF I wanted to add a WHERE clause to the SUM command, how would I do that? I want to SUM(comm_table.Commissions) for a date range comm_table.Date > X and comm_table.Date < Y
@Steve: Add your WHERE clause just before the GROUP BY clause.
There are some accounts listed in the fix_cost_summary table that are not in the comm_table (accounts that haven't done any commissions). I still want them to show up in the result. I made it a LEFT JOIN which I thought would solve it but it hasn't. Any suggestions?
@Steve: Yes, you have the correct idea with a LEFT JOIN, but you will need to move the filter (currently in your new WHERE clause) into the JOIN criteria: change USING (Short_Name) to ON fix_cost_summary.Short_Name = comm_table.Short_Name AND comm_table.Date > X AND comm_table.Date < Y; you will then also need to qualify the reference to Short_Name with a table name in the GROUP BY clause: GROUP BY fix_cost_summary.Short_Name. Note that you can make the whole thing somewhat less verbose by aliasing the tables.
0

The inline subquery will return more than 1 rows. One way to solve this, is to change it into a derived table:

SELECT 
      f.*,
      c.Sum_Commisions
FROM
      fix_cost_summary AS f
   JOIN
      ( SELECT Short_Name,
               SUM(Commissions) AS Sum_Commisions
        FROM
               comm_table     
        GROUP BY 
               Short_Name
      ) AS c 
         ON c.Short_Name = f.Short_Name ;

2 Comments

You're missing the join condition.
@Barmar thnx. It didn't look right when I posted but I couldn't figure out why ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.