Click here to Skip to main content
15,896,453 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am a very new to SQL. I am building a DB, but having difficulty determining totals of items in Table C from 2 tables A & B below based on the effective date.
Table A
WTH*	SG	EffectiveDate*
fr	3	01/01/2010
ox	1	01/01/2010
fr	5	05/01/2012
ox	2	05/01/2012


Table B
EQ	WTH	FTR	EffectiveDate
A	fr	30	01/01/2010
A	ox	10	01/01/2010
B	fr	50	05/01/2010
B	ox	40	05/01/2010
A	fr	60	31/12/2011
A	ox	15	31/12/2011
B	fr	35	05/02/2013
B	ox	5	05/02/2013

Table C
EQ	WTH	FTR	SG	TOTAL
A	fr	30	3	90
A	ox	10	1	10
B	fr	50	3	150
B	ox	40	1	40
A	fr	60	3	180
A	ox	15	1	15
B	fr	35	5	175
B	ox	5	2	10


WTH* and EffectiveDate* are the primary keys

What I have tried:

Currently, this is my code;
Select Q2.EQ, Q1.WTH, Q2.FTR Q1.SG, [Q1.SG]*[Q2.FTR] AS Total
FROM Table A Q1 INNER JOIN Table B Q2
ON Q1.WTH = Q2.WTH AND Q1.EffectiveDate >= Q2.EffectiveDate
Posted
Updated 15-May-18 13:24pm

1 solution

This is a join. Do you want to do a union? A union would join the two tables together. A join filters down the data in one table to records that also match the second table
 
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