Solved

SQL Subquery syntax

Posted on 2015-01-08
3
95 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 69

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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

726 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