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的主要内容,如果未能解决你的问题,请参考以下文章

图解 SQL 执行顺序,通俗易懂!

Spark 上的 SQL:如何获取 DISTINCT 的所有值?

Java找工作必备知识——day04万字SQL复习

sql两张表(主表和字典表)关联查询,字典项翻译问题

如何在 GitHub README 中使用 Markdown 呈现多个列?

SQL语句中countcount(*)count(字段)用法的区别