SQL Server : count symbols in game draws -


my question related creating new column in database counts number of times symbol of game called totobola (symbols 1, x, 2)

right have following code have 1 table called chaves 2 rows 2 actual 2016 draws of game.

i want add 3 columns (total1-counts number of 1's, totalx-counts number of x's, total2-counts number of 2's in each of 2 draws):

create database chaves_totobola;  create table chaves (     num_conc int identity(1,1) primary key,     datasort date,     s1 char(1),     s2 char(1),     s3 char(1),     s4 char(1),     s5 char(1),     s6 char(1),     s7 char(1),     s8 char(1),     s9 char(1),     s10 char(1),     s11 char(1),     s12 char(1),     s13 char(1),     s14 varchar(5),     jackpot bit,     premio money );  insert chaves (datasort, s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, jackpot, premio) values ('2016-01-03','2','x','x','x','1','2','2','x','1','2','x','x','2','m : 0','1','0'),        ('2016-01-10','x','2','1','x','1','x','1','1','1','1','1','2','x','m : m','1','0'); 

can please help?

try trick

select *,         (select count(v)            (values (s1),(s2),(s3),(s4),...) value(v)           v in ( '2', '1', 'x' )) total_count    chaves  

to individual count use this

;with cte       (select *,                  (select count(v)                     (values (s1),(s2),(s3),(s4),..) value(v)                    v = '1') count_1,                  (select count(v)                     (values (s1),(s2),(s3),(s4),..) value(v)                    v = 'x') count_x,                  (select count(v)                     (values (s1),(s2),(s3),(s4),..) value(v)                    v = '2') count_2             chaves)  select count_1 + count_x + count_2 total_count    cte;  

sql fiddle demo


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -