I've created main SELECT statement like below. For every row in Base table (base on IDKey I've added columns with values from result of INNER JOIN of few tables.
SELECT
IDKey,
Name,
(select Amount from Table1 INNER JOIN Table2 ... where ID=IDKey) AS AmountTypeX,
(select Amount from Table1 INNER JOIN Table2 ... where ID=IDKey) AS AmountTypeY
FROM BaseTable
BaseTable:
IDKey Name
----------
1 Ian
2 John
3 Ann
...
INNER JOIN of few tables Table1, Table2... :
ID Amount Type
--------------
1 100 X
1 200 Y
2 300 X
2 400 Y
3 500 X
3 600 Y
...
Result looks like:
IDKey Name AmountTypeX AmountTypeY
-------------------------------------
1 Ian 100 200
2 John 300 400
3 Ann 500 600
...
Do you have any suggestion how to avoid this nested SELECT statements in the main query at the begining and made this query simpler and more effective ?
I wast thinking about PIVOTING of the INNER JOIN result to look like below (all data for particular ID in one row) and then JOIN with main query but Have no idea how to do this and is it possible.
ID X Y
-----------
1 100 200
2 300 400
3 500 600
...