I have the following table,
-- Generated with pg_dump, some constraints are missing
CREATE TABLE articulos_factura_venta (
fila integer NOT NULL,
cantidad integer NOT NULL,
color integer NOT NULL,
talla integer NOT NULL,
estado integer DEFAULT 2 NOT NULL,
origen integer,
factura integer NOT NULL,
articulo integer NOT NULL,
precio integer NOT NULL,
vendedor integer,
anulado boolean DEFAULT false,
iva double precision DEFAULT 12.0,
fecha date DEFAULT ('now'::text)::date NOT NULL
);
and it contains the following rows1
fila | cantidad | color | talla | estado | origen | factura | articulo | precio | vendedor | anulado | iva | fecha
------+----------+-------+-------+--------+--------+---------+----------+--------+----------+---------+-----+------------
0 | 1 | 0 | 3 | 6 | 18 | 28239 | 1325 | 455 | 6 | f | 0 | 2015-04-22
1 | 1 | 0 | 2 | 6 | 93 | 28239 | 2071 | 615 | 6 | f | 0 | 2015-04-22
2 | 1 | 0 | 49 | 6 | 76 | 28239 | 2013 | 545 | 6 | f | 0 | 2015-04-22
3 | 1 | 0 | 78 | 6 | 85 | 28239 | 2042 | 235 | 6 | f | 0 | 2015-04-22
4 | 1 | 0 | 49 | 6 | 81 | 28239 | 2026 | 615 | 6 | f | 0 | 2015-04-22
5 | 1 | 0 | 50 | 6 | 90 | 28239 | 2051 | 755 | 6 | f | 0 | 2015-04-22
6 | 1 | 0 | 1 | 38 | 21 | 28239 | 1780 | 495 | 6 | f | 0 | 2015-04-22
7 | 1 | 15 | 2 | 38 | 16 | 28239 | 1323 | 845 | 6 | f | 0 | 2015-04-22
8 | 1 | 0 | 4 | 38 | 18 | 28239 | 1326 | 455 | 6 | f | 0 | 2015-04-22
2 | 1 | 0 | 49 | 22 | 76 | 28239 | 2013 | 545 | 6 | f | 0 | 2015-04-22
The question is very straight forward, why this query outputs no rows?
SELECT
filas.factura,
filas.fila,
filas.cantidad,
retirados.cantidad,
vendidos.cantidad,
filas.estado
FROM
articulos_factura_venta AS filas
LEFT JOIN
articulos_factura_venta AS retirados
USING (fila, color, talla, origen, factura, articulo, vendedor)
LEFT JOIN
articulos_factura_venta AS vendidos
USING (fila, color, talla, origen, factura, articulo, vendedor)
JOIN
articulos
ON articulos.codigo = filas.articulo
JOIN
tallas
ON tallas.codigo = filas.talla
JOIN
colores
ON colores.codigo = filas.color
JOIN
empleados
ON empleados.codigo = filas.vendedor
WHERE
filas.factura = 28239 AND
retirados.estado & 16 <> 0 AND
vendidos.estado & 8 <> 0 AND
filas.estado & 4 <> 0
ORDER BY
filas.estado
I expect this query to subtract cantidad
from the row that has fila == 2
the case where estado & 16 <> 0
and hence I expect only one row with fila == 2
and cantidad = 0
NOTE: The bit flags, are not hardcoded, they are an enum
that I use in the actual application written with c++.
Table definition
database# \d articulos_factura_venta
Column | Type | Modifiers
----------+------------------+--------------------------------------
fila | integer | not null
cantidad | integer | not null
color | integer | not null
talla | integer | not null
estado | integer | not null default 2
origen | integer |
factura | integer | not null
articulo | integer | not null
precio | integer | not null
vendedor | integer |
anulado | boolean | default false
iva | double precision | default 12.0
fecha | date | not null default ('now'::text)::date
Indexes:
"articulos_factura_venta_pkey" PRIMARY KEY, btree (fila, factura, articulo, precio, talla, color, estado)
"buscar_cantidad_venta_idx" btree (articulo, talla, color, origen)
Foreign-key constraints:
"cantidades_venta_articulo_fkey" FOREIGN KEY (articulo) REFERENCES articulos(codigo)
"cantidades_venta_color_fkey" FOREIGN KEY (color) REFERENCES colores(codigo) ON UPDATE CASCADE ON DELETE RESTRICT
"cantidades_venta_factura_fkey" FOREIGN KEY (factura) REFERENCES ventas(codigo)
"cantidades_venta_origen_fkey" FOREIGN KEY (origen) REFERENCES compras(codigo) ON UPDATE CASCADE ON DELETE RESTRICT
"cantidades_venta_talla_fkey" FOREIGN KEY (talla) REFERENCES tallas(codigo) ON UPDATE CASCADE ON DELETE RESTRICT
"cantidades_venta_vendedor_fkey" FOREIGN KEY (vendedor) REFERENCES empleados(codigo)
[1]The table contains thousands of rows, but I am interested in these rows only, i.e. rows for which factura == 28239
.