Sql 我是否正确使用CASE?

Sql 我是否正确使用CASE?,sql,case,case-when,Sql,Case,Case When,编辑: 对不起,我一开始没有澄清。CompanyType为smallint,不为null,TotalLicenses为int,null 基本上,如果C.CompanyType!=4 我看了一些参考指南和教程,但我找不到一个能证实我在本例中使用CASE的方法 如果到期日期大于或等于今天的日期,DemoLicense等于0,如果公司类型不是4,TotalLicenses大于0,我尝试将“购买的”列更新为1。如果是4,则可以是0 update WebCatalog.Published.DemoTrac

编辑:

对不起,我一开始没有澄清。CompanyType为smallint,不为null,TotalLicenses为int,null

基本上,如果C.CompanyType!=4

我看了一些参考指南和教程,但我找不到一个能证实我在本例中使用CASE的方法

如果到期日期大于或等于今天的日期,DemoLicense等于0,如果公司类型不是4,TotalLicenses大于0,我尝试将“购买的”列更新为1。如果是4,则可以是0

update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
    inner join WebCatalog.Published.RCompany RC
        on C.RCompany = RC.Link
    inner join WebCatalog.Published.DemoTracking DT
        on C.PKey = DT.CompanyPKey and DT.Purchased = 0
where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
and C.TotalLicenses > 
    Case 
        when C.CompanyType != 4 THEN 0
    END
我想你想要:

and ( C.CompanyType <> 4 AND C.TotalLicenses > 0 
   OR C.CompanyType = 4 AND C.TotalLicenses >= 0
    )
如果
Company.totalices
不能有负值,可以进一步简化为:

and ( C.TotalLicenses > 0 
   OR C.CompanyType = 4
    )
我想你想要:

and ( C.CompanyType <> 4 AND C.TotalLicenses > 0 
   OR C.CompanyType = 4 AND C.TotalLicenses >= 0
    )
如果
Company.totalices
不能有负值,可以进一步简化为:

and ( C.TotalLicenses > 0 
   OR C.CompanyType = 4
    )
我想你想要:

and ( C.CompanyType <> 4 AND C.TotalLicenses > 0 
   OR C.CompanyType = 4 AND C.TotalLicenses >= 0
    )
如果
Company.totalices
不能有负值,可以进一步简化为:

and ( C.TotalLicenses > 0 
   OR C.CompanyType = 4
    )
我想你想要:

and ( C.CompanyType <> 4 AND C.TotalLicenses > 0 
   OR C.CompanyType = 4 AND C.TotalLicenses >= 0
    )
如果
Company.totalices
不能有负值,可以进一步简化为:

and ( C.TotalLicenses > 0 
   OR C.CompanyType = 4
    )

首先,我认为不同DBMS之间的案例结构不同。因此,以下内容可能不适用于您

第二,当您未指定Else时,案例返回NULL。与NULL的比较也为NULL,因此不会返回该行。这意味着您需要一个默认案例

所以你应该写:

and C.TotalLicenses > 
    Case 
        when C.CompanyType != 4 THEN 0
        else -1
    END

首先,我认为不同DBMS之间的案例结构不同。因此,以下内容可能不适用于您

第二,当您未指定Else时,案例返回NULL。与NULL的比较也为NULL,因此不会返回该行。这意味着您需要一个默认案例

所以你应该写:

and C.TotalLicenses > 
    Case 
        when C.CompanyType != 4 THEN 0
        else -1
    END

首先,我认为不同DBMS之间的案例结构不同。因此,以下内容可能不适用于您

第二,当您未指定Else时,案例返回NULL。与NULL的比较也为NULL,因此不会返回该行。这意味着您需要一个默认案例

所以你应该写:

and C.TotalLicenses > 
    Case 
        when C.CompanyType != 4 THEN 0
        else -1
    END

首先,我认为不同DBMS之间的案例结构不同。因此,以下内容可能不适用于您

第二,当您未指定Else时,案例返回NULL。与NULL的比较也为NULL,因此不会返回该行。这意味着您需要一个默认案例

所以你应该写:

and C.TotalLicenses > 
    Case 
        when C.CompanyType != 4 THEN 0
        else -1
    END

这与您描述的方式没有太大区别:

