Link to home
Create AccountLog in
Avatar of efferz
efferz

asked on

Merging two tables via SQL

Hi,

having two tables A and B.

A contains month end observations and looks like:

DATE                     CRIT1            CRIT2      X         Y
12/31/1990            2/15/1991      400        54       40
01/31/1991            3/13/1991      200        52       10
02/28/1991            4/17/1991      300        42        9

B contains daily observations (including the monthly observations form table A) and looks like:
DATE                     CRIT1            CRIT2      X         Y
12/31/1990            2/15/1991      400        54       40
12/31/1990            3/15/1991      400        52       20
01/01/1991            2/15/1991      300        51       30
01/01/1991            2/15/1991      400        50       40
01/04/1991            2/15/1991      300        55       60
01/04/1991            2/15/1991      400        52       70
01/31/1991            2/15/1991      400        51       20
01/31/1991            3/13/1991      200        52       10
02/01/1991            3/13/1991      200        53       12
02/01/1991            3/14/1991      200        54       16
02/05/1991            3/13/1991      300        55       1
02/05/1991            3/13/1991      200        56       2
02/06/1991            3/13/1991      100        53       11
02/28/1991            3/13/1991      200        51       12
02/28/1991            4/17/1991      300        42        9
03/01/1991            4/17/1991      300        44        10
03/02/1991            4/17/1991      200        45        5
04/01/1991            4/17/1991      300        40        3

Now I want to have a query which gives me the daily observations from month end to month end matching the criteria in table A. It should look like

DATE                     CRIT1            CRIT2      X         Y
12/31/1990            2/15/1991      400        54       40
01/01/1991            2/15/1991      400        50       40
01/04/1991            2/15/1991      400        52       70
01/31/1991            2/15/1991      400        51       20
01/31/1991            3/13/1991      200        52       10
02/01/1991            3/13/1991      200        53       12
02/05/1991            3/13/1991      200        56       2
02/28/1991            3/13/1991      200        51       12
02/28/1991            4/17/1991      300        42        9
03/01/1991            4/17/1991      300        44        10

Regards,
Martin
Avatar of Binuth
Binuth
Flag of India image

try this
 

SELECT * FROM <TableB>
WHERE
Date >= (SELECT MIN(Date) FROM <TableA>)
AND Date <= (SELECT MAX(Date) FROM <TableA>)
ORDER BY Date ASC

SELECT * FROM <TableB>
WHERE 
	Date >= (SELECT MIN(Date) FROM <TableA>) 
	AND Date <= (SELECT MAX(Date) FROM <TableA>)
ORDER BY Date ASC

Open in new window

Avatar of efferz
efferz

ASKER

Hi Binuth,

your suggestion does not deliver the result I want. Let´s explain again. I have a set of objects which have static characteristics (the criteria CRTI1 and CRIT2). At month's end one object is selected (table A). I want to know how the selected object evovles during the following month. This information is found in table B which contains all information not only from the selected object.

Regards,
Martin
ASKER CERTIFIED SOLUTION
Avatar of Binuth
Binuth
Flag of India 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 efferz

ASKER

Great. Thanks a lot. Martin