通过使用计数(*)的组(分区)

发布于 2025-02-03 12:33:05 字数 1765 浏览 3 评论 0原文

我想创建一个基于其他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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

要走干脆点 2025-02-10 12:33:05

您对窗口功能的使用似乎放错了位置。问题中的预期输出具有完全未定义的列(losemeanbyco),因此,让我们从问题中删除它。未指定细节。

原始表不确定,所以让我们将问题输入(根本不是输入)进行,然后制作一个包含该数据的新表“测试”即可。

CREATE TABLE test ( cst  varchar(5), st  varchar(5),  co  varchar(5) );

INSERT INTO test VALUES
  ('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')
;

”简单的聚合:

SELECT cst
     , st
     , co
     , COUNT(*) AS count
  --mean of loss by co
  FROM test
 WHERE st != 'te'
 GROUP  BY cst,st ,co  -- , count
 ORDER BY cst , st, co
;

结果:

CSTSTCO计数
cuspactr1
cusre cus rectr3
salpactr2
salpaes1
salrectr2
sal 2 salreust1,

这似乎是可以用有限的细节来完成的。

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

CREATE TABLE test ( cst  varchar(5), st  varchar(5),  co  varchar(5) );

INSERT INTO test VALUES
  ('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')
;

The result column count is easily generated with simple aggregation:

SELECT cst
     , st
     , co
     , COUNT(*) AS count
  --mean of loss by co
  FROM test
 WHERE st != 'te'
 GROUP  BY cst,st ,co  -- , count
 ORDER BY cst , st, co
;

The result:

cststcocount
cuspactr1
cusrectr3
salpactr2
salpaest1
salrectr2
salreust1

This seems to be about all that can be done with the limited detail provided.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文