update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
        inner join WebCatalog.Published.RCompany RC
            on C.RCompany = RC.Link
        inner join WebCatalog.Published.DemoTracking DT
            on C.PKey = DT.CompanyPKey and DT.Purchased = 0
 where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
 and (C.TotalLicenses > 0 or C.CompanyType = 4)

这与您描述的方式没有太大区别:

update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
        inner join WebCatalog.Published.RCompany RC
            on C.RCompany = RC.Link
        inner join WebCatalog.Published.DemoTracking DT
            on C.PKey = DT.CompanyPKey and DT.Purchased = 0
 where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
 and (C.TotalLicenses > 0 or C.CompanyType = 4)

这与您描述的方式没有太大区别:

update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
        inner join WebCatalog.Published.RCompany RC
            on C.RCompany = RC.Link
        inner join WebCatalog.Published.DemoTracking DT
            on C.PKey = DT.CompanyPKey and DT.Purchased = 0
 where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
 and (C.TotalLicenses > 0 or C.CompanyType = 4)

这与您描述的方式没有太大区别:

update WebCatalog.Published.DemoTracking
set Purchased = 1
from WebCatalog.Published.Company C
        inner join WebCatalog.Published.RCompany RC
            on C.RCompany = RC.Link
        inner join WebCatalog.Published.DemoTracking DT
            on C.PKey = DT.CompanyPKey and DT.Purchased = 0
 where RC.Expiration >= Cast(GETDATE() as Date) and RC.DemoLicense = 0
 and (C.TotalLicenses > 0 or C.CompanyType = 4)


因此,如果
C.CompanyType
等于
4
,则应进行更新?无论C.CompanyType是什么类型,都应进行更新,但如果公司类型不是4,则仅当总计大于0时才应进行更新,如果
C.CompanyType
等于
4
,应该进行更新?无论C.CompanyType是什么,都应该进行更新,但如果公司类型不是4,则只有当TotalLicenses大于0时才应该进行更新。因此,如果
C.CompanyType
等于
4
,则应该进行更新。无论C.CompanyType是什么,都应该进行更新,但如果公司类型不是4,则仅当TotalLicenses大于0时才应进行更新。因此,如果
C.CompanyType
等于
4
,则应进行更新?无论C.CompanyType是什么类型,都应进行更新,但如果公司类型不是4,只有当TotalLicenses大于0时才应进行更新。不需要
C.TotalLicenses=0
@Bulat OP说“如果公司类型不是4,TotalLicenses大于0。如果是4,则可以为0”。您真的投了反对票,因为查询可以稍微简化吗(并且只有当你认为,
TotalLicenses
不能有负值的假设是正确的);[x可以是0]和[x等于0]对我来说不是相等的陈述。在这里,我想我们可以忽略“它可以是0部分”.我投了反对票,因为它是不正确的,而不是因为它可以简化。@b你假设了太多的东西。它在什么方面是不正确的?如果
TotalLicenses
只有正值或零值,你的查询和我的查询是等价的…没有必要
C.TotalLicenses=0
@b在OP上说“如果公司类型不是4,则TotalLicenses大于0。如果公司类型是4,则可以为0”您真的投了反对票吗?因为查询可以稍微简化(并且只有在假设
TotalLicenses
不能为负值的情况下才正确)?[x可以为0]和[x等于0]在这种情况下,我想我们可以忽略“它可以是0”部分“.我投了反对票,是因为它不正确,而不是因为它可以简化。@Bulat你假设了太多的东西。它以什么方式不正确?如果
TotalLicenses
只有正值或零值,你的查询和我的查询是等价的……没有必要
C.TotalLicenses=0
@Bulat OP说“如果公司类型不是4,则TotalLicenses大于0。如果公司类型是4,则可以为0”您真的投了反对票吗?因为查询可以稍微简化(并且只有在假设
TotalLicenses
不能为负值的情况下才正确)?[x可以为0]和[x等于0]在这种情况下,我想我们可以忽略“它可以是0”部分“.我投了反对票,因为它不正确,而不是因为它可以简化。@B你假设和假设了太多的东西。它以什么方式不正确?如果
TotalLicenses
只有正值或零值,你的查询和我的查询是等价的…没有必要