0

I want to get amount of same role in one sum. When I fetch a records of agent details introduced by some agent (11150000001). It will return lot of agents records with different role(1000,2000,3000,4000,5000,6000).

From that I need to get the agents business amount total. Now I'm getting it. But If there is some agent in same role I want to get that same role agent's amount in single amount, but I'm getting separately.

  //get all employee under one employee
            $rank1=''; $rank2='';$rank3='';$rank4='';$rank5='';$rank6='';
               $get_employee="SELECT emp_code,intro_code FROM emp_details WHERE intro_code='".$emp_code."'";
               $exe_employee=mysql_query($get_employee);

               while($fetch_emp=mysql_fetch_assoc($exe_employee))
               {
                     $total_amount1=0;
                     $role='';
                   $employee_code=$fetch_emp['emp_code'];
                   //echo $employee_code."<br>";
                   $get_premium="SELECT emp_code,user_role,premium_amount FROM business_details WHERE emp_code='".$employee_code."'";
                  //echo $get_premium."<br>";
                   $exe_premium=mysql_query($get_premium);

                   while($fetch_amount=mysql_fetch_array($exe_premium))
                   {
                      $total_amount1 +=$fetch_amount['premium_amount'];
                      $role=$fetch_amount['user_role'];  
                   }
              }
      echo $role."-".$total_amount1."<br>"; 
       // Role-Amount
       // 5000-75000
        //  5000-105000
        //3000-15000

enter image description here

In the above image I'm getting 10,500 in rank 5, but I want to get the 75000+10500 in rank 5 column.

Below is my table structure:

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
PHP dev
  • 410
  • 1
  • 7
  • 23
  • Ya!!I'm just a beginner!!will improve it soon!! – PHP dev Feb 13 '15 at 07:10
  • Hi there. Please don't add begging messages to your questions - I've edited two such messages from you just now. Remember that readers here are volunteers, and as such they will answer questions they find interesting, at their leisure. We regard all questions as equally important on Stack Overflow. – halfer Oct 28 '15 at 19:41

2 Answers2

2

The entire block of code than be replaced by a single SQL query. You want to create a pivot table.

Lets start by a relatively simple query that fetches the total premium_amount of all the employee-role combinations:

SELECT emp_code,
       user_role,
       SUM(premium_amount) AS total_amount
FROM business_details
GROUP BY emp_code, user_role;

This would yield something like this:

emp_code    | user_role | total_amount
-----------------------------------
15040000001 | 3000      |  15000     
15040000001 | 5000      | 180000
11130000001 | 4000      |  30000
11130000001 | 1000      |   5000

This already contains all the information you want in your table, but not in the correct layout. Instead of using PHP to reformat this into the rows/columns you want for your table, SQL can do it for us.

Instead of grouping by user_role, we're going to specify the different values user_role can take as different columns:

SELECT emp_code,
       SUM(CASE WHEN user_role=1000 THEN premium_amount ELSE 0 END) AS rank1,
       SUM(CASE WHEN user_role=2000 THEN premium_amount ELSE 0 END) AS rank2,
       SUM(CASE WHEN user_role=3000 THEN premium_amount ELSE 0 END) AS rank3,
       SUM(CASE WHEN user_role=4000 THEN premium_amount ELSE 0 END) AS rank4,
       SUM(CASE WHEN user_role=5000 THEN premium_amount ELSE 0 END) AS rank5,
       SUM(CASE WHEN user_role=5000 THEN premium_amount ELSE 0 END) AS rank6
FROM business_details
GROUP BY emp_code;

The result would be:

emp_code    | rank1 | rank2 | rank3 | rank4 | rank5  | rank6
------------------------------------------------------------
15040000001 |     0 |     0 | 15000 |     0 | 180000 |     0
11130000001 |  5000 |     0 |     0 | 30000 |      0 |     0

Bonus advise

The PHP code you've posted contains a glaring security flaw that leaves your database (or probably your entire system) wide open to anyone. SQL injection is not fun to have in your web application! I've written about it in a previous answer.

Community
  • 1
  • 1
Marijn van Vliet
  • 5,239
  • 2
  • 33
  • 45
  • emp_code | user_role | total_amount ----------------------------------- 15040000002 | 4000 | 15000 15040000001 | 5000 | 180000 11130000001 | 4000 | 30000 11130000001 | 1000 | 5000 In the above code i want to display 45,000 in rank 4(30000+15000 which has same role-4000) – PHP dev Feb 13 '15 at 09:40
  • the sum case query shows syntax error What does it may be?I'm using first time in this query @Rodin – PHP dev Feb 13 '15 at 10:00
  • I just use some test data here, If there are two entries, one with 30000 and one with 15000 that both share the same emp_code and user_role, the query will sum them. – Marijn van Vliet Feb 13 '15 at 10:21
  • You have mentioned about pivot table.How to do this in this case?Can you please guide me @rodin – PHP dev Feb 13 '15 at 10:34
  • Now how can I get the rank 1,rank2 etc values to display it in my format @Rodin – PHP dev Feb 13 '15 at 10:42
1

Try this...

 $get_premium="SELECT emp_code,user_role,premium_amount,SUM(premium_amount) FROM business_details WHERE emp_code='".$employee_code."' group by user_role" ;
Deenadhayalan Manoharan
  • 5,436
  • 14
  • 30
  • 50
  • When I execute your suggestion it returns following query "SELECT emp_code,user_role,premium_amount,SUM(premium_amount) FROM business_details WHERE emp_code='15040000001' group by user_roleSELECT emp_code,user_role,premium_amount,SUM(premium_amount) FROM business_details WHERE emp_code='11130000001' group by user_role – PHP dev Feb 13 '15 at 07:26
  • Now im getting both agents amount but its still not combine it by single sum.Because both are in same agent role – PHP dev Feb 13 '15 at 07:28
  • @paps k SELECT emp_code,user_role,premium_amount,SUM(premium_amount) FROM business_details group by user_role – Deenadhayalan Manoharan Feb 13 '15 at 07:33
  • yes @deena!!one agent can add more than one agent with unique employee code – PHP dev Feb 13 '15 at 07:39
  • post your table structure – Deenadhayalan Manoharan Feb 13 '15 at 08:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70834/discussion-between-paps-k-and-deena). – PHP dev Feb 13 '15 at 08:22