如何仅从MySQL查询中获得积极结果?

时间:2017-05-10 19:12:32

标签: mysql sql

我希望(`Pre-Scan Hours Planned`-`Pre-Scan Time Spent`)只是积极的。如果结果是否定的,我不想要它。我在SELECT子句中尝试了(`Pre-Scan Hours Planned`-`Pre-Scan Time Spent`)>0但它没有用。

 SELECT 

        `Account Executive`,
        `DC Clerk`,
        `Project Number`,
        Manufacturer,
        `Open Project Date`,
        `DUE DATE`,
        `Project Status`,

       ( `Pre-Scan Hours Planned`+
        `Radiated Emissions Hours Planned`+
        `Conducted Emissions Hours Planned`+
        `Harmonic and Flicker Hours Planned`+
        `ESD Hours Planned`+
        `RF Immunity Hours Planned`+
        `EFT Hours Planned`+
        `Surge Hours Planned`+
        `Conducted RF Hours Planned`+
        `Magnetic Field Hours Planned`+
        `Voltage Dips Hours Planned`+
        `EUT Photos and Data Sheet Hours Planned`+
        `Data Sheet Hours Planned` ) AS 'Total Planned Hours',

       ( `Pre-Scan Time Spent`+
        `Radiated Emissions Time Spent`+
        `Conducted Emissions Time Spent`+
        `Harmonic and Flicker Time Spent`+
        `ESD Time Spent`+
        `RF Immunity Time Spent`+
        `EFT Time Spent`+
        `Surge Time Spent`+
        `Conducted RF Time Spent`+
        `Magnetic Field Time Spent`+
        `Voltage Dips Time Spent`+
        `EUT Photos and Data Sheet Time Spent`+
        `Data Sheet Time Spent` ) AS 'Total Spent Hours',

        ( (`Pre-Scan Hours Planned`-`Pre-Scan Time Spent`)+
        (`Radiated Emissions Hours Planned`-`Radiated Emissions Time Spent`) +
        (`Conducted Emissions Hours Planned`-`Conducted Emissions Time Spent`) +
        (`Harmonic and Flicker Hours Planned`-`Harmonic and Flicker Time Spent`) +
        (`ESD Hours Planned`-  `ESD Time Spent`) +
        (`RF Immunity Hours Planned`-`RF Immunity Time Spent`) +
        (`EFT Hours Planned`-  `EFT Time Spent`) +
        (`Surge Hours Planned`-`Surge Time Spent`) +
        (`Conducted RF Hours Planned`-`Conducted RF Time Spent`) +
        (`Magnetic Field Hours Planned`-`Magnetic Field Time Spent`)+
        (`Voltage Dips Hours Planned`-`Voltage Dips Time Spent`) +
        (`EUT Photos and Data Sheet Hours Planned`-`EUT Photos and Data Sheet Time 
        Spent`) +
        (`Data Sheet Hours Planned` -`Data Sheet Time Spent`)) AS 'Remaining Hours',

        (`Pre-Scan Status`+
        `Radiated Emissions Status`+
        `Conducted Emissions Status`+
        `Harmonic and Flicker Status`+
        `ESD Status`+
        `RF Immunity Status`+
        `EFT Status`+
        `Surge Status`+
        `Conducted RF Status`+
        `Magnetic Field Status`+
        `Voltage Dips Status`+
        `EUT Photos Status`+
        `Data Sheet Status`) AS 'Items Remaining'

    FROM 
        EMCtestplan

让我更好地解释,例如我们有以下查询:

我需要这个:

select
a,
b,
c,
(f1-f2)+(f3-f4)+(f5-f6) as Sum
from table

我希望(f1-f2)+(f3-f4)+(f5-f6) as Sum此代码只计算正数公式。例如,如果(f3-f4)是negetive并且只是计算(f1-f2)+(f5-f6)

,则不计算总和

请有人帮帮我!

2 个答案:

答案 0 :(得分:0)

更改为OP提出的新问题的答案

OP有:

select
a,
b,
c,
(f1-f2)+(f3-f4)+(f5-f6) as Sum
from table
  

我希望(f1-f2)+(f3-f4)+(f5-f6)为Sum此代码只计算   积极的公式。例如,如果(f3-f4)是,则不计算总和   negetive和just calculte(f1-f2)+(f5-f6)

如果您只关心(f3-f4)否定:

select
a,
b,
c,
CASE 
    When (f3-f4) < 0 then (f1-f2)+(f5-f6)
    Else (f1-f2)+(f3-f4)+(f5-f6)
END as Sum
from table

如果担心(f1-f2),(f3-f4),(f5-f6)中的任何一个是负数,那么我会处理不同的

select
a,
b,
c,
(MAX(f1-f2,0) + MAX(f3-f4,0) + MAX(f5-f6,0) as Sum
from table

答案 1 :(得分:0)

负面时你想用什么?我假设你想要使用零。变化:

(`Pre-Scan Hours Planned`-`Pre-Scan Time Spent`)

为:

(CASE WHEN `Pre-Scan Hours Planned`-`Pre-Scan Time Spent` > 0
      THEN (`Pre-Scan Hours Planned`-`Pre-Scan Time Spent`)
      ELSE 0 END)

如果您愿意,可以使用零以外的其他内容。