通过使用计数(*)的组(分区)
我想创建一个基于其他3列的重复元素计数的新列。当我尝试添加组时,i_count会得到以下错误SQL状态:42p20。
SELECT
cst,
st,
co,
COUNT(*) OVER (PARTITION BY cst,st,co ) AS count
--mean of loss by co
FROM s.tb_st , s.tb_cst , s.tb_co , erp.tb_i
WHERE st != 'te'
GROUP BY cst,st ,co, count
ORDER BY cst , st, co;
输入
cst | st | co
sal | pa | ctr
sal | pa | ctr
sal | pa | est
sal | re | ctr
sal | re | ctr
sal | re | ust
cus | pa | ctr
cus | re | ctr
cus | re | ctr
cus | re | ctr
我错误的输出
cst | st | co |count
sal | pa | ctr | 55
sal | pa | est | 55
sal | re | ctr | 55
sal | re | ust | 55
cus | pa | ctr | 55
cus | re | ctr | 55
预期输出
cst | st | co |count
sal | pa | ctr | 2
sal | pa | est | 1
sal | re | ctr | 2
sal | re | ust | 1
cus | pa | ctr | 1
cus | re | ctr | 3
I want to create a new column with the counts of repeated elements based on other 3 columns. When I try on the group by to add, i_count I get the following error SQL state: 42P20.
SELECT
cst,
st,
co,
COUNT(*) OVER (PARTITION BY cst,st,co ) AS count
--mean of loss by co
FROM s.tb_st , s.tb_cst , s.tb_co , erp.tb_i
WHERE st != 'te'
GROUP BY cst,st ,co, count
ORDER BY cst , st, co;
input
cst | st | co
sal | pa | ctr
sal | pa | ctr
sal | pa | est
sal | re | ctr
sal | re | ctr
sal | re | ust
cus | pa | ctr
cus | re | ctr
cus | re | ctr
cus | re | ctr
my wrong output
cst | st | co |count
sal | pa | ctr | 55
sal | pa | est | 55
sal | re | ctr | 55
sal | re | ust | 55
cus | pa | ctr | 55
cus | re | ctr | 55
expected output
cst | st | co |count
sal | pa | ctr | 2
sal | pa | est | 1
sal | re | ctr | 2
sal | re | ust | 1
cus | pa | ctr | 1
cus | re | ctr | 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您对窗口功能的使用似乎放错了位置。问题中的预期输出具有完全未定义的列(
losemeanbyco
),因此,让我们从问题中删除它。未指定细节。原始表不确定,所以让我们将问题输入(根本不是输入)进行,然后制作一个包含该数据的新表“测试”即可。
”简单的聚合:
结果:
这似乎是可以用有限的细节来完成的。
Your use of a window function appears to be misplaced. The expected output in the question has a column (
lossmeanbyco
) which is completely undefined, so let's just remove that from the question. No detail was specified.The original tables are undefined, so let's just take your question input (which isn't input at all) and make a new table called "test" containing that data.
Test case
The result column
count
is easily generated with simple aggregation:The result:
This seems to be about all that can be done with the limited detail provided.