Agrupar en un query (2009-09-14)

14/09/2009 - 13:28 por Mauricio | Informe spam
Hola a todos,
tengo este query divertido:

SELECT distinct
PROGRP_DESCRIPTION, PRO_DESCRIPTION, FT_NAME, OL_FOOD_REMARK, (SELECT
sum(OL_Quantity) FROM MCS_ORDERLINES OL
inner join MCS_ORDERS ORD ON OL.OL_ORD_ID = ORD.ORD_ID
inner join MCS_PRODUCTS PRO ON OL.OL_PRO_ID = PRO.PRO_ID
inner join MCS_ProductGroups PROGRP ON PRO.PRO_PROGRP_ID =
PROGRP.PROGRP_ID
inner join MCS_FoodTypes FT ON OL.OL_FT_ID = FT.FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59' AND
OL.OL_Portion_Size = 1 AND
OL.OL_ORD_ID = MCS_ORDERS.ORD_ID AND OL.OL_PRO_ID = MCS_PRODUCTS.PRO_ID
AND
PRO.PRO_PROGRP_ID = MCS_ProductGroups.PROGRP_ID AND
OL.OL_FT_ID = Mcs_FoodTypes.FT_ID) AS Cantidad1,
(SELECT sum(OL_Quantity) FROM MCS_ORDERLINES OL
inner join MCS_ORDERS ORD ON OL.OL_ORD_ID = ORD.ORD_ID
inner join MCS_PRODUCTS PRO ON OL.OL_PRO_ID = PRO.PRO_ID
inner join MCS_ProductGroups PROGRP ON PRO.PRO_PROGRP_ID =
PROGRP.PROGRP_ID
inner join MCS_FoodTypes FT ON OL.OL_FT_ID = FT.FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59' AND
OL.OL_Portion_Size = 2 AND
OL.OL_ORD_ID = MCS_ORDERS.ORD_ID AND OL.OL_PRO_ID = MCS_PRODUCTS.PRO_ID
AND
PRO.PRO_PROGRP_ID = MCS_ProductGroups.PROGRP_ID AND
OL.OL_FT_ID = Mcs_FoodTypes.FT_ID) AS Cantidad2,
(SELECT sum(OL_Quantity) FROM MCS_ORDERLINES OL
inner join MCS_ORDERS ORD ON OL.OL_ORD_ID = ORD.ORD_ID
inner join MCS_PRODUCTS PRO ON OL.OL_PRO_ID = PRO.PRO_ID
inner join MCS_ProductGroups PROGRP ON PRO.PRO_PROGRP_ID =
PROGRP.PROGRP_ID
inner join MCS_FoodTypes FT ON OL.OL_FT_ID = FT.FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59' AND
OL.OL_Portion_Size = 3 AND
OL.OL_ORD_ID = MCS_ORDERS.ORD_ID AND OL.OL_PRO_ID = MCS_PRODUCTS.PRO_ID
AND
PRO.PRO_PROGRP_ID = MCS_ProductGroups.PROGRP_ID AND
OL.OL_FT_ID = Mcs_FoodTypes.FT_ID) AS Cantidad3
FROM MCS_ORDERLINES
inner join MCS_ORDERS ON OL_ORD_ID = ORD_ID
inner join MCS_PRODUCTS ON OL_PRO_ID = PRO_ID
inner join MCS_ProductGroups ON PRO_PROGRP_ID = PROGRP_ID
inner join MCS_FoodTypes ON OL_FT_ID = FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59'

Que me da el siguiente resultado:
Group 9999 Abrikos, tørret Food type 1 NULL NULL 0.000 NULL
Group 9999 Abrikos, tørret Food type 1 NULL 2.000 NULL NULL
Group 9999 Abrikos, tørret Food type 1 Test 1.000 NULL NULL
Group 9999 Brøndkarse, rå Food type 1 NULL NULL NULL 1.000
Group 9999 Brøndkarse, rå Food type 1 NULL 2.000 NULL NULL
Group 9999 Bønnespirer, soja, konserves
Food type 1 dsfdsfsd 4.000 NULL NULL
Group one Oksemørbrad Food type 1 NULL NULL NULL 3.000
Group one Oksemørbrad Food type 1 NULL 6.000 NULL NULL
Group one Recipe 6003 Food type 1 NULL NULL NULL 1.000
Group one Recipe 6003 Food type 1 NULL NULL 0.000 NULL
Group one Recipe 6003 Food type 1 NULL 2.000 NULL NULL

Si nos fijamos en las 2 primeras filas las 4 primeras columnas son
iguales. Cómo puedo agrupar el query por esas 4 columnas?
Gracias desde ya.

Mauricio
Copenhague, Dinamarca
 

Leer las respuestas

#1 Ruben Garrigos
14/09/2009 - 13:37 | Informe spam
Hola Mauricio,

