Link to home
Create AccountLog in
Avatar of xav056
xav056

asked on

how do I divide values in two tables MSSQL

How would I get a result table from the two table if the result is the tableA.dur/TableB.dur
the values that should be divided together are those that have a common
'year' field, I should have 13 results equal to the number of rows in the scond table
based on the date the first calculation would be 17.5/17.5
the second would be (17.5+28.5)/(17.5+28.5)
and the third would be   (17.5+28.5)/(17.5+28.5+23.5)
What is the sql query to perform the required calculations?


name          dur      year      date
aaa128      17.50      1      2007-01-01 10:00:00.000
aaa128      28.50      1      2007-01-01 04:00:00.000
aaa128      37.50      1      2007-01-01 18:00:00.000
aaa128      82.50      5      2007-05-10 06:00:00.000
aaa128      52.50      6      2007-06-11 08:00:00.000
aaa128      20.50      8      2007-08-14 12:00:00.000
aaa128      75.00      9      2007-09-15 14:00:00.000

here is the denom table
name           dur      year     date
aaa128      17.50      1      2007-01-01 10:00:00.000
aaa128      28.50      1      2007-01-01 04:00:00.000
aaa128      23.50      1      2007-01-01 08:00:00.000
aaa128      29.50      1      2007-01-01 20:00:00.000
aaa128      33.50      1      2007-01-01 20:00:00.000
aaa128      37.50      1      2007-01-01 18:00:00.000
aaa128      24.50      4      2007-04-08 04:00:00.000
aaa128      82.50      5      2007-05-10 06:00:00.000
aaa128      52.50      6      2007-06-11 08:00:00.000
aaa128      20.50      8      2007-08-14 12:00:00.000
aaa128      75.00      9      2007-09-15 14:00:00.000
aaa128      30.50      10      2007-10-17 16:00:00.000
aaa128      29.50      10      2007-10-17 16:00:00.000


Thank you
Avatar of james-ct16
james-ct16
Flag of Australia image

Howdy

I have had a look at this but I dont fully understand from your description of how the date affects the calculation. Could you please provide a fully worked example for the calculations for year 1 please. I have included the code here to produce sample data to help anyone else.

Kind Regards

James
declare @tableA as table
(
name	varchar(20),
dur		decimal,
[year]	int,
[date]	datetime
)

declare @tableB as table
(
name	varchar(20),
dur		decimal,
[year]	int,
[date]	datetime
)

insert into @tableA values ('aaa128',17.50,1,'2007-01-01 10:00:00.000')
insert into @tableA values ('aaa128',      28.50,      1,      '2007-01-01 04:00:00.000')
insert into @tableA values ('aaa128',      37.50,      1,      '2007-01-01 18:00:00.000')
insert into @tableA values ('aaa128',      82.50,      5,      '2007-05-10 06:00:00.000')
insert into @tableA values ('aaa128',      52.50,      6,      '2007-06-11 08:00:00.000')
insert into @tableA values ('aaa128',      20.50,      8,      '2007-08-14 12:00:00.000')
insert into @tableA values ('aaa128',      75.00,      9,      '2007-09-15 14:00:00.000')

insert into @tableB values ('aaa128',      17.50,      1,      '2007-01-01 10:00:00.000')
insert into @tableB values ('aaa128',      28.50,      1,      '2007-01-01 04:00:00.000')
insert into @tableB values ('aaa128',      23.50,      1,      '2007-01-01 08:00:00.000')
insert into @tableB values ('aaa128',      29.50,      1,      '2007-01-01 20:00:00.000')
insert into @tableB values ('aaa128',      33.50,      1,      '2007-01-01 20:00:00.000')
insert into @tableB values ('aaa128',      37.50,      1,      '2007-01-01 18:00:00.000')
insert into @tableB values ('aaa128',      24.50,      4,      '2007-04-08 04:00:00.000')
insert into @tableB values ('aaa128',      82.50,      5,      '2007-05-10 06:00:00.000')
insert into @tableB values ('aaa128',      52.50,      6,      '2007-06-11 08:00:00.000')
insert into @tableB values ('aaa128',      20.50,      8,      '2007-08-14 12:00:00.000')
insert into @tableB values ('aaa128',      75.00,      9,      '2007-09-15 14:00:00.000')
insert into @tableB values ('aaa128',      30.50,      10,      '2007-10-17 16:00:00.000')
insert into @tableB values ('aaa128',      29.50,      10,      '2007-10-17 16:00:00.000')

Open in new window

Avatar of xav056
xav056

ASKER

Hello,
actually here how it should be
row should be sorted by date asc, year asc
then given the fact that table 2 has more results you take the first row which willl be
aaa128      28.50   1     2007-01-01 04:00:00.000 ( the earliest in time) find a time<= to this time in table A
in this case it would be a matching row in table A
the first calculation would be 28.5/28.5 from (year1 date 2007-01-01 04:00:00.000)(table A and Table B)
the second would be (28.5)/(28.5+23.5) from (year1 date 2007-01-01 08:00:00.000)(num from A denom from B)
third would be   (28.5+17.5)/(28.5+23.5+17.5)   from (year1 date 2007-01-01 10:00:00.000)
4th would be (17.5+28.5+37.5)/(17.5+28.5+23.5+37.5)  from(year 1 date 2007-01-01 18:00:00.000)
5th would be (17.5+28.5+37.5)/(17.5+28.5+23.5+37.5+29.5) from(year 1 date 2007-01-01 20:00:00.000)
6th would be (17.5+28.5+37.5)/(17.5+28.5+23.5+37.5+29.5+33.5) from(year 1 date 2007-01-01 20:00:00.000)
Hope that is clear enough
Good evening

