Count numbers drawn on a lottery table

Asked

Viewed 283 times

1

I am new to BD studies and I have a table with the results of Lotofácil where I would like to do a Count that looked for, for example, how many times the tens 20 was drawn. I have done several tests but I only got column by column and if I put a general Count it points to values considering the dates and goes wrong. Below a part of the image of my bank.

Sorteios

  • 1

    Could show your failed tests and a return of the result of this example that shows?

  • 'code'SELECT COUNT(18) FROM sorteiosanteriores'code was the first one that resulted in the count informing the data contained in the date field. Another attempt was 'code'SELECT COUNT(*) FROM sorteiosanteriores WHERE Bola1 = 18 AND Bola2 = 18'code' returning zero

2 answers

6

Since this is a draw where there is no repetition of values in the columns, you can use the operator IN:

SELECT
      COUNT(*)
FROM
      sorteiosanteriores
WHERE
      20 IN (bola1,bola2,bola3,bola4,bola5,bola6 ... bola15)

See working on SQL FIDDLE.

Now, you need to see if this "horizontal" modeling is the best solution. Probably a related table is better than a lot of columns for the same purpose.

Attention: this query was optimized for the case of "Easy", and counts the number of lines that the 20 is returned, regardless of whether it occurred once or several times on the same line. For general count, see @Mauroalmeida’s reply or the following excerpt.

Counting multiple occurrences

SELECT
      SUM((bola1=20)+(bola2=20)+(bola3=20)+ ... +(bola15=20))
      AS quantidade
FROM
     sorteiosanteriores

See working on SQL FIDDLE.

This works in a very simple way. The true in Mysql has value 1, and false 0. Thus, just add the amount of equalities per line, and add with SUM.

This second option counts the total number of occurrences, including repetitions on the same line.

  • 4

    The two answers brought the result I need. @Mauroalmeida has a little more code, Bacco with less. Now I will begin to apply in the studies I am developing. Thank you all for your contribution

4


This answer ignores the fact that the value 20 cannot be repeated since it is one of the business rules this value cannot be repeated in several columns of the same draw.

SET @valor = 20;
select 
      (select count(Bola1) from sorteiosanteriores where Bola1 = @valor) 
    + (select count(Bola2) from sorteiosanteriores where Bola2 = @valor)
    + (select count(Bola3) from sorteiosanteriores where Bola3 = @valor)
    + (select count(Bola4) from sorteiosanteriores where Bola4 = @valor)
    + (select count(Bola5) from sorteiosanteriores where Bola5 = @valor)
    + (select count(Bola6) from sorteiosanteriores where Bola6 = @valor)
    + (select count(Bola7) from sorteiosanteriores where Bola7 = @valor)
    + (select count(Bola8) from sorteiosanteriores where Bola8 = @valor)
    + (select count(Bola9) from sorteiosanteriores where Bola9 = @valor)
    + (select count(Bola10) from sorteiosanteriores where Bola10 = @valor)
    + (select count(Bola11) from sorteiosanteriores where Bola11 = @valor)
    + (select count(Bola12) from sorteiosanteriores where Bola12 = @valor)
    + (select count(Bola13) from sorteiosanteriores where Bola13 = @valor)
    + (select count(Bola14) from sorteiosanteriores where Bola14 = @valor)
    + (select count(Bola15) from sorteiosanteriores where Bola15 = @valor);

What this does is count in all columns the value placed in the @value variable and at the end returns the value of the sum of counts.

For a response based on the business rule see the answer from @Bacchus.

  • 2

    It worked. I managed to generate the calculations and now I will apply in the queries. Thank you

Browser other questions tagged

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