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
, andlast_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:
- Use table aliases that are abbreviations of the table names.
- Always use table aliases when referring to columns.
- 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.) - Pay attention to the error messages; they can be informative in SQL Server (unfortunately, other databases are not so clear).
- 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.