Hello,
I found this site very usefull so i wonder if someone with much more experience would be able to help me. I am looking for an easier way to automatize it. I know how to make it but it would take me too long and many tables to build to take every situation that can occur. Iwould take every case each by each in different tables but i have more than 100 cases. In this example i gave only a few just to give me stat on how to make it easier. Any suggestion would be highly appreciated
I have this table:
create table [dbo].[Table](
ID nvarchar(50) null,
Line nvarchar (10) null,
Category nvarchar(100) null,
Unit nvarchar(100) null,
Amount float null
insert into Table values:
('A','96','Any','Comp Serv','100'),
('A','UY','Any','Comp','200'),
('B','96','Any','Comp Serv','10'),
('B','MV','lades','Comp Serv','10'),
('B','S7','lades','Comp','20'),
('E','96','Any','Comp Serv','40'),
('E','MV','lades','Comp','50'),
('E','1V','SDC','Comp','60'),
('E', 'FE','System','Comp Serv','70'),
('E', 'PD','ollo','Comp','50'),
('F','1V','SDC','Comp','10'),
('F', '96','Any','Comp Serv','30'),
('F', 'PD','ollo','Serv','30'),
('F','VM','Software','Stor Serv','90'),
('G','96','Any','Com Serv','90'),
('H','96','Any','Comp Serv','90'),
('H','VM','Software','Stor Serv','30')
this is the table:
An ID can have many Business Lines (column 2). I have to focus on the these Lines: 96. But these lines are mixed with other Business lines per ID.
I need to find a way to rename the "Any" name in Category column with different names but I have to take into consideration different facts:
-
If an Id has only 96 or 96 and UY and no other type of Line, instead of Any I put what I have in Unit column. for example I have ID=A,G
-
If an Id has among 96 only Lines like: MV or S7 then I rename "Any" into "Blades". Ex: ID=B
-
If an Id has among 96 only Lines like: MV, UZ but also 1V,FS or FE then I need to multiply the "Any" row with the number of the other Lines. Example: Id=E
I would need to have for 96 Line the following:
96 lades 20*(10/(10+50+30+40))
96 SDC 20*(50/(10+50+30+40)
96 System 20*(30/(10+50+30+40)
96 ollo 20*(40/(10+50+30+40)
where 20 is the vale of 96 Line, 10 is the value of the first Line divided by the sum of all the lines except 96. The same goes for teh other rows. So at the end i multiplied 96 rows by 4 rows. -
If an ID has among 96, UY, MV,S7,1V,FE,PD other Lines , then I ignore that other line and I implement the same formula as above but only for 96, UY, MV,S7,1V,FE,PD
example ID=F
I ignore VM because is not in my list and i remain with PD and 1V. At the end i would have for 96 Line the following:
96 SDC 10*(40/(40+20))
96 ollo 10*(20/(40+20))
- If I have 96 with any Line except UY, MV,S7,1V,FE,PD then I put Other +Unit
thank you