Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Iam having a table sample as given below

Sno	ColA	ColB	ColC	Perc
1	AA	A01	10	12.50
2	AA	A02	25	31.25
3	AA	A03	15	18.75
4	AA	A04	30	37.50
5	AA	AT	80	100.00
6	BB	B01	20	30.77
7	BB	B02	20	30.77
8	BB	B03	15	23.08
9	BB	B04	10	15.38
10	BB	BT	65	100.00


I have to arrive the column Perc.
Perc = ColC*100/(ColC where ColB='AT') where ColA='AA' similarly
Perc = ColC*100/(ColC where ColB='BT') where ColA='BB'

(ie)
Perc=10*100/80=12.50
Perc=25*100/80=31.25

What I have tried:

How to arriver Perc Column by dividing values in the same column
Posted
Updated 23-Jul-18 7:47am
Comments
Richard MacCutchan 21-Jul-18 10:15am    
That makes no sense, you already have the values. Although it is impossible to see how you can calculate them when you are only given a single number.
kirthiga S 23-Jul-18 0:41am    
Its not a single value, its a sum of this group. I have arrived all other values.
Naga Sindhura 25-Jul-18 9:16am    
AT and BT are two fixed vales in ColB? What if you have the following combination? That time will you go for the CT, DT,...? First check other dependencies where you can decide perfect combination like for AA use AT, And, for BB use BT... may be some table to decide this combination at runtime if you don't have fixed combination.
ColA ColB
CC C01
CC C02
CC C03
CC C04

1 solution

Something like this should work:
SQL
SELECT
    A.Sno,
    A.ColA,
    A.ColB,
    A.ColC,
    Round(100. * A.ColC / B.ColC, 2) As Perc
FROM
    YourTable As A
    INNER JOIN YourTable As T
    ON T.ColA = A.ColA
    And Len(T.ColB) = 2
    And T.ColB = STUFF(T.ColA, 2, 1, 'T')
;
Output:
Sno    ColA    ColB    ColC    Perc
-----------------------------------------------
1      AA      A01     10      12.50000000000
2      AA      A02     25      31.25000000000
3      AA      A03     15      18.75000000000
4      AA      A04     30      37.50000000000
5      AA      AT      80      100.00000000000
6      BB      B01     20      30.77000000000
7      BB      B02     20      30.77000000000
8      BB      B03     15      23.08000000000
9      BB      B04     10      15.38000000000
10     BB      BT      65      100.00000000000
 
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