Solved

SQL Subquery syntax

Posted on 2015-01-08
3
90 Views
Last Modified: 2015-01-08
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
Comment
Question by:visionetv
3 Comments
 
LVL 68

Expert Comment

by:Qlemo
ID: 40539015
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
 
LVL 11

Accepted Solution

by:
John_Vidmar earned 500 total points
ID: 40539094
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
 

Author Closing Comment

by:visionetv
ID: 40539238
Thank you, John
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now