Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Nemis79
Starting Member
1 Post |
Posted - 2009-10-16 : 08:50:00
|
| Hi,I have two tables.One is called TMSDAT.TRANSMAIN and contains transactions.TMSDAT.TRANSMAIN:TRANSMAIN.TRANSIK - transaction numberTRANSMAIN.NOMVAL - nominal/numberThe other is called TMSDAT.TRANSCOSTTAX and contains costs on the transactions.TMSDAT.TRANSCOSTTAX:TRANSCOSTTAX.TRANSIK - transaction numberTRANSCOSTTAX.COSTIK - cost typeTRANSCOSTTAX.AMOUNTPC - cost amountThe tables are joined on the followingSELECT *FROM TMSDAT.TRANSMAIN, TMSDAT.TRANSCOSTTAXWHERE TRANSMAIN.TRANSIK = TRANSCOSTTAX.TRANSIKA transaction can have several different costs. Each cost type has an own identifier:COMMISSION: TRANSCOSTTAX.COSTIK = 1INTEREST ON INTEREST FUND: TRANSCOSTTAX.COSTIK = 83A transaction can also have null entries for cost.Now what I'm trying to do, is pull the following data from the two tables:Transaction number, nominal/number, cost amount for COSTIK = 1, cost amount for COSTIK = 83Could anyone please help? |
|
|
Kumar_Anil
Yak Posting Veteran
68 Posts |
Posted - 2009-10-16 : 14:24:11
|
| Please try the following & see what happens. DECLARE @TRANSMAIN TABLE(TRANSIK INT, NOMVAL INT )INSERT @TRANSMAINSELECT 1, 1 UNION ALLSELECT 2, 2 UNION ALLSELECT 3, 1 UNION ALLSELECT 4, 4 UNION ALLSELECT 5, 6 UNION ALLSELECT 6, 4DECLARE @TRANSCOSTTAX TABLE(TRANSIK INT,COSTIK INT,AMOUNTPC Money)INSERT @TRANSCOSTTAXSELECT 1, 1, 10 UNION ALLSELECT 2, 2, 20 UNION ALLSELECT 3, 1, 33 UNION ALLSELECT 4, 83, 43 UNION ALLSELECT 5, 6, 55 UNION ALLSELECT 3, 1, 33 UNION ALLSELECT 4, 83, 22 UNION ALLSELECT 5, 6, 12 UNION ALLSELECT 6, 83, 22select a.Transik, a.Nomval, b.AmountPC, b.COSTIKfrom @Transmain a join @TRANSCOSTTAX b on a.TRANSIK = b.TRANSIKwhere b.COSTIK in (1, 83) |
 |
|
|
|
|
|
|
|