I have a table tbl_LEDGER
+-----------+--------+-----+
| AccountId | GlCode | Amt |
+-----------+--------+-----+
| LAS00001 | INTRAC | 100 |
| LAS00002 | INTRAC | 150 |
| LAS00001 | INTLAS | 200 |
+-----------+--------+-----+
Desired Result :
+-----------+------------+-----------+
| AccountId | intractamt | intlasAmt |
+-----------+------------+-----------+
| LAS00001 | 100 | 200 |
| LAS00002 | 150 | 0 |
+-----------+------------+-----------+
Here is my working query:
select accountid,sum(amt) intracamt, (select SUM(amt) from tbl_LEDGER
where GLCode='intlas' and AccountID=intrac.AccountID ) intlasamt
from tbl_LEDGER intrac
where GLCode='intrac' group by AccountID order by AccountID
Another Working query :
select a.accountId,a.amt as 'RACAMT',b.amt as 'LACAMT' from
(
select accountid ,glcode, SUM(amt) as amt from nbfcledger where GLCode='intrac' group by GLCode,AccountID
) a
inner join
(
select accountid ,glcode, SUM(amt) as amt from nbfcledger where GLCode='intlas' group by GLCode,AccountID
)b
on a.AccountID = b.AccountID order by AccountID
What are the other ways in which I can achieve the same result? Which one is best and why? I was hoping I could do this without PIVOT.