[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 102
  • Last Modified:

SQL Subquery syntax

Hello Experts;

Can someone help me with the syntax and/or structure of the following SQL Subquery;

declare @Salesperson AS varchar(20) =:Salesperson
declare @Salesperson2 AS varchar(20) =:Salesperson2
declare @Year AS int =:Year
declare @Month AS int =:Month

SELECT * FROM [EMON]
WHERE
EMO_YEAR = @Year  
AND
EMO_MONTH = @Month

  (SELECT EMO_SALESNAME FROM EMON,
   SUM(DISTINCT EMO_INVTAX) + SUM(DISTINCT EMO_RETTAX) AS Sales
   WHERE EMO_SALESNAME  = @Salesperson)
 
  (SELECT EMO_SALESNAME FROM EMON,
   SUM(DISTINCT EMO_INVTAX) + SUM(DISTINCT EMO_RETTAX) AS Sales
   WHERE EMO_SALESNAME  = @Salesperson2)

GROUP BY
[EMON].EMO_SALESNAME

I'm getting the message 'Incorrect syntax near the keyword 'DISTINCT'. I've tried a number of iteration with no success.

The purpose of the Query is to SUM the results of sales personnel from the columns EMO_INVTAX and EMO_RETTAX from the table named EMON by month and year. DISTINCT is required because the rows in the EMON table contain duplicate entries. There are 20 other sales personnel that will be added in additional Subqueries.

Thank you,
Visionet
0
visionetv
Asked:
visionetv
1 Solution
 
QlemoC++ DeveloperCommented:
What are the duplicates? Can you provide a small example data set and the expected result? Because the SQL as posted doesn't make sense to me.
0
 
John_VidmarCommented:
DECLARE @Salesperson AS varchar(20) =:Salesperson
DECLARE @Salesperson2 AS varchar(20) =:Salesperson2
DECLARE @Year AS int =:Year
DECLARE @Month AS int =:Month

SELECT	EMO_YEAR
,	EMO_MONTH
,	EMO_SALESNAME
,	Sales = SUM(EMO_INVTAX + EMO_RETTAX)
FROM	EMON
WHERE	EMO_YEAR = @Year  
AND	EMO_MONTH = @Month
AND	EMO_SALESNAME IN
	(	@Salesperson
	,	@Salesperson2
	)
GROUP
BY	EMO_YEAR
,	EMO_MONTH
,	EMO_SALESNAME

Open in new window

0
 
visionetvAuthor Commented:
Thank you, John
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now