Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot use group by and over(partition by) in the same query?

I have a table myTable with 3 columns. col_1 is an INTEGER and the other 2 columns are DOUBLE. For example, col_1={1, 2}, col_2={0.1, 0.2, 0.3}. Each element in col_1 is composed of all the values of col_2 and col_2 has repeated values for each element in col_1. The 3rd column can have any value as shown below:

    col_1 | col_2 | Value
    ----------------------
    1     |  0.1  |  1.0
    1     |  0.2  |  2.0
    1     |  0.2  |  3.0
    1     |  0.3  |  4.0
    1     |  0.3  |  5.0
    2     |  0.1  |  6.0
    2     |  0.1  |  7.0
    2     |  0.1  |  8.0
    2     |  0.2  |  9.0
    2     |  0.3  |  10.0

What I want is to use an aggregate-function SUM() on the Value column partition by col_1 and grouped by col_2. The Above table should look like this:

    col_1 | col_2 | sum_value
    ----------------------
    1     |  0.1  |  1.0
    1     |  0.2  |  5.0
    1     |  0.3  |  9.0
    2     |  0.1  |  21.0
    2     |  0.2  |  9.0
    2     |  0.3  |  10.0

I tried the following SQL query:

SELECT col_1, col_2, sum(Value) over(partition by col_1) as sum_value
from myTable
GROUP BY col_1, col_2

But on DB2 v10.5 it gave the following error:

SQL0119N  An expression starting with "Value" specified in a SELECT 
clause, HAVING clause, or ORDER BY clause is not specified in the 
GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER 
BY clause with a column function and no GROUP BY clause is specified.

Can you kindly point out what is wrong. I do not have much experience with SQL.

Thank you.

like image 859
user3557405 Avatar asked Mar 25 '15 08:03

user3557405


People also ask

Can we use GROUP BY and partition by in same query?

Therefore, in conclusion, the PARTITION BY retrieves all the records in the table, while the GROUP BY only returns a limited number. One more thing is that GROUP BY does not allow to add columns which are not parts of GROUP BY clause in select statement. However, with PARTITION BY clause, we can add required columns.

Can we use GROUP BY in over clause?

Did you know that you can use the SQL Server aggregate functions SUM, COUNT, MAX, MIN and AVG with an OVER Clause now? Using an OVER clause you can produce individual record values along with aggregate values to different levels, without using a GROUP BY clause.

Can we use two GROUP BY in same query?

Yes, it is possible to use MySQL GROUP BY clause with multiple columns just as we can use MySQL DISTINCT clause. Consider the following example in which we have used DISTINCT clause in first query and GROUP BY clause in the second query, on 'fname' and 'Lname' columns of the table named 'testing'.

Can we use both GROUP BY and order by together in SQL?

Both GROUP BY and ORDER BY are clauses (or statements) that serve similar functions; that is to sort query results. However, each of these serve very different purposes; so different in fact, that they can be employed separately or together.


1 Answers

Yes, you can, but you should be consistent regarding the grouping levels. That is, if your query is a GROUP BY query, then in an analytic function you can only use "detail" columns from the "non-analytic" part of your selected columns. Thus, you can use either the GROUP BY columns or the non-analytic aggregates, like this example:

select product_id, company, 
sum(members) as No_of_Members, 
sum(sum(members)) over(partition by company) as TotalMembership 
From Product_Membership 
Group by Product_ID, Company

Hope that helps

SELECT col_1, col_2, sum(Value) over(partition by col_1) as sum_value
    -- also try changing "col_1" to "col_2" in OVER
from myTable
GROUP BY col_2,col_1 
like image 200
Dave Avatar answered Oct 24 '22 11:10

Dave