Solved

SQL Subquery syntax

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

867 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

18 Experts available now in Live!

Get 1:1 Help Now