mysql - SQL : Count occurences of values in sql table efficiently -


possible duplicate:
sql : count number of occurences occuring on output column , calculate percentage based on occurences

here url test data / table : http://sqlfiddle.com/#!2/56ffd4/1

my table generates o/p 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 :

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 o/p script:

select st.*,         @prev:=@counter + 1,        @counter:= case           when st.resolution = 'pass'          0          else @counter + 1        end c,        case when @counter = 0              concat(format(100/@prev, 2), '%')              else '-'         end res   so_test st, (select @counter:=0) sc 

i need append 2 columns above output table count occurrences passing , 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 want pass , fail columns @ end of output table you've specified put comma after

end res  

and add following after it:

case when st.resolution = 'fail'             1             else null end fail, case when st.resolution = 'pass'             1             else null end pass 

not sure blank in columns i've set them null.


Comments

Popular posts from this blog

c# - SVN Error : "svnadmin: E205000: Too many arguments" -

c# - Copy ObservableCollection to another ObservableCollection -

All overlapping substrings matching a java regex -