Compare data sql

Asked

Viewed 1,403 times

1

I need to compare date in a certain time interval. I need to pick up the field DAT_REPOT_REALZ and check the dates between TODAY and seven days ago. I also need to count the number of V (visitor) and the number of L (leader) and M (participant), at query that I did didn’t work out because I’m not very familiar with sql.

It is worth noting that I am using the MYSQL.

I tried to do so

select case FLG_IDENT_PESSO when 'V' then count(FLG_IDENT_PESSO) when 'M' then count(FLG_IDENT_PESSO) end from tbl_PRESENCA WHERE FLG_IDENT_PRESE = 'S' and DAT_REPOT_REALZ <= now()-7

The structure of my table:

COD_IDENT_REUNI bigint(20) UN PK 
COD_IDENT_PESSO bigint(20) UN PK 
FLG_IDENT_PRESE char(1) //PODE SER S (presente) ou N (nao presente)
FLG_IDENT_PESSO char(1) // PODE SER V (visitantes) ou L (lider) ou ainda M (participante)
DAT_REPOT_REALZ datetime // É O DIA EM QUE FOI FEITO O REPORTE
  • You need to wear a BETWEEN or two checks in WHERE to work the way you want.

  • What the query would look like?

2 answers

2

It is possible to do so:

select 
    count(COD_IDENT_PESSO); 
FROM 
    tbl_PRESENCA 
WHERE 
    DAT_REPOT_REALZ BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() AND 
    FLG_IDENT_PRESE = 'S';

SQL above will return the total number of people who were present at the meeting. To count separately, just use another condition in WHERE

select 
    count(FLG_IDENT_PESSO); 
FROM 
    tbl_PRESENCA 
WHERE 
    DAT_REPOT_REALZ BETWEEN DATE_SUB(CURDATE(), INTERVAL 7 DAY) AND CURDATE() AND 
    FLG_IDENT_PRESE = 'S' AND
    FLG_IDENT_PESSO = '?';

Where '?' would be what you want to look for in particular, V, M or L

select 
    sum(case FLG_IDENT_PESSO when 'V' then 1 else 0 end) as 'Visitantes',
    sum(case FLG_IDENT_PESSO when 'L' then 1 else 0 end) as 'Lideres',
    sum(case FLG_IDENT_PESSO when 'M' then 1 else 0 end) as 'Participantes',
FROM 
    tbl_PRESENCA 
WHERE 
    DAT_REPOT_REALZ BETWEEN DATE_ADD(CURDATE(), INTERVAL -7 DAY) AND CURDATE() AND 
    FLG_IDENT_PRESE = 'S';

Or all in one select, dividing as it was quoted in the other reply.

  • Because the idea is to count the number of participants that was present , then I have the flag FLG_IDENT_PESSO and I have the possibility in the field to put V that would have to count, and L + M that I need to count, ie I will count the number of visitors, then count number of leader and participant together.

  • I also have a field FLG_IDENT_PRESE that can put’S' and 'N', I need to count only those that have FLG_IDENT_PRESE = ’S'

  • Could you put your table (or her SQL) in your question for me to better understand?

  • I updated the question

  • I updated my answer, any questions just ask here in the comments

  • Together in the same query you can do no ?

  • WITH LEFT JOIN I CAN’T DO THIS ?

  • it is possible to use a UNION to join the selects, each with a Where using a FLG_IDENT_PESSO = '?' different

Show 3 more comments

1


You can use BETWEEN and the function DATE_SUB

Your Where looks like this : where DAT_REPOT_REALZ between DATE_SUB(now(), interval 7 day) and now()

Follows a fiddle for example .

Regarding Count, you can put a sum in a case

example :

SUM(case FLG_IDENT_PESSO when 'V' then 1 else 0 end) as Visitantes

full query :

    select sum(case FLG_IDENT_PESSO when 'V' then 1 else 0 end) as visitantes , 
    sum(case FLG_IDENT_PESSO when 'M' then 1 when 'L' then 1 else 0 end) as participantes 
  from tbl_PRESENCA where DAT_REPOT_REALZ between DATE_SUB(now(), interval 7 day) and now()
  • you’re 10, just what you needed

  • Aeew o// ! Glad it worked out

  • I’d only have one thing to add because M might be L too, which means I need to count if it’s M and if it’s L too, which is how I’d be ?

  • Same logic Renan , I will edit the answer , but you have to put a SUM() with a case inside it , and then add 1 for each time the comparison is met and 0 on Else . ( to solve this way , there are other solutions)

  • No you did not understand if the guy is the leader or if he is a participant will have to add one more, for example has 3 L and 2 M then will have 5 participants

  • got it !! so you have to put one more when , I’ll modify again !

Show 1 more comment

Browser other questions tagged

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