简体   繁体   English

何时计算空值的 SQL 案例

[英]SQL case when to count null values

I'm writing a query, data looks like this:我正在写一个查询,数据如下所示:

Data:数据:

ID ID name姓名 date_completed date_completed version版本
1 1 Sydney悉尼 2021-01-01 2021-01-01 A一个
2 2 Melbourne墨尔本 2021-01-5 2021-01-5 A一个
3 3 Sydney悉尼 2021-01-10 2021-01-10 B
4 4 Sydney悉尼 2021-02-01 2021-02-01 A一个
5 5 Melbourne墨尔本 2021-02-07 2021-02-07 A一个
6 6 Melbourne墨尔本 2021-02-13 2021-02-13 A一个

My query:我的查询:

SELECT name
         , '01-01-21' AS Date
         , (case
                when version = 'A' then 'A'
                when version = 'B' then 'B'
        end)          AS 'Type'
         , (case
                when version = 'A' then count(ID)
                when version = 'B' then count(ID)
        end)          AS 'Count'
    FROM table
    WHERE date_completed between '2021-01-01' and '2021-01-31'
    GROUP BY name, version

    UNION

SELECT name
         , '01-02-21' AS Date
         , (case
                when version = 'A' then 'A'
                when version = 'B' then 'B'
        end)          AS 'Type'
         , (case
                when version = 'A' then count(ID)
                when version = 'B' then count(ID)
        end)          AS 'Count'
    FROM table
    WHERE date_completed between '2021-02-01' and '2021-02-28'
    GROUP BY name, version

Output:输出:

name姓名 date日期 Type类型 count数数
Sydney悉尼 01-01-21 01-01-21 A一个 1 1
Melbourne墨尔本 01-01-21 01-01-21 A一个 1 1
Sydney悉尼 01-01-21 01-01-21 B 1 1
Sydney悉尼 01-02-21 01-02-21 A一个 1 1
Melbourne墨尔本 01-02-21 01-02-21 A一个 2 2

The issue I'm facing is that because version A/B doesn't exist for some months it will not show a row with count = 0我面临的问题是,因为版本 A/B 几个月不存在,所以它不会显示 count = 0 的行

I'm trying to create an output like this:我正在尝试创建这样的输出:

name姓名 date日期 Type类型 count数数
Sydney悉尼 01-01-21 01-01-21 A一个 1 1
Melbourne墨尔本 01-01-21 01-01-21 A一个 1 1
Sydney悉尼 01-01-21 01-01-21 B 1 1
Melbourne墨尔本 01-01-21 01-01-21 B 0 0
Sydney悉尼 01-02-21 01-02-21 A一个 1 1
Melbourne墨尔本 01-02-21 01-02-21 A一个 2 2
Sydney悉尼 01-02-21 01-02-21 B 0 0
Melbourne墨尔本 01-02-21 01-02-21 B 0 0

Is this something that can be done without having to create a separate table with name, date & type and using LEFT JOIN between the two tables?这是否可以在不必创建具有名称、日期和类型的单独表并在两个表之间使用 LEFT JOIN 的情况下完成?

Also is there a better way of dealing with querying data within a specific month without having to union multiple select statements?还有没有更好的方法来处理特定月份内的查询数据而不必联合多个选择语句?

Thanks谢谢

You need to find all combination of name - month - version and then left join to your table and then perform the count()您需要找到name - month - version的所有组合,然后left join您的表,然后执行count()

If you have individual table of name, version, you may use that instead of finding the distinct value from the table ( select distinct name from tbl )如果您有单独的名称、版本表,您可以使用它而不是从表中查找不同的值( select distinct name from tbl

with 
name_mth_ver as
(
    select name, mth_st, mth_en, version
    from
    (
        select distinct name
        from   tbl
    ) n
    cross join
    (
         select distinct
                mth_st = convert(date, dateadd(month, datediff(month, 0, date_completed), 0)),
                mth_en = convert(date, dateadd(month, datediff(month, 0, date_completed) + 1, -1))
         from   tbl
    ) m
    cross join
    (
        select distinct version
        from   tbl
    ) v
)
select n.name
       , n.mth_st AS [Date]
       , n.version AS [Type]
       , count(t.ID) AS [Count]
from   name_mth_ver n
       left join tbl t on  n.name    = t.name
                       and n.mth_st <= t.date_completed
                       and n.mth_en >= t.date_completed
                       and n.version = t.version
group by n.name, n.mth_st, n.version

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

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