markdown sql,count,distinct
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了markdown sql,count,distinct相关的知识,希望对你有一定的参考价值。
## Hana not support count(distinct xx) over (partition by) syntax
I have a table show as:
```
sqlite> select * from user;
pid uname email
---------- ---------- ----------
1 u1 E
1 u2 e
2 u21 e21
3 u31 e21
4 u41 e41
4 u42 E41
5 u52 E41
Run Time: real 0.000 user 0.000127 sys 0.000026
```
query this table and find out records that
1. lower email is duplicated per pid
2. two user belong to same person with same email is allowed
if you are using Oracle, then you can do some query as:
```sql
SELECT
*
FROM
(
SELECT
*,
COUNT(DISTINCT pid) OVER(
PARTITION BY lower(email)
) AS cnt
FROM
user
)
WHERE
cnt > 1;
```
but many other db is not support such kind of syntax, the 'count(distinct xxx)'.
so here is two solution for you:
```sql
--solution 01
SELECT
*
FROM
(
SELECT
*,
( DENSE_RANK() OVER(
PARTITION BY lower(email)
ORDER BY
pid
) + DENSE_RANK() OVER(
PARTITION BY lower(email)
ORDER BY
pid DESC
) - 1 ) AS cnt
FROM
user
)
WHERE
cnt > 1;
```
Refer to [StackOverflow](https://stackoverflow.com/questions/11202878/partition-function-count-over-possible-using-distinct)
```sql
--solution 02
SELECT
*,
MAX(cnt) OVER(
PARTITION BY lower(email)
) AS distinct_cnt
FROM
(
SELECT
*,
DENSE_RANK() OVER(
PARTITION BY lower(email)
ORDER BY
pid
) AS cnt
FROM
user
)
WHERE
distinct_cnt > 1;
```
Refer to [SQL Server Post](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/8868da5f-873f-4584-8831-4fcc7a620e74/count-distinct-over-partition-syntax-error?forum=transactsql)
以上是关于markdown sql,count,distinct的主要内容,如果未能解决你的问题,请参考以下文章