比较同一表的数据(SELF-JOIN)

时间:2015-01-28 10:56:46

标签: sql self-join

此查询的想法是比较项目的两个周期的销售额。我做了一个很好的查询,但现在我想通过客户端分组,我试图将它添加到查询但FlameRobin崩溃。

QUERY

SELECT COD, DESCRIPCION, IIF(CANTIDAD_1 IS NULL, 0, CANTIDAD_1) AS CANTIDAD1, IIF(OBSEQUIO_1 IS NULL, 0, OBSEQUIO_1) AS OBSEQUIO1,     
IIF(ABONO_1 IS NULL, 0, ABONO_1) AS ABONO1, IIF(TOTAL_1 IS NULL, 0, TOTAL_1) AS TOTAL1,    
IIF(CANTIDAD_2 IS NULL, 0, CANTIDAD_2) AS CANTIDAD2, IIF(OBSEQUIO_2 IS NULL, 0, OBSEQUIO_2) AS OBSEQUIO2,     
IIF(ABONO_2 IS NULL, 0, ABONO_2) AS ABONO2, IIF(TOTAL_2 IS NULL, 0, TOTAL_2) AS TOTAL2,     
((IIF(TOTAL_2 IS NULL, 0, TOTAL_2) - IIF(TOTAL_1 IS NULL, 0, TOTAL_1))/ IIF(TOTAL_1 IS NULL OR TOTAL_1 = 0, 1, TOTAL_1)) AS COMP     
FROM (     
    SELECT SUM(CANT1) AS CANTIDAD_1,     
    SUM(CANT_OBSEQUIO) AS OBSEQUIO_1,    
    SUM(CANT_ABONO) AS ABONO_1,     
    (SUM(CANT1) + SUM(CANT_OBSEQUIO) - SUM(CANT_ABONO)) AS TOTAL_1,     
    CODIGO as COD, DESCR AS DESCRIPCION
    FROM (     
        SELECT DISTINCT D.IDALBARAN AS IDALB, D.CODARTICULO AS CODIGO, D.DESCRIPCION_PRODUCTO AS DESCR,
        IIF(D.IDCONCEPTO IS NULL OR D.IDCONCEPTO = '', D.CANTIDAD, 0) AS CANT1,     
        IIF(D.IDCONCEPTO = 'O' OR D.IDCONCEPTO = 'P', D.CANTIDAD, 0) AS CANT_OBSEQUIO,     
        IIF(D.IDCONCEPTO = 'A', D.CANTIDAD, 0) AS CANT_ABONO     
        FROM ALBARANESVENTA_DETALLE D     
        LEFT JOIN ALBARANESVENTA_CABECERA C ON C.IDALBARAN = D.IDALBARAN     
        LEFT JOIN ARTICULOS ar ON D.CODARTICULO = ar.CODARTICULO     
        LEFT JOIN ART_FAMILIA f ON ar.IDFAMILIA = f.IDFAMILIA     
        LEFT JOIN RUTAS r on C.IDRUTAORIGINAL = r.IDRUTA  
        LEFT JOIN CLIENTES cl ON cl.IDCLIENTE = c.IDCLIENTE
        WHERE C.FECHA BETWEEN '02.10.2014' AND '09.10.2014')     
    GROUP BY COD, DESCRIPCION) T1     
LEFT JOIN (    
    SELECT SUM(CANT2) AS CANTIDAD_2,     
    SUM(CANT_OBSEQUIO2) AS OBSEQUIO_2,     
    SUM(CANT_ABONO2) AS ABONO_2,     
    (SUM(CANT2) + SUM(CANT_OBSEQUIO2) - SUM(CANT_ABONO2)) AS TOTAL_2,     
    CODIGO2 as COD2
    FROM (     
        SELECT DISTINCT D.IDALBARAN AS IDALB, D.CODARTICULO AS CODIGO2, D.DESCRIPCION_PRODUCTO AS DESCR2,
        IIF(D.IDCONCEPTO IS NULL OR D.IDCONCEPTO = '', D.CANTIDAD, 0) AS CANT2,     
        IIF(D.IDCONCEPTO = 'O' OR D.IDCONCEPTO = 'P', D.CANTIDAD, 0) AS CANT_OBSEQUIO2,     
        IIF(D.IDCONCEPTO = 'A', D.CANTIDAD, 0) AS CANT_ABONO2, C.CODCLIENTE     
        FROM ALBARANESVENTA_DETALLE D     
        LEFT JOIN ALBARANESVENTA_CABECERA C ON C.IDALBARAN = D.IDALBARAN     
        LEFT JOIN ARTICULOS ar ON D.CODARTICULO = ar.CODARTICULO     
        LEFT JOIN ART_FAMILIA f ON ar.IDFAMILIA = f.IDFAMILIA     
        LEFT JOIN RUTAS r on C.IDRUTAORIGINAL = r.IDRUTA
        LEFT JOIN CLIENTES cl ON cl.IDCLIENTE = c.IDCLIENTE
        WHERE C.FECHA BETWEEN '10.10.2014' AND '17.10.2014')     
        GROUP BY COD2) T2     
ON T1.COD = T2.COD2 
GROUP BY COD, DESCRIPCION, CANTIDAD_1, OBSEQUIO_1, ABONO_1, TOTAL_1, CANTIDAD_2, OBSEQUIO_2, ABONO_2, TOTAL_2 

查询结果: Result query

我希望得到这样的结果:

Desired result

1 个答案:

答案 0 :(得分:0)

为什么不将结果输出到临时表。然后,您就可以使用此表来按客户端汇总数据。 IE将临时表加入自身。