简体   繁体   English

选择sum where where子句SQL

[英]Select sum where clause SQL

I have the following code, it works but I am trying to separate SUM for each Banksphere.servicio_id column, this code SUM only one servicio_id... I'm a bit lost, can someone help me? 我有以下代码,它可以工作,但我试图为每个Banksphere.servicio_id列分隔SUM,这个代码SUM只有一个servicio_id ...我有点迷失,有人可以帮帮我吗?

As you can see, every WHERE clause is exactly the same but Banksphere.peticion_id which is the only one that changes... So maybe there's some better way just to filter once the common clauses and leave only peticion_id for OK and KO? 正如你所看到的,每个WHERE子句都是完全相同的但是Banksphere.peticion_id是唯一一个改变的...所以也许有一些更好的方法来过滤一下常用的子句并且只留下peticion_id用于OK和KO?

SELECT
(SELECT
    SUM(valor)
FROM
    Banksphere
WHERE
    Banksphere.fecha = '2013-01-14'
AND
    Banksphere.servicio_id = '6'
AND
    Banksphere.entidad_id = '2'
AND
    Banksphere.peticion_id = '0') AS OK,
(SELECT
    SUM(valor)
FROM
    Banksphere
WHERE
    Banksphere.fecha = '2013-01-14'
AND
    Banksphere.servicio_id = '6'
AND
    Banksphere.entidad_id = '2'
AND
    Banksphere.peticion_id = '1') AS KO

EDIT WITH WORKING CODE 使用工作代码编辑

SELECT  Servicios.nombre as servicio,
        SUM(case when peticion_id = '0' then valor end) as OK,
        SUM(case when peticion_id = '1' then valor end) as KO
FROM    Banksphere
INNER JOIN
    Servicios
ON
    Banksphere.servicio_id = Servicios.id
WHERE   Banksphere.fecha = '2013-01-14'
        AND Banksphere.entidad_id = '2'
        AND Banksphere.peticion_id in ('0', '1')
group by Servicios.nombre

I think you want something along these lines: 我想你想要这样的东西:

SELECT  banksphere.servicio_id, SUM(valor),
        SUM(case when peticion_id = '0' then valor end) as OK,
        SUM(case when peticion_id = '1' then valor end) as KO
FROM    Banksphere
WHERE   Banksphere.fecha = '2013-01-14'
        AND Banksphere.entidad_id = '2'
        AND Banksphere.peticion_id in ('0', '1', ...)
group by banksphere.servicio_id

This has a group by so you can get multiple "servicio_ids" and it adds separate columns for OK and KO. 这有一个group by所以你可以得到多个“servicio_ids”,它为OK和KO添加了单独的列。 If you want only servicio_id = 6, then add that back into the where clause. 如果只想要servicio_id = 6,那么将其添加回where子句。 And, you might want other variables in the group by as well, but you only mention service in the question. 并且,您可能也希望group by其他变量,但您只在问题中提及服务。

SELECT  servicio_id,
        entidad_id,
        SUM(CASE WHEN peticion_id = 0 THEN valor ELSE 0 END) OK,
        SUM(CASE WHEN peticion_id = 1 THEN valor ELSE 0 END) KO
FROM    BankSpehere
WHERE   fecha = '2013-01-14' AND 
        entidad_id = '2' AND 
        peticion_id in ('0', '1')
GROUP BY servicio_id, entidad_id
SELECT  SUM(valor)
FROM    Banksphere
WHERE   Banksphere.fecha = '2013-01-14'
        AND Banksphere.servicio_id = '6'
        AND Banksphere.entidad_id = '2'
        AND Banksphere.peticion_id in ('0', '1', ...)

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

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