Begayim Muratalina
Begayim Muratalina

Reputation: 661

calculate the frequency

i have the table with variable version with modalities V1, V2, V3, V4 and the other variable call, it can be 1 or 0. what i need to do is to calculate the frequency of 1 to the total number of lines for each modality of version: for ex for V1 i have 6 lines with call equal to 1 and 13 lines in global, i need to create variable freq which will indicate 6/13. what i do for now is i create 2 table: first one with number of lines with call equal 1 for each

version Version Call=1 
             V1      6
             V2      4
             V3      8
             V4      3

and the second table with total number of lines of each version

   Version Call total
     V1      13
     V2      14
     V3      15
     V4      23

Then i join this 2 tables and calculate the frequency. i wanted to ask if there is no another way to do this?

this is my code:

create table Call1 select *, count(*) as call1 from Version where call like '1'group by Version;
create table Calltotal select *, count(*) as calltot from Version group by Version;
update Calltotal change Version Version1 VARCHAR(2);
alter table Call1 add index (Version);
alter table Calltot add index(Version1);
create table final select * from Call1 left join Calltot on Call1.version=Calltot.version1;
alter table final add frequency INT NOT NULL;
update final set frequency=call1/Calltot;

Upvotes: 0

Views: 357

Answers (1)

Strawberry
Strawberry

Reputation: 33935

A pointer...

SELECT thing
     , COUNT(CASE WHEN other_thing = 10 THEN 'foo' END) x 
     , COUNT(*) y  
  FROM my_table 
 GROUP 
    BY thing;

Upvotes: 1

Related Questions