Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
When I calculate percentage it only shows Location Wise Percent .But i want Regional total & percent in view.
else i have to make different query Region Wise & Location Wise..
    I have Data like below in SQL    
Region	Loc     Qty	Cap	%
South	CHN	10	15	66.66666667
South	BLR	20	25	80
South	COK	30	35	85.71428571
North	CHD	10	15	66.66666667
North	PUN	20	25	80
North	DER	40	65	61.53846154


 Desired Output  like below
Region	Loc     Qty	Cap	%</small>
South	CHN	10	15	66.66666667
South	BLR	20	25	80
South	COK	30	35	85.71428571
South Total		60	75	80
North	CHD	10	15	66.66666667
North	PUN	20	25	80
North	DER	40	65	61.53846154
North Total	70	105	66.66666667


What I have tried:

<pre lang="SQL">Select LocCode , (Qty/Capacity)*100 fro my Table group by LocCode 

SQL
Select Region , (Qty/Capacity)*100 fro my Table group by Region 


But i am only able achieve this in two different query Region wise % and Location Wise % . Is their a way to get in single query 

Loc     Qty	Cap	%
CHN	10	15	66.66666667
BLR	20	25	80
COK	30	35	85.71428571
CHD	10	15	66.66666667
PUN	20	25	80
DER	40	65	61.53846154
    ------------------
Region	Qty	Cap	%
South	60	75	80
North	70	105	66
Posted
Updated 24-Jul-18 2:07am
v2
Comments
ZurdoDev 24-Jul-18 7:54am    
If you can do one percent why can't you do another? I do not understand where you are stuck.

1 solution

If I understand your question correctly, you want just to put the results together in a single query. For that you can use UNION ALL. Something like
SQL
Select 'Locations', LocCode, (Qty/Capacity)*100 
from myTable 
group by 'Locations', LocCode 
UNION ALL
Select 'Regions', Region,  (Qty/Capacity)*100 
from myTable 
group by 'Regions', Region
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900