I believe the attached code to be pretty close however there are a few more scenarios i think need looking at.
what happens if a year / date appears in table a but not table b
what happens when there are multiple records for the same date year and name? the attached solution currently ignores this and adds them together.

So the attached solution works by using a pretty standard technique for creating a running total by utilising a cross join back to itself. Note these are the 2 commented queries. Then we simply apply you join logic to the query by using the two running total columns as sub selects.

I hope that helps
declare @tableA as table
(
name	varchar(20),
dur		decimal(10,2),
[year]	int,
[date]	datetime
)

declare @tableB as table
(
name	varchar(20),
dur		decimal(10,2),
[year]	int,
[date]	datetime
)

insert into @tableA values ('aaa128',		17.50,		1,		'2007-01-01 10:00:00.000')
insert into @tableA values ('aaa128',      28.50,      1,      '2007-01-01 04:00:00.000')
insert into @tableA values ('aaa128',      37.50,      1,      '2007-01-01 18:00:00.000')
insert into @tableA values ('aaa128',      82.50,      5,      '2007-05-10 06:00:00.000')
insert into @tableA values ('aaa128',      52.50,      6,      '2007-06-11 08:00:00.000')
insert into @tableA values ('aaa128',      20.50,      8,      '2007-08-14 12:00:00.000')
insert into @tableA values ('aaa128',      75.00,      9,      '2007-09-15 14:00:00.000')

insert into @tableB values ('aaa128',      17.50,      1,      '2007-01-01 10:00:00.000')
insert into @tableB values ('aaa128',      28.50,      1,      '2007-01-01 04:00:00.000')
insert into @tableB values ('aaa128',      23.50,      1,      '2007-01-01 08:00:00.000')
insert into @tableB values ('aaa128',      29.50,      1,      '2007-01-01 20:00:00.000')
insert into @tableB values ('aaa128',      33.50,      1,      '2007-01-01 20:00:00.000')
insert into @tableB values ('aaa128',      37.50,      1,      '2007-01-01 18:00:00.000')
insert into @tableB values ('aaa128',      24.50,      4,      '2007-04-08 04:00:00.000')
insert into @tableB values ('aaa128',      82.50,      5,      '2007-05-10 06:00:00.000')
insert into @tableB values ('aaa128',      52.50,      6,      '2007-06-11 08:00:00.000')
insert into @tableB values ('aaa128',      20.50,      8,      '2007-08-14 12:00:00.000')
insert into @tableB values ('aaa128',      75.00,      9,      '2007-09-15 14:00:00.000')
insert into @tableB values ('aaa128',      30.50,      10,      '2007-10-17 16:00:00.000')
insert into @tableB values ('aaa128',      29.50,      10,      '2007-10-17 16:00:00.000')

---- create running totals for table a
--select a.[YEAR],a.date, cast(SUM(aa.dur) as decimal(10,2)) [RunningTotalA] from @tableA a
--cross join @tableA aa 
--where (a.year = aa.year and aa.date <= a.date)
--group by a.[year], a.date
--order by a.date asc

---- create running totals for table b
--select b.[YEAR],b.date, cast(SUM(bb.dur) as decimal(10,2)) [RunningTotalB] from @tableB b
--cross join @tableB bb 
--where (b.year = bb.year and bb.date <= b.date)
--group by b.[year], b.date
--order by b.date asc


-- put the above together and perform the calculation
select b.year,b.date, isnull(MAX(a.RunningTotalA),0)/ MAX(b.RunningTotalB) [Result] from
(
	select b.[YEAR],b.date, cast(SUM(bb.dur) as decimal(10,2)) [RunningTotalB] from @tableB b
	cross join @tableB bb 
	where (b.year = bb.year and bb.date <= b.date)
	group by b.[year], b.date
) B
left join 
(
	select a.[YEAR],a.date, cast(SUM(aa.dur) as decimal(10,2)) [RunningTotalA] from @tableA a
	cross join @tableA aa 
	where (a.year = aa.year and aa.date <= a.date)
	group by a.[year], a.date
)A on b.year=a.year and a.date<=b.date
group by b.date, b.year
order by b.year,b.date asc

Open in new window

Avatar of xav056

ASKER

Hello james-ct16
what happens if a year / date appears in table a but not table b
--This would never happen as any record in A should be in B

what happens when there are multiple records for the same date year and name?
--if there were multiple records for the same name , year and date then it should be handled so the first record would have its value
the second records would have its value+ the previous value

Thank you
ASKER CERTIFIED SOLUTION
Avatar of james-ct16
james-ct16
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of xav056

ASKER

Thank you