Improve company productivity with a Business Account.Sign Up


SQL - Nesting two functions (SELECT DISTINCT and SUM)

Posted on 2010-09-01
Medium Priority
Last Modified: 2012-06-21

I have 2 SQL statements I want to nest yet I can't seem to be able to find the right combination

I want the sum of all the products called "Spots" from tblSurvey. I have the sql to give me the sum.

SELECT SUM(A.Volume) AS ["Total Sales"]
FROM tblSurvey AS A
WHERE A.Product='Spots';

I have another statement that gives me the product using the SELECT DISTINCT

FROM tblSurvey AS B
WHERE (((B.Product)='Spots'));

I thought I could combine the two with something like

SELECT SUM(A.Volume) AS "Total Sales"
FROM tblSurvey AS A
              FROM tblSurvey as B
              WHERE B.[Product] = 'Spots');

Except this sums all the numbers in the table (not using the WHERE criteria).

I tried using this method

SELECT  DISTINCT A.[Product], Sum(A.Volume) as "Total Sales",
FROM (SELECT B.[Product], B.[Volume]
              FROM tblSurvey as B
              WHERE B.[Product] = 'Spots') AS A;

But that produced an error. I'm pretty sure SELECT DISTINCT and the SUM Function can't be on the same statement, does anyone know how to achieve the desired result?

Product Total Sales
Spots    some #


Question by:Shino_skay
LVL 12

Accepted Solution

mcv22 earned 1000 total points
ID: 33580519
      SUM(Volume) AS [Total Sales]
      Product = 'Spots'
LVL 12

Assisted Solution

by:Missus Miss_Sellaneus
Missus Miss_Sellaneus earned 1000 total points
ID: 33580560
I'm not entirely sure what you're trying to accomplish. Is this what you needed?

SELECT product, SUM(volume) AS "Total Sales" FROM tblSurvey WHERE product='Spots' GROUP BY product ORDER BY product;

Author Comment

ID: 33580832
Silly me, that's for pointing out the GROUP BY clause which eliminates the distinct. Thanks you two.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article, we will show how to detach and attach a database and then show how to repair a corrupt database and attach it, If it has some errors. We will show how to detach and attach using SSMS or using T-SQL sentences.
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…

595 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