Reputation: 274
I have the following query
select
coalesce(NULLIF(No_Parte,' '), NULLIF(OEM,' '), Num_serie) as Producto,
Id_Sucursal, Tipo_Movimiento, Cantidad, Costo,No_Servicio,
F_Entrada, F_Salida, Observaciones,
(case
when F_Entrada > F_Salida
then F_Entrada
else F_Salida
end) as Fecha
from
Kardex_Producto
where
Id_Sucursal = 'tehuacan'
and ((F_Entrada >= CONVERT(DateTime, '20161031', 103) OR F_Salida >= CONVERT(DateTime, '20161031', 103))
and (F_Entrada <= CONVERT(DateTime, '20161031', 103) OR F_Salida <= CONVERT(DateTime, '20161031', 103))) --and Tipo_Movimiento='S-'
order by
Tipo_Movimiento, No_Servicio
But for some reason it doesn't work as expected, for some reason, it is returning
+------------+-------------+-----------------+----------+---------+------------------+-------------------------+-------------------------+---------------------------------------------------+-------------------------+
| Producto | Id_Sucursal | Tipo_Movimiento | Cantidad | Costo | No_Servicio | F_Entrada | F_Salida | Observaciones | Fecha |
+------------+-------------+-----------------+----------+---------+------------------+-------------------------+-------------------------+---------------------------------------------------+-------------------------+
| 1TU3CWH | TEHUACAN | E+ | 1.00 | 0 | Traspaso No. 568 | 2016-11-11 19:19:43.447 | 1900-01-01 00:00:00.000 | | 2016-11-11 19:19:43.447 |
| 23651 | TEHUACAN | E+ | 1.00 | 0 | Traspaso No. 569 | 2016-11-14 12:29:21.663 | 1900-01-01 00:00:00.000 | | 2016-11-14 12:29:21.663 |
| 37035 | TEHUACAN | E+ | 2.00 | 0 | Traspaso No. 569 | 2016-11-14 12:29:20.657 | 1900-01-01 00:00:00.000 | | 2016-11-14 12:29:20.657 |
| 39657 | TEHUACAN | E+ | 2.00 | 0 | Traspaso No. 569 | 2016-11-14 12:29:19.403 | 1900-01-01 00:00:00.000 | | 2016-11-14 12:29:19.403 |
| 37069 | TEHUACAN | E+ | 6.00 | 0 | Traspaso No. 571 | 2016-11-17 15:29:30.147 | 1900-01-01 00:00:00.000 | | 2016-11-17 15:29:30.147 |
| 37282 | TEHUACAN | E+ | 1.00 | 0 | Traspaso No. 571 | 2016-11-17 15:29:29.503 | 1900-01-01 00:00:00.000 | | 2016-11-17 15:29:29.503 |
| 37069 | TEHUACAN | S- | 1.00 | 0 | 0000000002368 | 1900-01-01 00:00:00.000 | 2016-10-31 18:07:05.880 | Venta de Mostrador con numero de ticket indicado. | 2016-10-31 18:07:05.880 |
| 44259 | TEHUACAN | S- | 1.00 | 0 | 0000000002369 | 1900-01-01 00:00:00.000 | 2016-11-03 15:59:39.307 | Venta de Mostrador con numero de ticket indicado. | 2016-11-03 15:59:39.307 |
| 37069 | TEHUACAN | S- | 1.00 | 0 | 0000000002370 | 1900-01-01 00:00:00.000 | 2016-11-04 11:07:04.713 | Venta de Mostrador con numero de ticket indicado. | 2016-11-04 11:07:04.713 |
| 39510 | TEHUACAN | S- | 1.00 | 0 | 0000000002370 | 1900-01-01 00:00:00.000 | 2016-11-04 11:07:05.553 | Venta de Mostrador con numero de ticket indicado. | 2016-11-04 11:07:05.553 |
| 302H493011 | TEHUACAN | S- | 1.00 | 0 | 0000000002371 | 1900-01-01 00:00:00.000 | 2016-11-04 20:07:17.730 | Venta de Mostrador con numero de ticket indicado. | 2016-11-04 20:07:17.730 |
+------------+-------------+-----------------+----------+---------+------------------+-------------------------+-------------------------+---------------------------------------------------+-------------------------+
but it really should be returning 1 row
| 37069 | TEHUACAN | S- | 1.00 | 180.00 | 0000000002368 | 1900-01-01 00:00:00.000 | 2016-10-31 18:07:05.880 | Venta de Mostrador con numero de ticket indicado. | 2016-10-31 18:07:05.880 |
My guess it is because both columns (F_entrada and F_Salida), so I decided to "join" them on the column Fecha (the last one), and now I want to filter the data by the column "Fecha", but I don't know how to do it.
Thank you for your help.
Upvotes: 0
Views: 44
Reputation: 6670
You have 2 options here:
Add the (CASE WHEN... )
statement to the where
clause like this
WHERE Id_Sucursal='tehuacan'
AND (case when F_Entrada>F_Salida then F_Entrada else F_Salida end) <= CONVERT(DateTime, '20161031', 103)
Wrap your existing select statement as a sub-query that calculates the fecha then you can use fecha in the outer query. like this
SELECT * FROM
(
select coalesce(NULLIF(No_Parte,' '), NULLIF(OEM,' '),Num_serie) as
Producto, Id_Sucursal, Tipo_Movimiento, Cantidad,
Costo,No_Servicio, F_Entrada, F_Salida, Observaciones,
(case when F_Entrada>F_Salida then F_Entrada else F_Salida end) as Fecha
from Kardex_Producto
) as product
WHERE Id_Sucursal='tehuacan'
AND Fecha <= CONVERT(DateTime, '20161031', 103)
order by Tipo_Movimiento, No_Servicio
Upvotes: 1