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

后端 未结 2 674
逝去的感伤
逝去的感伤 2020-12-30 04:01

I have a table myTable with 3 columns. col_1 is an INTEGER and the other 2 columns are DOUBLE. For example, col_1={

2条回答
  •  心在旅途
    2020-12-30 04:27

    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 
    

提交回复
热议问题