Possible Duplicate:
SQL : Count the number of occurences occuring on output column and calculate some percentage based on the occurences
Here is url for test data / table : http://sqlfiddle.com/#!2/56ffd4/1
My table generates o/p for following table :
(id, resolution)
('abc-123', 'fail'),
('abc-456', 'pass'),
('abc-789', 'pass'),
('abc-799', 'fail'),
('abc-800', 'pass'),
('abc-900', 'pass');
my script o/p is :
id RESOLUTION TS @PREV C RES
abc-123 fail July, 02 2012 1 1 -
abc-456 pass July, 02 2012 2 0 50.00%
abc-789 pass July, 02 2012 1 0 100.00%
abc-799 fail July, 02 2012 1 1 -
abc-800 pass July, 02 2012 2 0 50.00%
abc-900 pass July, 02 2012 0 0 100.00%
here is o/p script:
SELECT st.*,
@prev:=@counter + 1,
@counter:= CASE
WHEN st.resolution = 'pass'
THEN 0
ELSE @counter + 1
END c,
CASE WHEN @counter = 0
THEN CONCAT(FORMAT(100/@prev, 2), '%')
ELSE '-'
END res
FROM so_test st, (SELECT @counter:=0) sc
I need to append two columns to above output table to count occurrences for passing and fail values as:
id RESOLUTION TS @PREV C fail pass
abc-123 fail July, 02 2012 1 1 - 1
abc-456 pass July, 02 2012 2 0 50.00% 1
abc-789 pass July, 02 2012 1 0 100.00% 1
abc-799 fail July, 02 2012 1 1 - 1
abc-800 pass July, 02 2012 2 0 50.00% 1
abc-900 pass July, 02 2012 0 0 100.00% 1
Well if you just want the pass and fail columns at the end of the output table as you’ve specified then put a comma after
and add the following after it:
Not sure what the blank is in your columns but I’ve just set them as NULL.