简体   繁体   English

通过SQL对分区进行计数

[英]Count Distinct over partition by sql

I have a table like 我有一张桌子

col1ID  col2String Col3ID Col4String Col5Data
  1        xxx       20      abc     14-09-2018
  1        xxx       20      xyz     14-09-2018
  2        xxx       30      abc     14-09-2018
  2        xxx       30      abc     14-09-2018 

I would like to add column which count how many different strings I have in col4String group by col1ID and col3ID. 我想添加一列,该列通过col1ID和col3ID计算col4String组中有多少个不同的字符串。

So something like 所以像

COUNT(DISTINCT (Col4String)) over (partition by col1ID, col3ID)

but it doesn't work, I receive an error 但它不起作用,我收到一个错误

Use of DISTINCT is not allowed with the OVER clause. OVER子句不允许使用DISTINCT。
Msg 102, Level 15, State 1, Line 23. 消息102,第15级,州1,第23行。

I have more columns like col2String, col5Data but they shouldn´t be affected, so I can't use distinct at the beginning of SELECT , and dense_rank() also doen´t seems to work in my case. 我有更多列,例如col2String和col5Data,但它们不应该受到影响,因此我不能在SELECT的开头使用distinct,而且我的情况下dense_rank()似乎也不起作用。

Thank You for help. 谢谢你的帮助。

Try this: 尝试这个:

DECLARE @DataSource TABLE
(
    [col1ID] INT
   ,[col2String] VARCHAR(12) 
   ,[Col3ID]  INT
   ,[Col4String]  VARCHAR(12)
   ,[Col5Data] DATE
);

INSERT INTO @DataSource
VALUES (1, 'xxx', 20, 'abc', '2018-09-14')
      ,(1, 'xxx', 20, 'xyz', '2018-09-14')
      ,(2, 'xxx', 30, 'abc', '2018-09-14')
      ,(2, 'xxx', 30, 'abc', '2018-09-14');

SELECT *
     ,dense_rank() over (partition by col1ID, col3ID order by [Col4String])  + dense_rank() over (partition by col1ID, col3ID order by [Col4String] desc) - 1
FROM @DataSource

在此处输入图片说明

Obviously distinct is not supported in window function in SQL Server, therefore, you may use a subquery instead. 显然,SQL Server的窗口功能不支持distinct,因此,您可以改用子查询。 Something along these lines: 遵循以下原则:

 select (
           select COUNT(DISTINCT Col4String) 
           from your_table t2
           where t1.col1ID = t2.col1ID and t1.col3ID = t2.col3ID
        )
 from your_table t1

Try this way; 尝试这种方式;

select * from TableX X
outer apply(select count(*) as stringCount , X2.Col4String 
            from TableX X2 on X.col1ID= X2.col1ID and X.col3ID = X2.col3ID
            group by X2.Col4String ) K

I would use APPLY : 我会使用APPLY

SELECT t.*, t1.Col4String_Cnt
FROM table t CROSS APPLY
     (SELECT COUNT(DISTINCT t1.Col4String) AS Col4String_Cnt
      FROM table t1
      WHERE t1.col1ID = t.col1ID AND t1.col3ID  = t.col3ID 
     ) t1;

You can do this with an additional level of window functions. 您可以使用附加级别的窗口功能来执行此操作。 One method uses dense_rank() : 一种方法使用dense_rank()

SELECT . . .,
       MAX(DR) OVER (PARTITION BY col1ID, col3ID)
FROM (SELECT t.*, 
             DENSE_RANK() OVER (PARTITION BY col1ID, col3ID ORDER BY Col4String) as dr
      FROM t
     ) t 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM