Solved

SQL Subquery syntax

Posted on 2015-01-08
3
96 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 70

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

696 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