JasperReports Print repeated values when doing INNER JOIN Double

0

This is my inquiry code

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,
     VENTAPRODUCTO."ID" AS VENTAPRODUCTO_ID,
     VENTAPRODUCTO."PRODUCTOID" AS VENTAPRODUCTO_PRODUCTOID,
     VENTAPRODUCTO."FACTURAID" AS VENTAPRODUCTO_FACTURAID,
     VENTAPRODUCTO."CANTIDAD" AS VENTAPRODUCTO_CANTIDAD,
     VENTAPRODUCTO."PRECIOVENTA" AS VENTAPRODUCTO_PRECIOVENTA,
     VENTASERVICIO."ID" AS VENTASERVICIO_ID,
     VENTASERVICIO."SERVICIOID" AS VENTASERVICIO_SERVICIOID,
     VENTASERVICIO."FACTURAID" AS VENTASERVICIO_FACTURAID,
     VENTASERVICIO."PRECIOVENTA" AS VENTASERVICIO_PRECIOVENTA,
     VENTASERVICIO."CANTIDAD" AS VENTASERVICIO_CANTIDAD
FROM
     "USUARIO"."FACTURA" FACTURA INNER JOIN "USUARIO"."VENTAPRODUCTO" VENTAPRODUCTO ON FACTURA."ID" = VENTAPRODUCTO."FACTURAID"
     INNER JOIN "USUARIO"."VENTASERVICIO" VENTASERVICIO ON FACTURA."ID" = VENTASERVICIO."FACTURAID"

This query returns duplicate values, then in the report I deselect the "Print repeated values" box in all fields, which works for SALES_PRODUCT but the values of the table SALES_SERVICE are displayed repeatedly.

    
asked by Duvan Andres Rivera Rodriguez 24.07.2017 в 21:59
source

1 answer

0

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.

    
answered by 25.07.2017 / 04:23
source