Let's see, what I can tell you by looking at the model that only consists of three tables, is that you have a relationship between Invoices < - > Services (1-N) and between Invoices < - > Products (1-N), but the ternary relationship between Services < - > Products < - > Invoice (N-M) requires another table. As I said in my comment, I think you are missing an entity that defines how products and services are related to the invoice, something like FacturasItems
or FacturasLineas
(only by way of example). I'll answer you anyway, because you can be useful.
I imagine that in reality you can bill services and / or products and eventually only services or only products. For this, as you raise the fault is in the JOIN
you do, in fact you should NOT make a join
between both tables.
Suppose that in the invoice 1 you have incorporated the products P1 and P2 and the service S3, the output as you have planned it using two INNER JOIN
would be:
Factura Producto Servicio
======= ======== ========
1 P1 S3
1 P2 S3
This can give you the misconception that the S3 service is "duplicated". A worse case would be if you have not billed for example a service and yes a product, in that case the query would not return records.
The desirable way to see the data should be:
Factura Tipo Producto/Servicio
======= ========= =================
1 Producto P1
1 Producto P2
1 Servicio S3
How do we put it together?
If we imagine that conceptually an invoice has "Items" invoiced and that the "Items" can be Products and / or Services then you should have a query that "unite" the products and services and then perform the JOIN
. Since you do not have this entity or table in your model, we are going to simulate this by using UNION
.
I do not know in depth JasperReports
but you should be able to put together a query SQL
using a UNION
between Products and Services, something like this:
SELECT FACTURA."ID" AS FACTURA_ID,
FACTURA."CLIENTEID" AS FACTURA_CLIENTEID,
FACTURA."USUARIOID" AS FACTURA_USUARIOID,
FACTURA."FECHA" AS FACTURA_FECHA,
FACTURA."TOTAL" AS FACTURA_TOTAL,
-- detalle de los ítems facturados
PRODUCTOSERVCICIO.TIPO,
PRODUCTOSERVCICIO.ID,
PRODUCTOSERVCICIO.VENTAPRODUCTO_PRODUCTOSERVICIO_ID,
PRODUCTOSERVCICIO.CANTIDAD,
PRODUCTOSERVCICIO.PRECIOVENTA
FROM "USUARIO"."FACTURA" FACTURA
INNER JOIN ( SELECT 'PRODUCTO' AS TIPO,
VENTAPRODUCTO."FACTURAID" AS FACTURAID,
VENTAPRODUCTO."ID" AS ID,
VENTAPRODUCTO."PRODUCTOID" AS VENTAPRODUCTO_PRODUCTOSERVICIO_ID,
VENTAPRODUCTO."CANTIDAD" AS CANTIDAD,
VENTAPRODUCTO."PRECIOVENTA" AS PRECIOVENTA
FROM "USUARIO"."VENTAPRODUCTO" VENTAPRODUCTO
UNION
SELECT 'SERVICIO' AS TIPO,
VENTASERVICIO."FACTURAID" AS FACTURAID,
VENTASERVICIO."ID" AS ID,
VENTASERVICIO."SERVICIOID" AS VENTAPRODUCTO_PRODUCTOSERVICIO_ID,
VENTASERVICIO."CANTIDAD" AS CANTIDAD,
VENTASERVICIO."PRECIOVENTA" AS PRECIOVENTA
FROM "USUARIO"."VENTASERVICIO" VENTASERVICIO
) PRODUCTOSERVCICIO
ON PRODUCTOSERVCICIO.FACTURAID = FACTURA."ID"
Obviously without the complete model and sample data, I can not assure you that as it is written works, but I hope it serves to understand how to make the query.