Catch Total value except date

Asked

Viewed 56 times

1

I’m trying to develop a query that will return me a total order value for a particular customer, with an informed date range. For example, between 05/11/2015 to 05/12/2015.

The problem is that I need to make the exception of a few days between the informed dates. For example, that this query takes the value between the dates reported above, except the days 14/11/2015 and the days 21/11/2015.

Those days could not have the value of your orders added in the total amount.

What I managed to do was this:

select sum(p.totalValor), c.nomeCliente 
from Pedido p join Cliente c 
on c.idCliente =  p.idCliente
where p.dataPedido = /*filtro das datas*/
and idCliente = 2

I couldn’t develop the dates part. Is there any way to do this?

The tables are below

client table

idClient Nomecliente

order table

idPedido idClient dated totalValor

1 answer

1


To find a date period use the operator BETWEEN and to exclude some dates use the operator NOT IN.

How you are using the function SUM you need to use the grouper GROUP BY at your request.

SELECT 
    SUM(P.TOTALVALOR),
    C.NOMECLIENTE
FROM 
    PEDIDO P 
JOIN 
    CLIENTE C ON C.IDCLIENTE =  P.IDCLIENTE
WHERE 
    DATAPEDIDO BETWEEN '05/11/2015' AND '05/12/2015'
AND
    DATAPEDIDO NOT IN ('14/11/2015', '21/11/2015')
AND 
    IDCLIENTE = 2
GROUP BY C.NOMECLIENTE

Browser other questions tagged

You are not signed in. Login or sign up in order to post.