Ahead of this question
Sqlite 3 Insert and Replace fails on more than 1 unique column
I have a table with schema,
CREATE TABLE tbl_poll (
id INTEGER PRIMARY KEY AUTOINCREMENT,
poll_id STRING NOT NULL,
ip_address STRING NOT NULL,
opt STRING NULL,
CONSTRAINT 'unique_vote_per_poll_per_ip_address' UNIQUE ( poll_id, ip_address ) ON CONFLICT REPLACE
);
When I do,
select opt,count(opt) as count from tbl_poll where poll_id = 'jsfw' group by opt
Result is
opt count
0 4
2 2
3 2
i.e. 4 users have selected 0 option, 2 and 3 option is selected by 2 and 2 users respectively.
Is there any way so I can get a result like following
opt count percent
0 4 0.5
2 2 0.25
3 2 0.25
where percent = count / total count
If I can get total count i.e. (4+2+2 = 8 ) that will solve my problem too.
I have tried this,
select opt,count(opt) as count from tbl_poll where poll_id = 'jsfw'
but it doesn’t work as no of columns are not same.
1 Answer