具有THEN 1 THEN 0 ELSE 0的PostgreSQL SUM CASE表达式

时间:2019-01-20 01:53:28

标签: sql postgresql expression case

在寻找有关CASE表达问题的答案时,我找不到我想要的东西。目的是求和“活动”帐户的出现总数。但是,在查看结果后,似乎我不想在我的SUM值中包含一个活动帐户上的一组参数。这是原始的SUM案例:

 sum(
    CASE
        WHEN (type_txt = 'ACCOUNT'::text AND status = 'ACTIVE' THEN 1
        ELSE 0
    END) AS accounts_actv

但是,我重新编写了代码,以便在特殊情况下不应该计算有效帐户时进行计数:

 sum(
    CASE
        WHEN (type_txt = 'ACCOUNT' AND status = 'ACTIVE' THEN 1
        WHEN (type_txt = 'ACCOUNT' AND status = 'ACTIVE' AND code = 483 AND open_date < CURRENT_DATE) THEN 0
        ELSE 0
    END) AS accounts_actv

我认为,那么THEN 1 ELSE 0不能具有0的THEN条件----因此,我试图弄清楚如何在SUM中不包括这种类型的帐户

2 个答案:

答案 0 :(得分:1)

CASE返回第一个(从上到下)THEN表达式的值,该表达式具有一个WHEN表达式,其结果为true(如果没有匹配项,则返回ELSE )。由于您的第一个WHEN在第二个为真的情况下也为真,因此选择第一个而不是第二个。始终将WHEN中较窄的CASE放在较小的CASE WHEN (type_txt = 'ACCOUNT' AND status = 'ACTIVE' AND code = 483 AND open_date < CURRENT_DATE) THEN 0 WHEN (type_txt = 'ACCOUNT' AND status = 'ACTIVE' THEN 1 ELSE 0 END 中。

Server Error in '/' Application.
No http handler was found for request type 'GET'
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Web.HttpException: No http handler was found for request type 'GET'

Source Error: 

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

Stack Trace: 


[HttpException (0x80004005): No http handler was found for request type 'GET']
   System.Web.HttpApplication.MapIntegratedHttpHandler(HttpContext context, String requestType, VirtualPath path, String pathTranslated, Boolean useAppConfig, Boolean convertNativeStaticFileModule) +748
   System.Web.HttpServerUtility.Execute(String path, TextWriter writer, Boolean preserveForm) +589

[HttpException (0x80004005): Error executing child request for ~/ErrorPage.aspx.]
   System.Web.HttpServerUtility.Execute(String path, TextWriter writer, Boolean preserveForm) +1295
   System.Web.HttpServerUtility.Transfer(String path, Boolean preserveForm) +72
   System.Web.HttpServerUtility.Transfer(String path) +45
   DotNetNuke.Common.Initialize.CheckVersion(HttpApplication app) +845
   DotNetNuke.Common.Initialize.InitializeApp(HttpApplication app, Boolean& initialized) +154
   DotNetNuke.Common.Initialize.Init(HttpApplication app) +157
   DotNetNuke.HttpModules.RequestFilter.RequestFilterModule.FilterRequest(Object sender, EventArgs e) +258
   System.Web.SyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +139
   System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +195
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +88

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.7.3282.0

答案 1 :(得分:1)

在相同情况下放置其他条件:

error == 4