Jump to content

Improve existing coding style?


alvin567

Recommended Posts

$person =  array(1,2,3,4,5,6,7,8,9,10);
       foreach($person as $person){
           $sizeOfCompany = $this->Claim->query("Select count(*),count(*) * 100/(Select count(*) from claims) from claims where size = '$person' ");
                                    # of person,No of firms,% of contributions
           array_push($data,array($person,$sizeofCompany[0][0]['computed'],$sizeofCompany[0][0]['computed1'] . '%'));
       }
        $sizeofCompany = $this->Claim->query("Select count(*),count(*) * 100/(Select count(*) from claims) from claims where size Between 11 and 19");
             array_push($data,array('11-19',$sizeofCompany[0][0]['computed'],$sizeofCompany[0][0]['computed1'] . '%'));
        $sizeofCompany = $this->Claim->query("Select count(*),count(*) * 100/(Select count(*) from claims) from claims where size Between 20 and 40");
             array_push($data,array('20-40',$sizeofCompany[0][0]['computed'],$sizeofCompany[0][0]['computed1'] . '%'));
        $sizeofCompany = $this->Claim->query("Select count(*),count(*) * 100/(Select count(*) from claims) from claims where size > 40");
             array_push($data,array('Over 40',$sizeofCompany[0][0]['computed'],$sizeofCompany[0][0]['computed1'] . '%'));
         array_push($data,'');

 

How can i improve the quality of this code?

I want to find the range 1,2,3,4,5,6,7,8,9,10,11-19,20-40,Over 40

And then i want to calculate the percentage of the total count

Link to comment
Share on other sites

I manage to do this some how

 

Select size,

Case

when size = 1 then COUNT(*)

when size = 2 then COUNT(*)

when size = 3 then COUNT(*)

when size = 4 then COUNT(*)

when size = 5 then COUNT(*)

when size = 6 then COUNT(*)

when size = 7 then COUNT(*)

when size = 8 then COUNT(*)

When size = 9 then COUNT(*)

When size = 10 then COUNT(*)

When size Between 11 and 19 then COUNT(*)

When size Between 20 and 40 then COUNT(*)

End As Count

from  group by size order by size asc;

 

but the size is returns 11 and 19 between all?

Link to comment
Share on other sites

SELECT 
    size_group,
    COUNT(*) AS quantity
FROM (
    SELECT size, CASE 
            WHEN size <= 10 THEN CAST(size AS CHAR)
            WHEN size BETWEEN 11 AND 19 THEN '11-19'
            WHEN size BETWEEN 20 AND 40 THEN '11-40'
        END AS size_group
    FROM table_name) AS table_alias
GROUP BY size_group
ORDER BY size_group ASC;

Link to comment
Share on other sites

This will fix your ordering issues

SELECT 
    size_group,
    COUNT(*) AS quantity
FROM (
    SELECT size, CASE 
            WHEN size <= 10 THEN CAST(size AS CHAR)
            WHEN size BETWEEN 11 AND 19 THEN '11-19'
            WHEN size BETWEEN 20 AND 40 THEN '11-40'
        END AS size_group
    FROM table_name) AS table_alias
GROUP BY size_group
ORDER BY MAX(size) ASC;

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.