Solved

SQL - Nesting two functions (SELECT DISTINCT and SUM)

Posted on 2010-09-01
3
562 Views
Last Modified: 2012-06-21
Hi,

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

SELECT DISTINCT B.Product
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
WHERE (SELECT DISTINCT B.[Product]
              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 #

Thanks


0
Comment
Question by:Shino_skay
3 Comments
 
LVL 12

Accepted Solution

by:
mcv22 earned 250 total points
ID: 33580519
SELECT
    Product,
      SUM(Volume) AS [Total Sales]
FROM
      tblSurvey
WHERE
      Product = 'Spots'
GROUP BY
      Product
0
 
LVL 11

Assisted Solution

by:Missus Miss_Sellaneus
Missus Miss_Sellaneus earned 250 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;
0
 

Author Comment

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

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

13 Experts available now in Live!

Get 1:1 Help Now