SQL grouping information

Asked

Viewed 50 times

0

I have the following sql statement:

SELECT * FROM TB_CADPRECO
WHERE MAT_GESTOR = 123
AND UF_PRODUTO LIKE 'PR'
AND TIPO_CAMPANHA = 5
AND DT_CAMPANHA = '01.08.2018'
UNION
SELECT * FROM TB_CADPRECO
WHERE MAT_GESTOR = 123
AND UF_PRODUTO LIKE 'PR' 
AND TIPO_CAMPANHA = 0
AND DT_CAMPANHA IS NULL
ORDER BY 1

In this scenario:

|produto|mat_gestor|uf_produto|tipo_campanha|dt_campanha|  dt_alteracao   |
|  1    |   123    | PR       |   0         |    null   |01.01.18 23:54:56|
|  2    |   123    | PR       |   0         |    null   |01.01.18 23:53:56|
|  3    |   123    | PR       |   0         |    null   |01.01.18 23:52:56|
|  4    |   123    | PR       |   0         |    null   |01.01.18 23:51:56|
|  5    |   123    | PR       |   0         |    null   |01.01.18 23:50:56|
|  5    |   123    | PR       |   5         | 01.08.2018|01.01.18 22:58:56|
|  2    |   123    | PR       |   5         | 01.08.2018|01.01.18 23:55:56|

I want you to return me like this:

|produto|mat_gestor|uf_produto|tipo_campanha|dt_campanha| dt_alteracao  |
|  1    |   123    | PR       |   0         |    null   |01.01.18 23:54:56|
|  2    |   123    | PR       |   5         | 01.08.2018|01.01.18 23:55:56|
|  3    |   123    | PR       |   0         |    null   |01.01.18 23:52:56|
|  4    |   123    | PR       |   0         |    null   |01.01.18 23:51:56|
|  5    |   123    | PR       |   5         | 01.08.2018|01.01.18 22:58:56|

I’m trying to use UNION but it didn’t work.

I tried using case:

 SELECT * FROM TB_CADPRECO CAD
 WHERE CAD.MAT_GESTOR = 2111531
 AND CAD.UF_PRODUTO LIKE 'PR' AND
 case when CAD.TIPO_CAMPANHA = 5 and CAD.DT_CAMPANHA = '01.08.2018' then
   CAD.TIPO_CAMPANHA = 5 and CAD.DT_CAMPANHA = '01.08.2018'
 else
   CAD.TIPO_CAMPANHA = 0 and CAD.DT_CAMPANHA is null
 end
 ORDER BY produto

But you keep bringing in duplicate records. I can’t put Distinct in select because I have datetime fields.

Resolved as follows:

 SELECT CAD.*, P.DESC_PRODUTO AS desc_produto,
 FROM TB_CADPRECO CAD
 LEFT JOIN TB_PRODUTO P ON CAD.COD_PRODUTO = P.COD_PRODUTO
 WHERE
 MAT_GESTOR = 123
 AND UF_PRODUTO LIKE 'PR'
 AND
 (
 (
 (TIPO_CAMPANHA = 0 AND DT_CAMPANHA IS NULL) and CAD.cod_produto not in
 (
   SELECT cod_produto FROM TB_CADPRECO
   WHERE
   MAT_GESTOR = 123
   AND UF_PRODUTO LIKE 'PR'
   AND (TIPO_CAMPANHA = 5 AND DT_CAMPANHA = '01.08.2018')
  )
 )
OR
(TIPO_CAMPANHA = 5 AND DT_CAMPANHA = '01.08.2018')
)
ORDER BY 1
  • It wouldn’t be, because the "IN" instruction will bring everyone.

1 answer

0

Test the following code:


SELECT produto,mat_gestor,uf_produto,max(tipo_campanha),max(dt_campanha)
FROM TB_CADPRECO
WHERE MAT_GESTOR = 123
AND UF_PRODUTO LIKE 'PR'
AND
(
  (TIPO_CAMPANHA = 5 AND DT_CAMPANHA = '01.08.2018')
  OR
  (TIPO_CAMPANHA = 0 AND DT_CAMPANHA IS NULL)
)
GROUP BY produto,mat_gestor,uf_produto
ORDER BY produto

  • Felipe, there is another way to use "SELECT * because I have several fields, in the example illustration I spent only a few.

  • @Tiagocasanova as in this solution I grouped the data, will have to describe all of them in select and also in group by, but in group by should not be the fields with aggregation function, which in this case are: tipo_campanha,dt_campanha

  • I can’t because I have datetime field, which I bring in select *, if I bring in the select manual, the filter does not work, as it keeps bringing the two records.

  • I didn’t understand your problem with the Datetime field... it’s no problem to have a Datetime field and also select * has no change in the actual execution of the script. Using * or describing all fields ends up being the same command executed.

  • I’m sorry I wasn’t very clear, I have a field, for example, PC_INDICE that in one record this null in the other this with value, when I put this field in select it brings the two record, like it "ignores" the max.

  • Put MAX(PC_INDICE) and nay placed it in group by? Can edit the question and include the full select, even if it is large?

  • You can do it another way, but thanks for the tips.

  • Good! Share how you resolved, so that the staff who get to the topic, find the solution.

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.