Open fjtello opened 1 year ago
declare @t as table (id int identity(1,1), vehiculo varchar(50), tipovehiculo varchar(25), ocupacion int);
insert into @t (vehiculo, tipovehiculo, ocupacion) values ('A1', 'A', 5), ('A2', 'A', 4), ('A3', 'A', 3), ('A4', 'A', 9), ('A5', 'A', 15); insert into @t (vehiculo, tipovehiculo, ocupacion) values ('B1', 'B', 4), ('B2', 'B', 6), ('B3', 'B', 7), ('B4', 'B', 2), ('B5', 'B', 25), ('B6', 'B', 15); insert into @t (vehiculo, tipovehiculo, ocupacion) values ('C1', 'C', 9), ('C2', 'C', 4), ('C3', 'C', 6), ('C4', 'C', 1); insert into @t (vehiculo, tipovehiculo, ocupacion) values ('B7', 'B', 17), ('B8', 'B', 12), ('A7', 'A', 7), ('A6', 'A', 10);
; with a as ( select tipovehiculo, count() numrows, sum(ocupacion) ocupacion, sum(sum(ocupacion)) over() grandtotal from @t group by tipovehiculo ) select tipovehiculo, numrows, ocupacion, grandtotal, ocupacion 100.00 / grandtotal porc from a
declare @t as table (id int identity(1,1), vehiculo varchar(50), tipovehiculo varchar(25), ocupacion int);
insert into @t (vehiculo, tipovehiculo, ocupacion) values ('A1', 'A', 5), ('A2', 'A', 4), ('A3', 'A', 3), ('A4', 'A', 9), ('A5', 'A', 15); insert into @t (vehiculo, tipovehiculo, ocupacion) values ('B1', 'B', 4), ('B2', 'B', 6), ('B3', 'B', 7), ('B4', 'B', 2), ('B5', 'B', 25), ('B6', 'B', 15); insert into @t (vehiculo, tipovehiculo, ocupacion) values ('C1', 'C', 9), ('C2', 'C', 4), ('C3', 'C', 6), ('C4', 'C', 1); insert into @t (vehiculo, tipovehiculo, ocupacion) values ('B7', 'B', 17), ('B8', 'B', 12), ('A7', 'A', 7), ('A6', 'A', 10);
; with a as ( select tipovehiculo, count() numrows, sum(ocupacion) ocupacion, sum(sum(ocupacion)) over() grandtotal from @t group by tipovehiculo ) select tipovehiculo, numrows, ocupacion, grandtotal, ocupacion 100.00 / grandtotal porc from a