| Author |
Topic |
|
gongxia649
So Suave
344 Posts |
Posted - 2006-05-24 : 20:25:09
|
| 2 tables, TRANSACTIONS and UNPAIREDTRANSACTIONS is defined as:TransactionCode intPairedWith intAmount decimal(19,2)Assume TRANSACTIONS contains the following recordsTransactionCode PairedWith Amount1 2 $102 NULL $203 NULL $504 NULL $305 3 $106 NULL $207 10 $208 NULL $30UNPAIRED is defined as:TransactionCode intLinkedTo intAssume UNPAIRED contains no recordsWrite a T-SQL procedure that will go through each record in the TRANSACTIONS table, and print to screen (using the PRINT function) the TransactionCode, the TransactionCode of its pair (if applicable), and the sum of the pair's amounts. In cases where the Transaction has a PairedWith value, but no matching transaction is found, insert the TransactionCode and its PairedWith code to the UNPAIRED table.Expected Output:In the example above, the correct output would be:1 - 2 - $302 - 1 - $303 - 5 - $604 - NULL - $305 - 3 - $606 - NULL - $208 - NULL - $30and the UNPAIRED table would contain one rowTransactionCode PairedWith7 10 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-24 : 20:44:15
|
Homework assignment ? KH |
 |
|
|
gongxia649
So Suave
344 Posts |
Posted - 2006-05-24 : 20:45:19
|
| no |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-05-24 : 20:51:22
|
| Can you show us the code you have written already, and tell us what problems you are having with it?It would also be helpful if you posted the create table statements for the tables.CODO ERGO SUM |
 |
|
|
gongxia649
So Suave
344 Posts |
Posted - 2006-05-24 : 21:03:50
|
| ok for the insert is this right?: cause i think i should write small queries first then turn it into tsqlinsert into unpaired (transactionCode, linkedTo)select transactionCode, pairedWith from transactions where transactionCode is not nulland pairedWith is not nulland pairedwith > 8 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-24 : 21:15:37
|
the UNPAIRED should be easy to obtain, just apply the condition of NOT EXISTS for TransactionCode & PairedWithfor the PAIRED, you can INNER JOIN back the same table using similar condition as the NOT EXISTS KH |
 |
|
|
gongxia649
So Suave
344 Posts |
Posted - 2006-05-24 : 21:18:07
|
| what i did is wrong ? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-24 : 21:19:12
|
And in future as MVJ suggest try posting your table structure and sample data in CREATE TABLE statements. Most of us are lazy to type and preferred easy way of COPY & PASTE to work on.For me, i use table variable approach-- Table Structuredeclare @transaction table( transactioncode int, pairedwith int, amount int)declare @unpaired table( transactioncode int, linkedto int)-- Sample Datainsert into @transactionselect 1, 2, 10 union allselect 2, NULL, 20 union allselect 3, NULL, 50 union allselect 4, NULL, 30 union allselect 5, 3, 10 union allselect 6, NULL, 20 union allselect 7, 10, 20 union allselect 8, NULL, 30 KH |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-24 : 21:21:31
|
quote: Originally posted by gongxia649 what i did is wrong ?
It's the logic. UNPAIRED is where you can't find the PairedWith record and also no record (PairedWith) is linked to the TransactionCodeselect a.transactioncode, a.pairedwithfrom @transaction a where a.pairedwith is not nulland not exists (select * from @transaction x where x.pairedwith = a.transactioncode or x.transactioncode = a.pairedwith) KH |
 |
|
|
funketekun
Constraint Violating Yak Guru
491 Posts |
Posted - 2006-05-25 : 09:44:59
|
| how do you do the paired? |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-05-25 : 09:57:58
|
[code]-- PAIREDselect trans_code = a.transactioncode, paired_with = coalesce(a.pairedwith, b.transactioncode), amount = (a.amount + isnull(b.amount, 0))from @transaction a left join @transaction b on ( a.pairedwith = b.transactioncode or a.transactioncode = b.pairedwith )where a.pairedwith is nullor exists (select * from @transaction x where x.pairedwith = a.transactioncode or x.transactioncode = a.pairedwith)[/code] KH |
 |
|
|
|