cancel
Showing results for 
Search instead for 
Did you mean: 

Trying to join tables on foreign key

Maryfrances_To1
Confirmed Champ
Confirmed Champ

I've been banging my head long enough, throwing this out to the universe hoping it comes back with a solution. 

Here's the scenario:

AFKS contains Num, LastName, FirstName and ExpDate (among other keywords)

DocTypes contain Num, LastName and FirstName (not ExpDate, except on a small sub-set)

Need a report to pull all documents associated with a Num that has an ExpDate prior to date specified.

I have two queries that get me close, but not close enough.  It appears the date is stored differently in the AFKS than in the KW field and I fear that is interfering with what I'm trying to do.  This query only returns items from the sub-set of doc types that contain the ExpDate KW.

select distinct top 10 	id.itemnum as DocHandle, 	id.itemname as DocName, 	ki103.keyvaluechar as Num,	ki104.keyvaluechar as LastName, 	ki105.keyvaluechar as FirstName, 	ks116.ks113 as ExpDate,	ki113.keyvaluedate as KWExpDate	from hsi.itemdata idright join hsi.keyitem103 ki103		on id.itemnum = ki103.itemnumjoin hsi.keyitem104 ki104	on id.itemnum = ki104.itemnumjoin hsi.keyitem105 ki105	on id.itemnum = ki105.itemnumfull outer join hsi.keyitem113 ki113	on id.itemnum = ki113.itemnumfull outer join hsi.keysetdata116 ks116	on ki113.keyvaluedate = ks116.ks113where id.itemnum = ki103.itemnumand ks116.ks113 < '1995-07-01'

b528d9327bf0471ea25d736b08ddf825

So, then I tried this, but it's not getting the data I'm after either. 

select top 10 	ks116.ks103 as Num, 	ks116.ks104 as LastName,	ks116.ks105 as FirstName,	ks116.ks113 as ExpDate	from hsi.keysetdata116 ks116	UNION	select top 10		ki103.keyvaluechar,		ki104.keyvaluechar,		ki105.keyvaluechar,		ki113.keyvaluedate 	from hsi.itemdata id	join hsi.keyitem103	ki103 on id.itemnum = ki103.itemnum	join hsi.keyitem104 ki104 on id.itemnum = ki104.itemnum	join hsi.keyitem105 ki105 on id.itemnum = ki105.itemnum	left outer join hsi.keyitem113 ki113 on id.itemnum = ki113.itemnum

Even though Top 10 are specified in each select statement, only 19 rows are returned.  Also, the ExpDate (which is supposed to be coming from the AFKS) is NULL in rows where it definitely exists in the autofill set.

3eeee34ecbb8461f929f82ca7429dce0

Obviously, I'm missing something vital.  Any ideas would be GREATLY appreciated!!

1 REPLY 1

Maryfrances_To1
Confirmed Champ
Confirmed Champ

I finally have a solution that gives the desired results:

select distinct top 100		ki103.keyvaluechar as Num,		id.itemnum as DocHandle,		id.itemname as DocType,				ki104.keyvaluechar as LastName,		ki105.keyvaluechar as FirstName,		ks116.ks113 as ExpDate	from hsi.itemdata id	left join hsi.keyitem103 ki103 on id.itemnum = ki103.itemnum	left join hsi.keyitem104 ki104 on ki103.itemnum = ki104.itemnum	left join hsi.keyitem105 ki105 on ki103.itemnum = ki105.itemnum	right outer join hsi.keysetdata116 ks116 on ki103.keyvaluechar = ks116.ks103where ki103.keyvaluechar is not NULL        and  ks116.ks113 < '1995-07-01'