Quantcast
Channel: Help in join with logic
Viewing all articles
Browse latest Browse all 8

Help in join with logic

$
0
0

You might want to consider using a lookup table as a go between to avoid the loss of your indices, if relevant:

IF OBJECT_ID('tempdb..#A') IS NOT NULL DROP TABLE #A
IF OBJECT_ID('tempdb..#B') IS NOT NULL DROP TABLE #B

CREATE TABLE #A (Ename char(10), ED char(4), EE char(4), EG char(4))
INSERT INTO #A (Ename, ED, ee, eg) VALUES
('214129-03','03','CAS','GRP'),
('214130-04','04','PER','GRP'),
('214132-06','06','CAS','GRP')

CREATE INDEX idx_ename ON #A (ename) INCLUDE (ED, EE, EG)

CREATE TABLE #B (Ename1 char(10), ED1 char(4), EE1 char(4), EG1 char(4))
INSERT INTO #B (Ename1, ED1, EE1, EG1) VALUES
('214129','03','CAS','GRP'),
('214130','04','CAS','GRP'),
('214132','06','TAS','GRP')

CREATE INDEX idx_ename1 ON #B (ename1) INCLUDE (ED1, EE1, EG1)

DECLARE @lookupTable TABLE (ename CHAR(10), ename1 CHAR(10))
INSERT INTO @lookupTable (ename, ename1) 
SELECT a.ename, LEFT(a.ename,CHARINDEX('-',a.ename)-1)
  FROM #A a

SELECT *
  FROM #A a
    INNER JOIN @lookupTable lt
	  ON a.ename = lt.ename
	INNER JOIN #B b
	  ON lt.ename1 = b.Ename1


Don't forget to mark helpful posts, and answers. It helps others to find relevant posts to the same question. Enjoyed my post? Hit the up arrow (left)
Really enjoyed it? See my profile!
My Tech Net Articles.


Viewing all articles
Browse latest Browse all 8

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>