Link to home
Start Free TrialLog in
Avatar of searchsanjaysharma
searchsanjaysharma

asked on

How to find out the percentage for alias name this type of query in sql?

How to find percentage is BCOP-201(A)/BCOP-201(D) as 'P' for all columns
select       Rollno, Name,
      sum(case when scode = 'BCOP-201' then status else 0 end) as 'BCOP-201(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-201') as 'BCOP-201(D)',
      sum(case when scode = 'BCOP-202' then status else 0 end) as 'BCOP-202(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-202') as 'BCOP-202(D)',
      sum(case when scode = 'BCOP-203' then status else 0 end) as 'BCOP-203(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-203') as 'BCOP-203(D)',
      sum(case when scode = 'BCOP-204' then status else 0 end) as 'BCOP-204(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-204') as 'BCOP-204(D)',
      sum(case when scode = 'BCOP-205' then status else 0 end) as 'BCOP-205(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-205') as 'BCOP-205(D)',
      sum(case when scode = 'BCOP-206' then status else 0 end) as 'BCOP-206(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-206') as 'BCOP-206(D)'
      
from msta
where rollno <> 777 and doa>=@tfdate and doa<=@ttdate and section='Nil' and semester='Sem-2'
group by rollno, name
ASKER CERTIFIED SOLUTION
Avatar of appari
appari
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Wrap your query with another SELECT. Put all your calculations for percentages in the other SELECT.

SELECT
Rollno, Name,
100 * 'BCOP-201(A)' / 'BCOP-201(D)' P,
...
FROM (
select       Rollno, Name,
      sum(case when scode = 'BCOP-201' then status else 0 end) as 'BCOP-201(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-201') as 'BCOP-201(D)',
      sum(case when scode = 'BCOP-202' then status else 0 end) as 'BCOP-202(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-202') as 'BCOP-202(D)',
      sum(case when scode = 'BCOP-203' then status else 0 end) as 'BCOP-203(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-203') as 'BCOP-203(D)',
      sum(case when scode = 'BCOP-204' then status else 0 end) as 'BCOP-204(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-204') as 'BCOP-204(D)',
      sum(case when scode = 'BCOP-205' then status else 0 end) as 'BCOP-205(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-205') as 'BCOP-205(D)',
      sum(case when scode = 'BCOP-206' then status else 0 end) as 'BCOP-206(A)',
      (select sum(status) from msta where rollno = 777 and scode = 'BCOP-206') as 'BCOP-206(D)'
     
from msta
where rollno <> 777 and doa>=@tfdate and doa<=@ttdate and section='Nil' and semester='Sem-2'
group by rollno, name
) AA


Regards,
Ian
Avatar of searchsanjaysharma
searchsanjaysharma

ASKER

That works fine,
but in the output it suffixs 1,2,3, etc as P, P1,P2 and so on,
the header like this
Rollno,Name,BCOP-201(A),BCOP-201(D),P,BCOP-202(A),BCOP-202(D),P1,BCOP-203(A),BCOP-203(D),P2,BCOP-204(A),BCOP-204(D),P3      BCOP-205(A),BCOP-205(D),P4,BCOP-206(A),BCOP-206(D),P5

Select
Rollno, Name,
[BCOP-201(A)], [BCOP-201(D)], round(([BCOP-201(A)]*100)/[BCOP-201(D)],2) 'P',
[BCOP-202(A)], [BCOP-202(D)], round(([BCOP-202(A)]*100)/[BCOP-202(D)],2) 'P',  
[BCOP-203(A)], [BCOP-203(D)], round(([BCOP-203(A)]*100)/[BCOP-203(D)],2) 'P',
[BCOP-204(A)], [BCOP-204(D)], round(([BCOP-204(A)]*100)/[BCOP-204(D)],2) 'P',  
[BCOP-205(A)], [BCOP-205(D)], round(([BCOP-205(A)]*100)/[BCOP-205(D)],2) 'P',
[BCOP-206(A)], [BCOP-206(D)], round(([BCOP-206(A)]*100)/[BCOP-206(D)],2) 'P'
From (
select       Rollno, Name,
      sum(case when scode = 'BCOP-201' then status else 0 end) as [BCOP-201(A)],
      (select sum(status) from mstattendance where rollno = 777 and scode = 'BCOP-201') as [BCOP-201(D)],
      sum(case when scode = 'BCOP-202' then status else 0 end) as [BCOP-202(A)],
      (select sum(status) from mstattendance where rollno = 777 and scode = 'BCOP-202') as [BCOP-202(D)],
      sum(case when scode = 'BCOP-203' then status else 0 end) as [BCOP-203(A)],
      (select sum(status) from mstattendance where rollno = 777 and scode = 'BCOP-203') as [BCOP-203(D)],
      sum(case when scode = 'BCOP-204' then status else 0 end) as [BCOP-204(A)],
      (select sum(status) from mstattendance where rollno = 777 and scode = 'BCOP-204') as [BCOP-204(D)],
      sum(case when scode = 'BCOP-205' then status else 0 end) as [BCOP-205(A)],
      (select sum(status) from mstattendance where rollno = 777 and scode = 'BCOP-205') as [BCOP-205(D)],
      sum(case when scode = 'BCOP-206' then status else 0 end) as [BCOP-206(A)],
      (select sum(status) from mstattendance where rollno = 777 and scode = 'BCOP-206') as [BCOP-206(D)]
     
from mstattendance
where rollno <> 777 and doa>=@tfdate and doa<=@ttdate and section='Nil' and semester='Sem-2'
group by rollno, name) A
end