Question

I'm trying to run a query where it will give me one Sum Function, then select two columns from a joined table and then to group that data by the unique id i gave them. This is my original query and it works.

SELECT Sum (Commission_Paid)
FROM [INTERN_DB2].[dbo].[PaymentList]
INNER JOIN [INTERN_DB2]..[RealEstateAgentList]
ON RealEstateAgentList.AgentID = PaymentList.AgentID
WHERE Close_Date >= '1/1/2013' AND Close_Date <= '12/31/2013'
GROUP BY RealEstateAgentList.AgentID

I've tried the query below, but I keep getting an error and I don't know why. It says its a syntax error.

SELECT Sum (Commission_Paid)
FROM [INTERN_DB2].[dbo].[PaymentList]
INNERJOIN [INTERN_DB2]..[RealEstateAgentList](
Select First_Name, Last_Name
From [Intern_DB2]..[RealEstateAgentList]
Group By Last_name
)
ON RealEstateAgentList.AgentID = PaymentList.AgentID
WHERE Close_Date >= '1/1/2013' AND Close_Date <= '12/31/2013'
GROUP BY RealEstateAgentList.AgentID
Was it helpful?

Solution

Your query has multiple problems:

SELECT rl.AgentId, rl.first_name, rl.last_name, Sum(Commission_Paid)
FROM [INTERN_DB2].[dbo].[PaymentList] pl inner join
     (Select agent_id, min(first_name) as first_name, min(last_name) as last_name
      From [Intern_DB2]..[RealEstateAgentList]
      GROUP BY agent_id
     ) rl
     ON rl.AgentID = pl.AgentID
WHERE Close_Date >= '2013-01-01' AND Close_Date <= '2013-12-31'
GROUP BY rl.AgentID, rl.first_name, rl.last_name;

Here are some changes:

  • INNERJOIN --> inner join.
  • Fixed the syntax of the subquery next to the table name.
  • Removed columns for first and last name. They are not used.
  • Changed the subquery to include agent_id.
  • Added agent_id, first_name, and last_name to the outer aggregation, so you can tell where the values are coming from.
  • Changed the date formats to a less ambiguous standard form.
  • Added table alias for subquery.

I suspect the subquery on the agent list is not important. You can probably do:

SELECT rl.AgentId, rl.first_name, rl.last_name, Sum(pl.Commission_Paid)
FROM [INTERN_DB2].[dbo].[PaymentList] pl inner join
     [Intern_DB2]..[RealEstateAgentList] rl
     ON rl.AgentID = pl.AgentID
WHERE pl.Close_Date >= '2013-01-01' AND pl.Close_Date <= '2013-12-31'
GROUP BY rl.AgentID, rl.first_name, rl.last_name;

EDIT:

I'm glad this solution helped. As you continue to write queries, try to always do the following:

  1. Use table aliases that are abbreviations of the table names.
  2. Always use table aliases when referring to columns.
  3. When using date constants, either use "YYYY-MM-DD" format or use convert() to convert a string using the specified format. (The latter is actually the safer method, but the former is more convenient and works in almost all databases.)
  4. Pay attention to the error messages; they can be informative in SQL Server (unfortunately, other databases are not so clear).
  5. Format your query so other people can understand it. This will help you understand and debug your queries as well. I have a very particular formatting style (which no one is going to change at this point); the important thing is not the particular style but being able to "see" what the query is doing. My style is documented in my book "Data Analysis Using SQL and Excel.

There are other rules, but these are a good way to get started.

OTHER TIPS

SELECT Sum (Commission_Paid)
FROM [INTERN_DB2].[dbo].[PaymentList] pl
INNER JOIN (
    Select First_Name, Last_Name
    From [Intern_DB2]..[RealEstateAgentList]
    Group By Last_name
) x ON x.AgentID = pl.AgentID
WHERE Close_Date >= '1/1/2013' 
    AND Close_Date <= '12/31/2013'
GROUP BY RealEstateAgentList.AgentID

This is how the query should look... however, if you subquery first and last name, you'll also have to include them in the group by. Assuming Close_Date is in the PaymentList table, this is how I would write the query:

SELECT 
    al.AgentID, 
    al.FirstName, 
    al.LastName, 
    Sum(pl.Commission_Paid) AS Commission_Paid
FROM [INTERN_DB2].[dbo].[PaymentList] pl
INNER JOIN [Intern_DB2].dbo.[RealEstateAgentList] al ON al.AgentID = pl.AgentID
WHERE YEAR(pl.Close_Date) = '2013'
GROUP BY al.AgentID, al.FirstName, al.LastName

Subqueries are evil, for the most part. There's no need for one here, because you can just get the columns from the join.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top