English 中文(简体)
使用 MySQL 中案例研究语句中的和
原标题:Using Sum inside a Case statement in MySQL

我对 MySQL 添加三个值有问题, 这应该很简单吧?

我有代码,根据第二列的值从一列中选择数值,我使用这样一个案例的语句:

Select
        Max(Case
            When Table1.costcode Like  %Costcode1%  
            Then Table1.costs 
            Else Null End) As  Costcode1 ,
        Max(Case
            When Table1.costcode Like  %Costcode2%  
            Then Table1.costs
            Else Null End) As  Costcode2 ,
        Max(Case
            When Table1.costcode Like  %Costcode3%  
            Then Table1.costs 
            Else Null End) As  Costcode3 ,
        (Case
            When Table1.costcode In ( %Costcode1% , %Costcode2% , %Costcode3% )
            Then Sum(Table1.costs)
            Else Null End) As  Total Cost ,

From Table1

前三个案件报表运作良好,所有回报值(在数据库中保存为负数,例如-13624.00),但总成本案例只返回Null...

表1. 成本代码栏中包括许多其他代码,所以我无法在不先选取这些代码的情况下,对所有数值进行总和。

但显然我遗漏了一些东西...

谢谢 谢谢

问题回答

试试这个

    SUM(Case Table1.costcode
        When LIKE ( %Costcode1% ) Then Table1.costs
        When LIKE ( %Costcode2% ) Then Table1.costs
        When LIKE ( %Costcode3% ) Then Table1.costs
        Then Table1.costs
        Else 0.00 End) As  Total Cost 




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签