2
Based on the query below, I need to add the column Percentage, being the calculation of Passed
on top of the Total
but I do not know how to take the values of this same select to insert in the calculation.
SELECT
Data,
SUM(CASE WHEN Status = 'OK' THEN 1 ELSE 0 END) as Passed,
SUM(CASE WHEN Status = 'NOK' THEN 1 ELSE 0 END) as Failed,
COUNT(*) AS Total
FROM VWDADOSFPY
GROUP BY Data
ORDER BY Data DESC;
The table should look like this:
Data Passed Failed Total Porcentagem
2018-03-15 470 5 475 98,94%
2018-03-14 485 17 502 96,61%
2018-03-13 1631 74 1705 95,65%
Someone can help?
In T-SQL it is not possible to use an alias created in the same SELECT. See the error in the test with the code you proposed: http://sqlfiddle.com/#! 18/1257d/1
– José Diz
True @Josédiz ! I got it right... I thought of one thing and made another ! rs
– rbz
Ok. The use of subconsultation is one of the solutions. Just add the comma before Count(), ok?
– José Diz
I thought it was the people of slutty, who comes in giving -1 to medal ! Sorry there ! Thanks for the attention !
– rbz
Thanks for the help, I just had to adjust some things. But that was it
– rmneves
@rmneves this answer is not 100% correct. The two decimal digits will always be 00
– EstevaoLuis
@Estevaoluis explain to me how you tested ? Here is normal http://sqlfiddle.com/#! 9/43b5aa/8
– rbz
@RBZ Voce is using Mysql. The question is about SQL Server which gives a different result
– EstevaoLuis
I had to use CAST to get the decimals.
– rmneves
Whoa, my fault, I didn’t realize !
– rbz
Fixed. Well noted @Estevaoluis. That’s what your answer should have, or even just this comment as you did here. The intention is not a lot of answers, but good answers, well explained, answers with other forms of execution, etc... But failure happens !
– rbz