Click here to monitor SSC
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Differnet Join Statement


Differnet Join Statement

Author
Message
thava
thava
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 556
Hi guru's
Thanks for your support this is my following situation

DECLARE @A as TABLE(Eid VARCHAR(5),DocNo VARCHAR(5))
DECLARE @B as TABLE(Cid VARCHAR(5),CDocNo VARCHAR(5))

INSERT INTO @A
VALUES
('a','1'), ('a','2'),('a','4'), ('a','6'), ('c','3'), ('b','4'), ('e','5'),
('b','1'), ('b','3'), ('b','6')

INSERT INTO @B
VALUES
('a','1'), ('a','2'), ('b','4')



Now i want the result as follows
Eid cDocNo
a 1
a 2
a NULL
a NULL
b NULL
b NULL
b 4
b NULL

how did i bring this result
if have tried this one but not successfull

SELECT a.Eid, b.cDocNo FROM @A a
LEFT OUTER JOIN @B b ON A.Eid = b.cid AND a.DocNo=b.CDocNo
ORDER BY a.Eid


but result include with the
c NULL
e NULL
any other idea how to get that result



Every rule in a world of bits and bytes, can be bend or eventually be broken
MyBlog About Common dialog control
A Visualizer for viewing SqlCommand object script
s_osborne2
s_osborne2
Right there with Babe
Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)Right there with Babe (745 reputation)

Group: General Forum Members
Points: 745 Visits: 2292
SELECT 
a.Eid,
b.cDocNo
FROM
@A a
LEFT OUTER JOIN @B b ON A.Eid = b.cid AND a.DocNo=b.CDocNo
WHERE
Eid IN (SELECT Cid FROM @B)
ORDER BY
a.Eid



That seems to do the trick, unless i've mis-understood what you're after.

Thanks,

Simon



MCSE: Data Platform
MCSE: Business Intelligence
Follow me on Twitter: @WazzTheBadger
LinkedIn Profile: Simon Osborne
thava
thava
SSC Veteran
SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)SSC Veteran (264 reputation)

Group: General Forum Members
Points: 264 Visits: 556
thanks for your response bro, i tried it works thanks , just make a little bit faster here which will increase the IO opeartion also

SELECT a.Eid, b.cDocNo
FROM @A a
LEFT OUTER JOIN @B b
ON A.Eid = b.cid AND
a.DocNo = b.CDocNo
WHERE EXISTS (SELECT 1 FROM @b c WHERE c.Cid = a.Eid )
ORDER BY
a.Eid


it increases the scan count of the second table 2 and double the logical reads, any other solution bro



Every rule in a world of bits and bytes, can be bend or eventually be broken
MyBlog About Common dialog control
A Visualizer for viewing SqlCommand object script
senor.tamas
senor.tamas
Forum Newbie
Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)Forum Newbie (1 reputation)

Group: General Forum Members
Points: 1 Visits: 1
try this:


DECLARE @A as TABLE(Eid VARCHAR(5),DocNo VARCHAR(5))
DECLARE @B as TABLE(Cid VARCHAR(5),CDocNo VARCHAR(5))
DECLARE @C as TABLE(Did VARCHAR(5),EDocNo VARCHAR(5))

INSERT INTO @A
VALUES
('a','1'), ('a','2'),('a','4'), ('a','6'), ('c','3'), ('b','4'), ('e','5'),
('b','1'), ('b','3'), ('b','6')

INSERT INTO @B
VALUES
('a','1'), ('a','2'), ('b','4')

-- Solution1:
SELECT x.Eid AS Eid
, b.cDocNo AS cDocNo
FROM @B b RIGHT JOIN ( SELECT A.Eid
, A.DocNo
FROM @A a INNER JOIN @B b ON ( A.Eid = b.cid )
GROUP BY A.Eid
, A.DocNo ) x ON ( ( b.Cid = x.Eid )
AND ( b.CDocNo = x.DocNo ))
ORDER BY x.Eid

-- Solution2:
INSERT INTO @C
SELECT A.Eid
, A.DocNo
FROM @A a INNER JOIN @B b ON ( A.Eid = b.cid )
GROUP BY A.Eid
, A.DocNo

SELECT c.Did AS Eid
, b.cDocNo AS cDocNo
FROM @C c LEFT JOIN @B b ON ( ( C.Did = b.cid )
AND ( C.EDocNo = b.CDocNo ))
ORDER BY C.Did
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search