-3

I have a table Transactions, having following columns:

txn_id,
Account_id,
Amount,
txn_id,
txn_type_id

txn_type_id is the column which shows whether the transaction was CDT or DBT, using self join I want to present the records to show the CDT and DBT transactions separately

The code I used is :

select 
    t1.txn_id as DBTAccId, 
    t1.Account_id as DBTAccountid,
    t1.Amount as DBTAmount, 
    t2.txn_id as CDTId, 
    t2.Account_id as CDTAid,
    t2.Amount as CDTAmount,
    t1.txn_type_id
from Transactions t1 , Transactions t2
where t1.txn_id <> t2.txn_id
and t1.txn_type_id = t2.txn_type_id;

But it prints all records for a single txn_id, just like

tx_id    txn_type_id
1           CBT
1           CBT
1           CBT
1           CBT
1           CDT
1           CDT
2           CBT
2           CBT
2           CBT
2           CBT
2           CDT
2           CDT

and i want it to print like:

1           CBT
2           CBT
3           CDT
4           CDT
Zia Ullah
  • 93
  • 1
  • 1
  • 8
  • 3
    Why are you joining the table to itself? I don't understand from your post why you need to do that. The biggest issue though it your question doesn't make much sense because it is lacking details. [Here](http://spaghettidba.com/2015/04/24/how-to-post-a-t-sql-question-on-a-public-forum/) is a great place to start. Also, don't use a comma delimited list of tables in a query. That syntax is archaic. You should use the ANSI-92 style joins, they have been around now for more than 25 years. https://sqlblog.org/2009/10/08/bad-habits-to-kick-using-old-style-joins – Sean Lange Aug 28 '19 at 13:31
  • 2
    Add some sample data for your expected output. – mkRabbani Aug 28 '19 at 13:31
  • I'm learning SQL server inner Joins, while practicing I got this problem so i posted, and i'm new to this site so i get issues posting questions. – Zia Ullah Aug 28 '19 at 13:35
  • What is the logic in the result output? What's the first column? Some ID? – lypskee Aug 28 '19 at 13:43
  • yes, it is the transaction id – Zia Ullah Aug 28 '19 at 13:47
  • So why in row with tx_id = 1 second column equals CDT, while in table above for tx_id = 1 there are rows with CDT and CBT. Whats the logic behind that? – lypskee Aug 28 '19 at 13:56
  • You need to post sample data instead of your wrong output. Posting *just* the wrong output tells us nothing. – Zorkolot Aug 28 '19 at 13:59
  • @lypskee check it now, in simple words i want to show the transactions that show the CBT and CDT transactions separately. – Zia Ullah Aug 28 '19 at 14:07
  • is there a reason not to use PARTITION BY for this problem? http://www.sqltutorial.org/sql-window-functions/sql-partition-by/ – Thomas Aug 28 '19 at 14:19
  • "present the records to show the CDT and DBT transactions separately" & "show the transactions that show the CBT and CDT transactions separately" are unintelligible. Use enough words, sentences & references to parts of examples to clearly & fully say what you mean. When giving a relation(ship)/association or table (base or query result), say what a row in it states about the business situation in terms of its column values. A "in simple words", "basically", etc that doesn't introduce or summarize a clear, precise & full description that you also give just means "unclearly". – philipxy Aug 29 '19 at 04:36
  • Please in code questions give a [mre]--cut & paste & runnable code; example input with desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. That includes the least code you can give that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) Please clarify via edits, not comments. – philipxy Aug 29 '19 at 04:36

2 Answers2

0

You don't want a self-join; you want order by:

select t.*
from Transactions t
order by t.txn_type_id, t.txn_id;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
0

If order by isn't the fix, I get the impression that you might be looking for results in two columns:

with dbt as (
    select *, row_number() over (partition by Account_id order by txn_id) as rn
    from Transactions where tx_type_id = 'DBT'
), cbt as (
    select *, row_number() over (partition by Account_id order by txn_id) as rn
    from Transactions where tx_type_id = 'CBT'
)
select *
from dbt d full outer join cbt c
    on c.Account_id = d.Account_id and c.rn = d.rn;
shawnt00
  • 16,443
  • 3
  • 17
  • 22