Deberás añadir una cláusula group by por las columnas que indicas (PROGRP_DESCRIPTION,
PRO_DESCRIPTION, FT_NAME, OL_FOOD_REMARK). Para el resto de funciones deberás
elegir la función de agregado más apropiada. Entiendo que en tu caso la función
apropiada será la suma (SUM) aunque quizás deberías replantearte la reescritura
de toda la consulta intentando evitar en lo posible las subconsultas (que
tienen pinta de costosas a priori).

Un saludo,

Rubén Garrigós
Solid Quality Mentors

Blog: http://blogs.solidq.com/es/elrincondeldba

Hola a todos,
tengo este query divertido:
SELECT distinct
PROGRP_DESCRIPTION, PRO_DESCRIPTION, FT_NAME, OL_FOOD_REMARK, (SELECT
sum(OL_Quantity) FROM MCS_ORDERLINES OL
inner join MCS_ORDERS ORD ON OL.OL_ORD_ID = ORD.ORD_ID
inner join MCS_PRODUCTS PRO ON OL.OL_PRO_ID = PRO.PRO_ID
inner join MCS_ProductGroups PROGRP ON PRO.PRO_PROGRP_ID > PROGRP.PROGRP_ID
inner join MCS_FoodTypes FT ON OL.OL_FT_ID = FT.FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59' AND
OL.OL_Portion_Size = 1 AND
OL.OL_ORD_ID = MCS_ORDERS.ORD_ID AND OL.OL_PRO_ID > MCS_PRODUCTS.PRO_ID
AND
PRO.PRO_PROGRP_ID = MCS_ProductGroups.PROGRP_ID AND
OL.OL_FT_ID = Mcs_FoodTypes.FT_ID) AS Cantidad1,
(SELECT sum(OL_Quantity) FROM MCS_ORDERLINES OL
inner join MCS_ORDERS ORD ON OL.OL_ORD_ID = ORD.ORD_ID
inner join MCS_PRODUCTS PRO ON OL.OL_PRO_ID = PRO.PRO_ID
inner join MCS_ProductGroups PROGRP ON PRO.PRO_PROGRP_ID > PROGRP.PROGRP_ID
inner join MCS_FoodTypes FT ON OL.OL_FT_ID = FT.FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59' AND
OL.OL_Portion_Size = 2 AND
OL.OL_ORD_ID = MCS_ORDERS.ORD_ID AND OL.OL_PRO_ID > MCS_PRODUCTS.PRO_ID
AND
PRO.PRO_PROGRP_ID = MCS_ProductGroups.PROGRP_ID AND
OL.OL_FT_ID = Mcs_FoodTypes.FT_ID) AS Cantidad2,
(SELECT sum(OL_Quantity) FROM MCS_ORDERLINES OL
inner join MCS_ORDERS ORD ON OL.OL_ORD_ID = ORD.ORD_ID
inner join MCS_PRODUCTS PRO ON OL.OL_PRO_ID = PRO.PRO_ID
inner join MCS_ProductGroups PROGRP ON PRO.PRO_PROGRP_ID > PROGRP.PROGRP_ID
inner join MCS_FoodTypes FT ON OL.OL_FT_ID = FT.FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59' AND
OL.OL_Portion_Size = 3 AND
OL.OL_ORD_ID = MCS_ORDERS.ORD_ID AND OL.OL_PRO_ID > MCS_PRODUCTS.PRO_ID
AND
PRO.PRO_PROGRP_ID = MCS_ProductGroups.PROGRP_ID AND
OL.OL_FT_ID = Mcs_FoodTypes.FT_ID) AS Cantidad3
FROM MCS_ORDERLINES
inner join MCS_ORDERS ON OL_ORD_ID = ORD_ID
inner join MCS_PRODUCTS ON OL_PRO_ID = PRO_ID
inner join MCS_ProductGroups ON PRO_PROGRP_ID = PROGRP_ID
inner join MCS_FoodTypes ON OL_FT_ID = FT_ID
WHERE
OL_DATE BETWEEN '20090901 00:00:00' AND '20090914 23:59:59'
Que me da el siguiente resultado:
Group 9999 Abrikos, tørret Food type 1 NULL NULL 0.000 NULL
Group 9999 Abrikos, tørret Food type 1 NULL 2.000 NULL NULL
Group 9999 Abrikos, tørret Food type 1 Test 1.000 NULL NULL
Group 9999 Brøndkarse, rå Food type 1 NULL NULL NULL 1.000
Group 9999 Brøndkarse, rå Food type 1 NULL 2.000 NULL NULL
Group 9999 Bønnespirer, soja, konserves
Food type 1 dsfdsfsd 4.000 NULL NULL
Group one Oksemørbrad Food type 1 NULL NULL NULL 3.000
Group one Oksemørbrad Food type 1 NULL 6.000 NULL NULL Group one
Recipe 6003 Food type 1 NULL NULL NULL 1.000 Group one Recipe 6003
Food type 1 NULL NULL 0.000 NULL Group one Recipe 6003 Food type 1
NULL 2.000 NULL NULL

Si nos fijamos en las 2 primeras filas las 4 primeras columnas son
iguales. Cómo puedo agrupar el query por esas 4 columnas?
Gracias desde ya

Preguntas similares