我有一个选择查询,如下所示:
sel aa.*
, min(Case when BAL >0 then 1
when bb.CUST_DT < aa.APPL_DT then 2
else 3
end)
over (partition by app_id) as cus_Typ
from
tablea AS aa
left join tableb as bb
on aa.cust_num=bb.cust_num
group by 1,2,3
运行时我看到以下错误。
- 如果删除Group-by子句中的"cus_Typ"(Group-by-col 3(,则会出现以下错误
3504选定的非聚合值必须是关联组的一部分
- 如果我添加cus_Typ(因为它在分区/排名子句中(,我得到以下错误:-
错误5481-按子句分组中不允许使用有序分析函数
代码运行良好的场景:-
如果没有分区,groupby工作得很好。对于分区,它会抛出一个错误。。
没有分区分组也可以。
我的问题是,我们如何通过同时包含groupby和partition来使这个查询工作?哪些列需要按包含在组中?
谢谢。
的一些工作选项
select
column_1
, count(*) over(partition by column_1 order by column_1) -- will give you "1" in this specific case
, count(*) -- number of occurences of each column_1 values
, count(*) over() -- will give you number of unique column_1 values
, sum(count(*)) over () -- will give a total number of rows in the dataset
from
table_1
group by
column_1
如果您试图在不同的列中运行分区和分组,那么将分区和分组加倍似乎是行不通的。