SQL: GROUP BY Clause
NickName:Tauseef Hussain Ask DateTime:2015-07-01T00:28:25

SQL: GROUP BY Clause

SELECT
    (1.0*( SELECT SUM(r.SalesVolume)
           FROM
               RawData r
               INNER JOIN Product p
                   ON r.ProductId = p.ProductId
           WHERE p.Distributor in ('TF1','WARNER')
           GROUP BY p.Distributor
         )
         /
         ( SELECT SUM(r.SalesVolume)
           FROM RawData r
         )*100)
    ;

The query above leads to an error:

Lookup Error - SQL Server Database Error: Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

Is the issue with the GROUP BY Clause? Not sure how to go about this.

Copyright Notice:Content Author:「Tauseef Hussain」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/31143589/sql-group-by-clause

Answers
Jeffrey Wieder 2015-06-30T16:32:21

This part of your query is the problem:\n\nSELECT SUM(r.SalesVolume) FROM RawData r\nINNER JOIN Product p\nON r.ProductId = p.ProductId\nWHERE p.Distributor in ('TF1','WARNER') GROUP BY p.Distributor\n ^^^^^^^^^^^^^^^^^^^^^^\n\n\nYou are Grouping BY p.Distributor and there could possibly be two of those based on your where clause. WHERE p.Distributor in ('TF1','WARNER'). You can take the group by out and it will have a total SUM of both TF1 and WARNER\n\nSELECT\n(1.0 * (SELECT SUM(r.SalesVolume) FROM RawData r\n INNER JOIN Product p ON r.ProductId = p.ProductId\n WHERE p.Distributor in ('TF1','WARNER')\n) / (SELECT SUM(r.SalesVolume) FROM RawData r)*100);\n\n\nIf you are meaning to get a percentage of sales volume by distributor you should use this instead.\n\nSELECT p.Distributor, SUM(r.SalesVolume) as Sales, ROUND((SUM(r.SalesVolume) / (SELECT SUM(r.SalesVolume) FROM RawData r) * 100), 1) as percentOfTotal\nFROM RawData r\nINNER JOIN Product p ON r.ProductId = p.ProductId\nWHERE p.Distributor in ('TF1','WARNER')\nGROUP BY p.Distributor\n\n\nI suggest using the ROUND function here. The above query rounds to a single digit after the decimal point. If you want two digits use this:\n\nROUND((SUM(r.SalesVolume) / (SELECT SUM(r.SalesVolume) FROM RawData r) * 100), 2) as percentOfTotal\n",


Rowland Shaw 2015-06-30T16:32:12

Your correlated query:\n\nSELECT SUM(r.SalesVolume) \nFROM RawData r\nINNER JOIN Product p\n ON r.ProductId = p.ProductId\nWHERE p.Distributor in ('TF1','WARNER')\nGROUP BY p.Distributor)\n\n\nWill return a row for each group, i.e. one for TF1, one for WARNER.\n\nYou need to not group by any field that will lead to multiple rows",


More about “SQL: GROUP BY Clause” related questions

Is the GROUP BY clause in SQL redundant?

Whenever we use an aggregate function in SQL (MIN, MAX, AVG etc), we must always GROUP BY all non-aggregated columns, for instance: SELECT storeid, storename, SUM(revenue), COUNT(*) FROM Sales GR...

Show Detail

using group by clause in subquery in sql

I am trying to use group by clause in subquery which is in from clause select userID,count(id) from ( ( select id,max(bidAmount),userID from Bids group by id,bidAmo...

Show Detail

oracle sql select syntax with GROUP BY and HAVING clause

I been going thru some of the sql syntax to study for the oracle sql exam, I found something rather confusing based on the official references, the select syntax is as follow : SELECT [ hint...

Show Detail

SQL: GROUP BY Clause

SELECT (1.0*( SELECT SUM(r.SalesVolume) FROM RawData r INNER JOIN Product p ON r.ProductId = p.ProductId WHERE p.Distribut...

Show Detail

SQL Server CE GROUP BY clause

I have to use GROUP BY statement to pull data from SQL Server CE. Now I'm getting In aggregate and grouping expressions, the SELECT clause can contain only aggregates and grouping expressions...

Show Detail

Issue with Group by clause in linq to sql

I want to make this query in linq to sql . Please help. I am new to linq and having problem to with the group by clause . Here is the sql query select count(USERID), d.DEPTNAME from USERS u ...

Show Detail

unable to write having clause with group by clause in sql server

i want to show the daily fan running hours consumption with in particular dates. im able to get the records with out between the dates while writing the having clause to get the records between dates

Show Detail

Underlying implementation of Group By clause in Spark SQL

What is the underlying implementation of Group By clause in Spark SQL? I understand that Spark supports two types of Group by operations underneath i.e. GroupByKey and ReduceByKey. ReduceByKey is a...

Show Detail

Access LINQ to SQL results with a group clause

I have a LINQ to SQL query and I'm having trouble to access the results. Without the 'group' clause, it works fine, but with the group clause the resulting fields seems to be missing. var q = (fro...

Show Detail

SQL Server Management Studio GROUP BY clause SHORTCUT

Is there is a shortcut or tool or something in T-SQL in SQL Server Management Studio that will allow you to automatically create a GROUP BY clause?

Show Detail