SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


help with joins


help with joins

Author
Message
ssurekha2000
ssurekha2000
SSC-Enthusiastic
SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)

Group: General Forum Members
Points: 145 Visits: 207
i have 2 table with 1 to many relation


table1
hid ,refid cid
500 4 1200
501 4 1200


table2

id description refid cid type
500 de 0 1200 H
501 de 0 1200 H
502 desc1 4 1200 D
503 desc2 4 1200 D
504 desc5 4 1200 D
506 desc8 4 1200 D

1001 desc120 3 1200 D
1002 desc121 3 1200 D

and so on

i have a query with table2 joined with some other tables for which i get the output as

id description refid cid type
502 desc1 4 1200 D
503 desc2 4 1200 D
504 desc5 4 1200 D
506 desc8 4 1200 D


i need to join table1 such that

i get the output as





id description
500 de
501 de
502 desc1
503 desc2
504 desc5
506 desc8

i have the below
select ID, tenderdesc from table2 where cid=1200
and refid in(select refid from table1 where cid=1200 )
or
id in (select hid from table1 where cid=1200)
when executed individually i get the result i need
but when joined with the existing query i dont get the needed output
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27799 Visits: 13268
You probably need a full outer join.

Also check the first link in my signature on how to ask questions on forums.
It will make life easier for people trying to help you.


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
ssurekha2000
ssurekha2000
SSC-Enthusiastic
SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)

Group: General Forum Members
Points: 145 Visits: 207
i tried with full outer join but not getting the result
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27799 Visits: 13268
Can you post the query you tried?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
ssurekha2000
ssurekha2000
SSC-Enthusiastic
SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)SSC-Enthusiastic (145 reputation)

Group: General Forum Members
Points: 145 Visits: 207

SELECT
''As RefNo,
'' AS ERPItmDesc,
'' as TDesc,
'' as Item Description,
'' as Date,
0 as ID

FROM TBLDETAIL FM INNER JOIN TBLHDRDETAIL MD
ON MD.CID=FM.CID AND MD.ID=FM.MRPID LEFT OUTER JOIN TBLHDR MH
ON MD.BRNID=MH.BRNID AND MD.CID=MH.CID AND MD.HDRID=MH.ID AND MH.ITMID=FM.ITEMID
FULL OUTER JOIN TBLDESCRIPTION st ON mh.id=st.HID
WHERE
FM.CID=1200 AND FM.YMONTH=201207 AND FM.MONTHSECTION=1


UNION
(SELECT

TENDERREFNO as RefNo,
I.ITNAME AS ERPItmDesc,
MH.TDESC as TDesc,
MH.Item Description,
FM.Date,
MH.ID
FROM TBLDETAIL FM INNER JOIN TBLHDRDETAIL MD
ON MD.CID=FM.CID AND MD.ID=FM.MRPID LEFT OUTER JOIN TBLHDR MH
ON MD.CID=MH.CID AND MD.HDRID=MH.ID AND MH.ITMID=FM.ITEMID
LEFT OUTER JOIN STITMMAS I
ON MH.ITMID=I.ITMID LEFT OUTER JOIN TBLMAS U
ON I.ITMUNITID=U.UNITID
FULL OUTER JOIN TBLDESCRIPTION st ON mh.id=st.HID
WHERE FM.CID=1200 AND FM.YMONTH=201207 AND FM.MONTHSECTION=1
)



this is the query i am having currently

need to modify it to get the result
SELECT mh.ID, mh.tdesc
FROM TBLHDR mh
FULL OUTER JOIN TBLDESCRIPTION st ON mh.id=st.HID
where mh.cid=1200
Koen Verbeeck
Koen Verbeeck
One Orange Chip
One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)One Orange Chip (27K reputation)

Group: General Forum Members
Points: 27799 Visits: 13268
Regarding the desired output that you gave in your original question:
you can get that from the second table only, so why would you join at all?


How to post forum questions.
Need an answer? No, you need a question.
What’s the deal with Excel & SSIS?
My blog at SQLKover.

MCSE Business Intelligence - Microsoft Data Platform MVP
Snigdha Vartak
Snigdha Vartak
Mr or Mrs. 500
Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)Mr or Mrs. 500 (570 reputation)

Group: General Forum Members
Points: 570 Visits: 247
Hi,

What are you trying to achieve?

Do you need only records from Table 2 those exist in Table 1?

Please clarify.
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