| Author |
Topic |
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-13 : 22:13:00
|
Hi All,we have two tables Table1 and Table2 and both has primary key NUM_ID . I want to select specific columns by running query which returns 3 rows (means NUM_ID are same in 3 rows).TABLE1NUM_ID S_NO COLUMN_1 COLUMN_2 COLUMN_31350 2 A B C1350 1 D E F1120 1 G H I1240 1 J K L1240 2 M N OTABLE2NUM_ID S_NO COLUMN_4 COLUMN_5 COLUMN_61350 2 P Q R1350 1 S T U1120 1 V W X1240 Y Z AA AB1240 2 AC AD AEHere I want to retrive COLUMN_2 FROM TABLE1 AND COLUMN_5 FROM TABLE2WHERE NUM_ID = 1350 AND ORDERBY S_NOI have written following code but it is not giving me my desired tableAny idea what is wrong in this code ?quote: SELECT A.COLUMN_2, B.COLUMN_5 , A.S_NOFROM TABLE1 A, TABLE2 BWHERE (A.NUM_ID = 1350 ) and (B.NUM_ID = 1350) and (A.S_NO = B.S_NO)ORDERBY S_NO
Expected Table should beResultS_NO COLUMN_2 COLUMN_5 1 E T 2 B QThanks,Shahid |
|
|
wormz666
Posting Yak Master
110 Posts |
Posted - 2008-10-13 : 22:25:50
|
| Select a.column_2,b.column_5,a.s_no from table1 a inner join table2 bon a.num_id=b.num_idwhere a.num_id=1350 order by a.s_no |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2008-10-13 : 22:35:13
|
| Why there is duplicate in Primary Key(according to you) NUM_ID? |
 |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-13 : 22:41:57
|
| Yes , NUM_ID is a primary key because 2 rows consist of one iteration for example one claim has two different charge form ( 1 for blood test and second is for x-ray) so claim transaction id is same which is NUM_ID here |
 |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-13 : 23:54:46
|
| Thanks every one for the reply i have one more question how should i handle S_No here ? Because I want correct data should be populated in the row. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 01:09:45
|
quote: Originally posted by shahid09 Yes , NUM_ID is a primary key because 2 rows consist of one iteration for example one claim has two different charge form ( 1 for blood test and second is for x-ray) so claim transaction id is same which is NUM_ID here
I guess primary key should composite including columns NUM_ID and S_No |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 01:10:43
|
quote: Originally posted by shahid09 Thanks every one for the reply i have one more question how should i handle S_No here ? Because I want correct data should be populated in the row.
didnt get that. Arent you getting correct values from solution provided? |
 |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-14 : 10:52:45
|
| No, Actually I am expecting two rows with COLUMN_2 and COLUMN_5 values but i am getting 4 rows.Thanks,Shahid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 11:11:56
|
think this is what you wantSELECT A.COLUMN_2, B.COLUMN_5 , A.S_NOFROM TABLE1 AJOIN TABLE2 BON A.S_NO = B.S_NOAND A.NUM_ID =B.NUM_ID WHERE A.NUM_ID = 1350 ORDER BY S_NO |
 |
|
|
shahid09
Starting Member
35 Posts |
Posted - 2008-10-14 : 11:21:21
|
| Perfect. Thats what i wanted.Thanks alot everyone.Regards,Shahid |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-10-14 : 11:28:21
|
quote: Originally posted by shahid09 Perfect. Thats what i wanted.Thanks alot everyone.Regards,Shahid
welcome |
 |
|
|
|