Mysql query with 3 different criteria within the same table in the DB

Asked

Viewed 117 times

1

I need to create a query Mysql/PHP with 3 different criteria, but within the same BD table.

Tabela Negociações (Nome: tbl_neg)
ID    STS     VALOR
 1     1    R$23.000,00
 2     2    R$60.000,00
 3     1    R$10.000,00
 4     1    R$50.000,00
 5     6    R$20.000,00
 6     2    R$88.000,00
 7     6    R$15.000,00

I need to make a query in this table, but in the status column (name: Sts) there will always be three criteria, quote 1, 2 and 6, that I need to separate them and then add them up.

I don’t know if you can understand my "question". I’m thinking of something like a Join in the same table, because I’ve been doing a search on Dad (Google) and I know it exists. Would it be possible to use something like:

SELECT * FROM tb_neg a 
INNER JOIN tb_neg b ON a.stsneg = '1' AS analise 
INNER JOIN tb_neg c ON a.stsneg = '2' AS aprovado 
INNER JOIN tb_neg d ON a.stsneg = '6' AS reprovado

And then: (take each value)

$vranalise = $row["analise"];
$vraprovado = $row["aprovado"];
$vrreprovado = $row["reprovado"];

And then: (Imaginary Excel type: take the sub-total of each status)

$totalanalise = SOMA($vranalise);
$totalaprovado = SOMA($vraprovado);
$totalreprovado = SOMA($vrreprovado);

And then: (take total value)

$vrgeral = ($totalanalise + $totalaprovado + $totalreprovado);

I know it’s kind of imaginary, but I don’t know how to do it like you do and "catch" the hell out of it! ?

  • ai ai ai ai ai... shuffled everything again... This time I did everything certin in Word... copied, glued and went wrong... Aff

  • I managed to give spaces... was 10! rs

  • The text is markdown-formatted and not HTML-formatted. That’s why you’re having difficulties.

  • I couldn’t figure out what you were asking. Can you give an example of calculation and expected results?

  • It’s like Murilão is saying! Caracas!! when I look at this exit... Ussa... this is what I need guys! I’ll test later... it will be huh!

  • Thanks for formatting Guto... the text was beautiful! rs

Show 1 more comment

1 answer

1


Buddy, it can be done using a pivot (turn the rows into columns).
If you want the select bring you 3 columns analise, aprovado and reprovado with the summed values, use this select:

SELECT SUM(IF(sts = 1, valor, NULL)) AS 'analise',
       SUM(IF(sts = 2, valor, NULL)) AS 'aprovado',
       SUM(IF(sts = 6, valor, NULL)) AS 'reprovado'
FROM tbl_neg;

The output of this select for the example you entered is:

analise    aprovado     reprovado  
83000       148000        35000

Now if you want the select to swallow the same 3 columns but without adding, remove the SUM of each row and you will get the result:

analise     aprovado    reprovado
23000       (null)      (null)
(null)      60000       (null)
10000       (null)      (null)
50000       (null)      (null)
(null)      (null)      20000
(null)      88000       (null)
(null)      (null)      15000

You can test the example above here

Edited
As requested by the author, the complete code for solving the problem using the above example as a basis is:
1 - Change the mask so that the currency values are saved in the bank in American format, from R$15,000.55 to 15000.55

// --------- SOMA DE VALORES - TOTAL - PORCENTAGEM ------------
$sqlnegvr = mysqli_query($conn, "SELECT SUM(IF(stsneg = '1', vrpro, NULL)) AS 'analise', SUM(IF(stsneg = '2', vrpro, NULL)) AS 'aprovado', SUM(IF(stsneg = '6', vrpro, NULL)) AS 'reprovado' FROM tb_neg");
$rowvr = $sqlnegvr->fetch_assoc();
$aprovado = $rowvr["aprovado"];
$analise = $rowvr["analise"];
$reprovado = $rowvr["reprovado"];

//----- VALORES SEPARADOS
$vra = number_format($aprovado, 2,",",".");
$vraprovado = "R$ " . $vra;
$vrn = number_format($analise, 2,",",".");
$vranalise = "R$ " . $vrn;
$vrr = number_format($reprovado, 2,",",".");
$vrreprovado = "R$ " . $vrr;

//----- SOMA TUDO
$somat = ($vrn + $vra + $vrr);
$vrgt = number_format( $aprovado + $analise +$reprovado, 2,",",".");
$vrgeral = "R$ " .  $vrgt;

//----- PORCENTAGEM DE ANÁLISE
$ptvra = ($vra * 100) / $somat;
$nwpta = round($ptvra, 1);
$ptvra  = str_replace('.', ',', $nwpta);
$pcvag = str_replace('.', '', $nwpta);
$ptvrag = $pcvag;

  //----- PORCENTAGEM DE APROVADO 
$ptvrn = ($vrn * 100) / $somat;
$nwptn = round($ptvrn, 1);
$ptvrn  = str_replace('.', ',', $nwptn);
$pcvng = str_replace('.', '', $nwptn);
$ptvrng = $pcvng;

  //----- PORCENTAGEM DE REPROVADO  
$ptvrr = ($vrr * 100) / $somat;
$nwptr = round($ptvrr, 1);
$ptvrr  = str_replace('.', ',', $nwptr);
$pcvrg = str_replace('.', '', $nwptr);
$ptvrrg = $pcvrg;
  • Opa Murilão! How are you buddy? Thanks for the kindness of the answer saw... I’ll test in a little while... when I see this exit there... man, I can’t wait to see... It’s full of pennies in these figures... Will it be? that the SUM will not arrendondar huh? Thanks guy!

  • @Infonet the SUM should not round, it will add up the pennies normally, there in the example it does not show the pennies because the same ones are 0, if you were a value greater than 0 would appear.

  • Hey buddy... hold on... thanks a lot! But 10.250,00 + 10.100,55 = 10.350,55 isn’t it? Yeah... here is 20.35 Aff

  • correction: 20.350.55

  • Sorry there Murilão... the mistake was mine then of not having explained that there would be pennies... (this forum here is kind of complicated with questions, it seems not like them rsrs... prank). Anyone else with any brilliant ideas? :)

  • @Infonet, I don’t understand you, do you want me to start or not? In the sum 10.250,00 + 10.100,55 using the SUM the result will be 20350,55, isn’t that what you need? What value do you want to appear? see a test using the quoted sum here.

  • Wow man, I’m just saying that to me appears: 20.35 (TWENTY POINT THIRTY AND FIVE) and I did just like CRTL + C and CTRL + V and if the SUM is not rounding it is because I’m crazy!

  • @Infonet understood, do the following, so you can check the values, strip the SUM and rotates this same select, it will separate the values by column, (as it is in my second example) there if you can put this result so that we can see what is your sample of values.

  • Thanks Murilão, I’ve even solved the problem (rounding of the SUM) about DISPLAY/ SOMA; decimal places; American currency/ currency "Brazil"... If it wasn’t for your SQL query example, I wouldn’t have been able to! I’d like to post the solution I found, but the field here doesn’t fit! Give me your e-mail and I’ll send you... rsrs?)

  • @Infonet, if you want me to post, you can send it to [email protected]

  • I sent it, buddy! Look, man.... vlw

  • @Infonet, I just edited the answer and added the code you informed me! Note: If the answer helped you please consider marking it as correct to help others with the same question.

  • Yes dude, that’s it, but we can’t help but comment that in the bank, the currency values must be written in American format: FROM R$1,500.55 to 1500.55. Thank you and may God enlighten you!

  • @Infonet ok, added the information! May God enlighten you too and good coding!

Show 9 more comments

Browser other questions tagged

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