[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL - Nesting two functions (SELECT DISTINCT and SUM)

Posted on 2010-09-01
3
Medium Priority
?
570 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
[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 12

Accepted Solution

by:
mcv22 earned 1000 total points
ID: 33580519
SELECT
    Product,
      SUM(Volume) AS [Total Sales]
FROM
      tblSurvey
WHERE
      Product = 'Spots'
GROUP BY
      Product
0
 
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;
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

649 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