0

I m trying to get the Status percentage by different status wise and we have 4 types of status. Here is the data for Columns below.

MR_ID Sup_ID    Status

1   1   Rejected    
1   1   Accepted    
1   1   Accepted    
1   1   Rejected    
2   2   InProgress  
2   2   InProgress  
2   2   Accepted    
2   2   Fordwarded  

Like for MR_ID:1 and Sup_ID:1 combination the % of "Accepted" is 50% and % of "Rejected" is 50%

Need to write a sql query or Stored Procedure to find it out in sql server

Not getting any clue.

4
  • 2
    Specify the expected result as well. And show us your current query attempt. Commented Jun 17, 2019 at 9:35
  • 2
    Are you using SQL Server or Oracle? Commented Jun 17, 2019 at 9:37
  • 1
    The clue is "conditional aggregation". Commented Jun 17, 2019 at 9:43
  • They're clearly using T-SQL in Oracle SQLDeveloper @jarhl. :/ Commented Jun 17, 2019 at 9:46

5 Answers 5

1

You are looking for a simple group by statement:

SELECT 
    MR_ID,
    Sup_ID,
    (CAST(SUM(CASE WHEN [Status] ='InProgress' THEN 1 ELSE 0 END) AS FLOAT) / CAST(COUNT(*) AS FLOAT)) * 100 'InProgress',
    (CAST(SUM(CASE WHEN [Status] ='Fordwarded' THEN 1 ELSE 0 END) AS FLOAT) / CAST(COUNT(*) AS FLOAT)) * 100 'Fordwarded',
    (CAST(SUM(CASE WHEN [Status] ='Accepted' THEN 1 ELSE 0 END) AS FLOAT) / CAST(COUNT(*) AS FLOAT)) * 100 'Accepted',
    (CAST(SUM(CASE WHEN [Status] ='Rejected' THEN 1 ELSE 0 END) AS FLOAT) / CAST(COUNT(*) AS FLOAT)) * 100 'Rejected'
FROM 
    @Table1 t
GROUP BY 
    MR_ID,
    Sup_ID

Result:

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

0

The following query should do what you want, the solution is for SQL server:

CREATE TABLE #temp (MR_ID INT, Sup_ID INT, [Status] VARCHAR(20))

INSERT INTO #temp VALUES
(1,1,'Rejected'),
(1,1,'Accepted'), 
(1,1,'Accepted'), 
(1,1,'Rejected'), 
(2,2,'InProgress'), 
(2,2,'InProgress'),
(2,2,'Accepted'),
(2,2,'Fordwarded') 

SELECT t.MR_ID, t.Sup_ID, t.[Status], (COUNT(t.[Status]) * 100 / (SELECT COUNT(*) FROM #temp WHERE MR_ID = t.MR_ID AND Sup_ID = t.Sup_ID )) AS [Percentage]
FROM #temp t
GROUP BY MR_ID, Sup_ID, [Status]

The result is as below,

MR_ID   Sup_ID  Status      Percentage
1       1       Accepted    50
1       1       Rejected    50
2       2       Accepted    25
2       2       Fordwarded  25
2       2       InProgress  50

Comments

0

You could try this:

CREATE TABLE TMP_PERC(
FIELD1 INT NOT NULL
, FIELD2 INT NOT NULL
, DESCR VARCHAR (100) NOT NULL
)


INSERT INTO TMP_PERC VALUES(1, 1, 'Rejected');
INSERT INTO TMP_PERC VALUES(1, 1, 'Accepted');
INSERT INTO TMP_PERC VALUES(1, 1, 'Accepted');
INSERT INTO TMP_PERC VALUES(1, 1, 'Rejected');
INSERT INTO TMP_PERC VALUES(2, 2, 'InProgress');
INSERT INTO TMP_PERC VALUES(2, 2, 'InProgress');
INSERT INTO TMP_PERC VALUES(2, 2, 'Accepted');
INSERT INTO TMP_PERC VALUES(2, 2, 'Fordwarded');

SELECT FD.FIELD1
     , FD.FIELD2
     , FD.DESCR
     , CAST(FD.TOTAL_DESCR AS FLOAT) / CAST(FC.TOTAL  AS FLOAT) * 100 AS PERC
FROM
(
SELECT FIELD1
     , FIELD2
     , DESCR
     , COUNT(*) AS TOTAL_DESCR   
FROM TMP_PERC TP_DECSCR
GROUP BY FIELD1
       , FIELD2
       , DESCR
) FD
INNER JOIN
(

SELECT FIELD1
     , FIELD2
     , COUNT(*) AS TOTAL
FROM TMP_PERC
GROUP BY FIELD1
       , FIELD2
) FC             
ON FD.FIELD1 = FC.FIELD1
AND FD.FIELD2 = FC.FIELD2
;       

Comments

0

I would simply do:

select mr_id, sup_id,
       avg(case when status = 'Accepted' then 1.0 else 0 end) as ratio_accepted,
       avg(case when status = 'Rejected' then 1.0 else 0 end) as ratio_rejected,
       avg(case when status = 'InProgress' then 1.0 else 0 end) as ratio_inprogress,
       avg(case when status = 'Forwarded' then 1.0 else 0 end) as ratio_forwarded
from t
group by mr_id, sup_id;

If you want the values in separate rows rather than columns, use window functions:

select mr_id, sup_id, status,
       count(*) * 1.0 / sum(count(*)) over (partition by mr_id, sub_id) as ratio
from t
group by mr_id, sup_id, status;

These queries are standard SQL and should work in both SQL Server and Oracle.

Comments

0

msql way, don`t kown did it help

SELECT
    MR_ID mrId,
    `Status` `Status`,
    count(DISTINCT `Status`) / count(*) * 100 per
FROM
    xxx
GROUP BY
    mrId, `Status`[enter image description here][1]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.