Duplicate data when working with multiple tables. Clarification needed - sql

I have a very simple table as shown when query returning clean data select * from Income where symbol = 'AAPL' and statementitem = 'Revenues' and periodtype="Annual";
--
When I attempt to put together a report, I get 0 from someplace. How can I remove the 0 to get a clean output? Where do these 0s come from?
DROP TABLE IF EXISTS _vars;
CREATE TABLE _vars(symbol);
INSERT INTO _vars(symbol) VALUES ('AAPL');
SELECT distinct a.yearmonth, a.symbol, periodtype,
case when a.statementitem = 'Revenues' AND a.periodtype = 'Annual' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end Revenue,
case when a.statementitem = 'Gross Profit' AND a.periodtype = 'Annual' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end GrossProfit,
case when a.statementitem = 'Selling General & Admin Expenses' AND a.periodtype = 'Annual' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end GrossProfit
FROM _vars
INNER JOIN Income a ON a.symbol = _vars.symbol and a.periodtype = 'Annual'

Your 1st query contains 3 conditions:
WHERE symbol = 'AAPL' AND statementitem = 'Revenues' AND periodtype = 'Annual'
and the result is 10 rows.
Your 2nd query joins _vars to the table and since _vars contains only 1 row with the column symbol having value 'AAPL' and you also apply the condition:
and a.periodtype = 'Annual'
in the ON clause, you are covering only 2 of the conditions of the 1st query.
You do not apply the condition:
statementitem = 'Revenues'
you are just checking it in:
case when a.statementitem = 'Revenues' AND .....
Checking a condition does not apply it.
This means that you get more rows in the results that are coming from all the rows that do not have 'Revenues' in the column statementitem and the CASE expression returns 0 for these rows in the column Revenue.
Edit:
You should use conditional aggregation:
SELECT a.yearmonth, a.symbol, a.periodtype,
COALESCE(MAX(CASE WHEN a.statementitem = 'Revenues' THEN a.value END), 0) Revenue,
COALESCE(MAX(CASE WHEN a.statementitem = 'Gross Profit' THEN a.value END), 0) GrossProfit,
COALESCE(MAX(CASE WHEN a.statementitem = 'Selling General & Admin Expenses' THEN a.value END), 0) [Selling General & Admin Expenses]
FROM _vars INNER JOIN Income a
ON a.symbol = _vars.symbol AND a.periodtype = 'Annual'
GROUP BY a.yearmonth, a.symbol, a.periodtype

You are getting 0 because of the below case statement. If your value is null and statementitem is not 'Revenues' and periodtype is not 'Annual', you are a making it as 0
case when a.statementitem = 'Revenues' AND a.periodtype = 'Annual'
then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0
You can get rid of these if you don't want 0's in your result.
This will give you without 0's but with nulls,
SELECT distinct a.yearmonth, a.symbol, periodtype,
a.value as Revenue
FROM _vars
inner JOIN Income a ON a.symbol = _vars.symbol
where a.statementitem = 'Revenues' and a.periodtype = 'Annual'
This query will give you no nulls,
SELECT distinct a.yearmonth, a.symbol, periodtype,
a.value as Revenue
FROM _vars
inner JOIN Income a ON a.symbol = _vars.symbol
where a.statementitem = 'Revenues' and a.periodtype = 'Annual'
and (a.value is null or a.value = '')
Without Inner join,
SELECT distinct a.yearmonth, a.symbol, periodtype,
a.value as Revenue
from Income a
where a.statementitem = 'Revenues' and a.periodtype = 'Annual'
and (a.value is null or a.value = '')
and a.symbol in ('AAPL');

these duplicate data are because of records that are (AAPL,Annual) but are not (Revenues).you should move the condition of statementitem in where cluse or in join condition:
SELECT distinct a.yearmonth, a.symbol, periodtype,isnull(a.value,0) as Revenue
FROM _vars
inner JOIN Income a ON a.symbol = _vars.symbol
where a.statementitem = 'Revenues' and a.periodtype = 'Annual'
or
SELECT distinct a.yearmonth, a.symbol, periodtype,isnull(a.value,0) as revenue
FROM _vars
inner JOIN Income a ON a.symbol = _vars.symbol
and a.statementitem = 'Revenues' and a.periodtype = 'Annual'

Please use below query,
With Join
SELECT distinct a.yearmonth, a.symbol, periodtype,
case when a.statementitem = 'Revenues' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end Revenue,
case when a.statementitem = 'Gross Profit' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end GrossProfit,
case when a.statementitem = 'Selling General & Admin Expenses' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end GrossProfit
FROM _vars
INNER JOIN Income a ON a.symbol = _vars.symbol
where a.periodtype = 'Annual'
Without Join
SELECT distinct a.yearmonth, a.symbol, periodtype,
case when a.statementitem = 'Revenues' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end Revenue,
case when a.statementitem = 'Gross Profit' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end GrossProfit,
case when a.statementitem = 'Selling General & Admin Expenses' then (CASE WHEN (a.value is null or a.value = '') then 0 ELSE a.value END) else 0 end GrossProfit
FROM Income a
where a.periodtype = 'Annual'
and a.symbol in ('AAPL');

Related

Selecting the union part of an sql query

I have this query
SELECT
L.Account,
L.PaidOffDate
FROM
MARS.dbo.vw_Loans L
WHERE
L.isActive = 'False'
AND L.LoanStatus NOT LIKE '%REO%'
AND L.LoanStatus <> 'Trailing Claims'
)
UNION
-- loan numbers for REO sold
(
SELECT
L.Account,
R.SoldDate
FROM
MARS.dbo.vw_REO R
LEFT JOIN
MARS.dbo.vw_Loans L ON L.ConvertedToPropertyID = R.PropertyID
WHERE
R.Active = 0
AND R.Status = 'Sold'
AND L.isActive = 'False'
AND L.LoanStatus LIKE '%REO%'
)
I put this as a common table expression, how do I select R.SoldDate in that case?
Specifically I am trying to run this
CASE
WHEN Inactives.PaidOffDate IS NOT NULL
AND Inactives.PaidOffDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, #reportingDate), -1) AND #reportingDate
THEN Inactives.PaidOffDate
ELSE
CASE
WHEN Inactives.SoldDate IS NOT NULL
AND Inactives.SoldDate BETWEEN DATEADD(MONTH, DATEDIFF(MONTH, 0, #reportingDate), -1) AND #reportingDate
THEN Inactives.SoldDate
ELSE ''
END
END AS [CHECKING PIF DATE]
But I cannot select Inactives.SoldDate
I think what you are after here is a computed column, something like this:
SELECT
L.Account,
L.PaidOffDate,
'Payoff Date' AS Type
FROM MARS.dbo.vw_Loans L
WHERE
L.isActive = 'False' AND
L.LoanStatus NOT LIKE '%REO%' AND
L.LoanStatus <> 'Trailing Claims'
UNION ALL
SELECT
L.Account,
R.SoldDate,
'Sale Date'
FROM MARS.dbo.vw_REO R
LEFT JOIN MARS.dbo.vw_Loans L
ON L.ConvertedToPropertyID = R.PropertyID
WHERE
R.Active = 0 AND
R.Status = 'Sold' AND
L.isActive = 'False' AND
L.LoanStatus LIKE '%REO%';
The computed column Type keeps track of the origin of each record, so that you will know this after the union has been peformed. Note that I switched to UNION ALL, assuming that you always wanted to retain all records.
You need to put the values into separate columns rather than separate *rows. I think the logic you want is conditional logic:
SELECT L.Account,
(CASE WHEN L.LoanStatus NOT LIKE '%REO%' AND L.LoanStatus <> 'Trailing Claims'
THEN L.PaidOffDate
END) as PaidOffDate
(CASE WHEN L.LoanStatus LIKE '%REO%'
THEN R.SoldDate
END) as R.SoldDate
FROM MARS.dbo.vw_Loans L LEFT JOIN
MARS.dbo.vw_REO R
ON L.ConvertedToPropertyID = R.PropertyID AND
R.Active = 0 AND R.Status = 'Sold'
WHERE L.isActive = 'False';
The other possibility is that you actually want two separate CTEs, one for each part of the query.

Incorrect results using parameter in SSRS

I have an SSRS report for which I created two datasets for. One for the actual report and one for the parameter.
Dataset 1 is the report
Dataset 2 simply pulls the parameter that I want to report on from the table where it is stored.
When I run the report and select the parameter, it shows me all the values, but when I select one, I still get every record rather than just the records based on the parameter.
Dataset 1
SELECT stk.ItemCode AS ItemCode, stk.warehouse AS warehouse,
stk.Description AS Description, stk.Assortment AS Assortment,
Items.Class_02, stk.ItemGroup AS ItemGroup, stk.ItemStatus AS ItemStatus,
stk.ItemUnit AS ItemUnit, ISNULL((SELECT TOP 1 dbo.bacoSalesPrice(p.ID,
0) FROM staffl p WHERE ({d '2019-05-27'} BETWEEN validfrom AND
ISNULL(validto,{d '9999-12-31'}))
AND prijslijst = 'SALESPRICE' AND Items.ItemCode = artcode ORDER BY
artcode, validfrom desc),0) AS SalesPackagePrice,
ROUND((stk.Stock/stk.SalesPkg),2) AS Stock, ROUND((stk.Stock +
stk.QtyToBeReceived - stk.QtyToBeDelivered)/stk.SalesPkg,2) AS
AvailableStock, stk.SearchCode AS SearchCode FROM Items INNER JOIN (
SELECT
v.magcode AS warehouse,
MAX(i.itemcode) AS ItemCode,
MAX(i.description_0) AS Description,
i.SearchCode AS SearchCode,
MAX(ia.Assortment) AS Assortment,
MAX(ia.Description_0) AS ItemGroup,
MAX(CASE
WHEN i.condition = 'A' THEN 'Active'
WHEN i.condition = 'B' THEN 'Blocked'
WHEN i.condition = 'D' THEN 'Discontinued'
WHEN i.condition = 'E' THEN 'Inactive'
WHEN i.condition = 'F' THEN 'Future'
END) AS ItemStatus,
ISNULL(MAX(a.purchasepackage), '-') AS ItemUnit,
MAX(i.SalesPackagePrice) AS SalesPackagePrice,
MAX(v.bestniv) AS Minimum,
MAX(v.maxvrd) AS Maximum,
MAX(i.lev_crdnr) AS Supplier,
MAX(a.DeliveryTimeInDays) AS DeliveryTime,
MAX(ISNULL(a.SlsPkgsPerPurPkg,1)) AS SalesPkg,
ISNULL(Actual.Quantity,0) AS Stock
,ISNULL(SUM(QtyToReceived),0) AS QtyToBeReceived
,ISNULL(SUM(QtyToBeDelivered),0) AS QtyToBeDelivered
FROM items i
JOIN voorrd v ON i.itemcode=v.artcode
LEFT JOIN itemaccounts a ON i.itemcode=a.itemcode and i.lev_crdnr=a.crdnr
AND i.itemcode IS NOT NULL AND a.itemcode IS NOT NULL AND i.lev_crdnr IS N
NOT NULL AND a.crdnr IS NOT NULL
JOIN ItemAssortment ia ON ia.Assortment = i.Assortment
LEFT OUTER JOIN
dbo.TempToBeReceivedDelivered#3E5BBBE6#FB7B#4309#9CE1#560885B9BF94# budget
ON v.magcode = budget.warehouse AND i.ItemCode = budget.artcode
AND v.magcode IS NOT NULL AND budget.warehouse IS NOT NULL AND i.ItemCode IS NOT NULL AND budget.artcode IS NOT NULL
LEFT OUTER JOIN
(SELECT GX.artcode, GX.warehouse,
GX.Quantity,
GX.AmtActualStock, GX.TotalCnt,
CASE WHEN GX.FreeQuantity > GX.CurrentQuantity THEN (CASE WHEN GX.CurrentQuantity < 0 THEN 0 ELSE GX.CurrentQuantity END) ELSE
(CASE WHEN GX.FreeQuantity < 0 THEN 0 ELSE GX.FreeQuantity END) END As FreeQuantity
FROM (
SELECT sb.ItemCode AS artcode, SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.Quantity END) as Quantity,
ROUND(SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.StockAmount END),2) as AmtActualStock,
SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.Quantity END) as CurrentQuantity,
SUM(CASE WHEN sb.Date <= {d '2019-05-27'} THEN sb.FreeStock END) AS FreeQuantity, sb.Warehouse,
SUM(sb.GbkmutCount) AS TotalCnt
FROM StockBalances sb WITH (NOLOCK) JOIN Items ON sb.ItemCode = Items.ItemCode JOIN ItemAssortment ON Items.Assortment = ItemAssortment.Assortment
--WHERE Items.Class_02 in #Customer
GROUP BY sb.ItemCode, sb.WareHouse, Items.Class_02
HAVING SUM(sb.GbkmutCount) > 0) GX) AS Actual
ON Actual.artcode = i.ItemCode AND Actual.warehouse = v.magcode AND Actual.artcode IS NOT NULL AND i.ItemCode IS NOT NULL AND Actual.warehouse IS NOT NULL AND v.magcode IS NOT NULL
WHERE i.type IN ('S', 'B')
AND i.ItemCode BETWEEN '0030122186' AND 'XS45000'
AND i.Condition IN ('A')
AND ((i.IsSalesItem <> 0)) AND i.Type IN ('S', 'B')
AND 1=1
GROUP BY v.magcode , i.itemcode, i.SearchCode, Actual.Quantity
) stk ON items.ItemCode = stk.ItemCode
INNER JOIN grtbk ON Items.GLAccountDistribution = grtbk.reknr
ORDER BY stk.ItemCode
Dataset 2
SELECT Class_02, ItemCode FROM Items
Parameter in SSRS is set to Get values from Query, Value field: Class_02
Label field: Class_02

SQL Server - 2008 : Nested Select, need improve execution faster?

Here the problem:
First I have a nested query, for which I need to improve the execution time and make it faster. Imagine 47 seconds at first time and second time was 46 / 45 second. Second was running by cache (the second execution always faster than first time but not this).
Second that query uses SUM, CAST, CASE in each sub select. Can't dodge that
Third I read some another solution from internet using OPTION (FAST 500).
Here the example :
SELECT [cutomerSection],
SUM(DEBIT) AS DEBIT,
SUM(CREDIT) AS CREDIT,
SUM([OTHERS]) AS 'OTHERS'
FROM
(-- SECTOR A
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN j.paymentStat = 'DEBIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN j.paymentStat = 'CREDIT' THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (j.paymentStat = ''
OR j.paymentStat IS NULL) THEN SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*d.adm/100, 0) AS INT)) + SUM(CAST(ISNULL(isnull(qty, 1)*(100-disc)*(d.fee-d.adm)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbactDetail d WITH(NOLOCK)
LEFT JOIN tbsectorSection j WITH(NOLOCK) ON d.tranCode = j.tranCode
LEFT JOIN tbact t ON (d.actCode=t.actCode
AND t.sectorCode = j.sectorCode)
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
LEFT JOIN tbpaymentList cb WITH(NOLOCK) ON cb.paymentK = a.paymentK
LEFT JOIN tbleading dc ON dc.leadCode = j.leadCode
LEFT JOIN tbsector p ON j.sectorCode = p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted = 0)
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
j.paymentStat
UNION ALL -- SECTOR PARK
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*d.qty*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbparkDetail d
LEFT JOIN tbTranPark t ON d.tranCode = t.tranCode
JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo=j.struk
JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (j.deleted IS NULL
OR j.deleted=0)
AND (t.strukPoli IS NOT NULL
OR t.strukPoli <> ''
OR t.strukPoli <> '--')
AND a.status = 'FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat
UNION ALL -- SECTOR FRONT
SELECT cb.paymentGrp AS 'cutomerSection',
CASE
WHEN t.paymentStat = 'DEBIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'DEBIT',
CASE
WHEN t.paymentStat = 'CREDIT' THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'CREDIT',
CASE
WHEN (t.paymentStat = ''
OR t.paymentStat IS NULL) THEN SUM(CAST(isnull(d.rate*(100-disc)/100, 0) AS INT))
ELSE 0
END AS 'OTHERS'
FROM tbDetailRadioterapi d
LEFT JOIN tbtranFront t ON d.tranCode = t.tranCode
LEFT JOIN tbsectorSection j ON t.strukPoli = j.struk
LEFT JOIN tbCustomer c WITH(NOLOCK) ON c.custCode=j.custCode
LEFT JOIN tbfrontDesk a WITH(NOLOCK) ON a.struckNo = j.struk
LEFT JOIN tbpaymentList cb ON a.paymentK = cb.paymentK
LEFT JOIN tbleading dc ON dc.leadCode=j.leadCode
LEFT JOIN tbsector p ON j.sectorCode=p.sectorCode
WHERE (t.deleted IS NULL
OR t.deleted=0)
AND (strukPoli IS NOT NULL
OR strukPoli <>''
OR strukPoli <>'--')
AND a.status='FINISH'
AND (j.tranDate BETWEEN '2018-11-01 00:00:00' AND '2018-11-01 23:59:59')
GROUP BY cb.paymentGrp,
t.paymentStat) AS a
GROUP BY [cutomerSection]
Third using option (fast 500), I don't know why but always got error :
Incorect Syntax near OPTION
PS : Sorry for my bad English

Display query results using pivot

I have the below query which i to display the results using a PIVOT so that the results are displayed in one row.
With T1 As
(EXT Code
),
T2 AS (
SELECT p.SId, p.TransType, Amount =SUM(p.Amount)
FROM T1 p
GROUP BY p.SId, p.TransType
)
SELECT m.SNo, m.Name, t.TransType,
CASE WHEN t.TransType ='PAYCM' THEN t.Amount ELSE 0 END AS 'GROSS CM',
CASE WHEN t.TransType ='PAYYTD' THEN t.Amount ELSE 0 END AS 'GROSS YTD',
CASE WHEN t.TransType ='TAXCM' THEN t.Amount ELSE 0 END AS 'Tax CM',
CASE WHEN t.TransType ='TAXYTD' THEN t.Amount ELSE 0 END AS 'Tax YTD'
FROM Master m INNER JOIN T2 t ON t.PId = m.Id
If you want one row, then use aggregation and remove the non-aggregated columns:
SELECT SUM(CASE WHEN t.TransType = 'PAYCM' THEN t.Amount ELSE 0 END) AS GROSS _CM,
SUM(CASE WHEN t.TransType = 'PAYYTD' THEN t.Amount ELSE 0 END) AS GROSS_YTD,
SUM(CASE WHEN t.TransType = 'TAXCM' THEN t.Amount ELSE 0 END) AS Tax_CM,
SUM(CASE WHEN t.TransType = 'TAXYTD' THEN t.Amount ELSE 0 END) AS Tax_YTD
FROM Master m INNER JOIN
T2 t
ON t.PId = m.Id;

How can I rewrite this query without repeating SELECT subqueries

I have written a query like this. It is working and giving me the result which I wanted.
I have a SELECT query to get CAR_AMOUNT and HOTEL_AMOUNT.
But, I have to repeat the same SELECT query to get the SUM of both. I am not able to use alias name. How can I avoid this?
SELECT B.EMPLOYEE_ID,
(select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'CAR'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED') as CAR_AMOUNT,
(select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'HOTEL'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED') as HOTEL_AMOUNT,
NVL((select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'CAR'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED'),0)
+NVL((select SUM(AMOUNT)
FROM travel_reimbursements_items
WHERE type = 'HOTEL'
AND travel_request_no = B.travel_request_no
AND STATUS='APPROVED'),0) as TOTAL
FROM TRAVEL_REQUEST_ITEM A
LEFT OUTER JOIN TRAVEL_REQUEST B
ON (B.TRAVEL_REQUEST_NO= A.TRAVEL_REQUEST_SR_NO)
select b.employee_id
, sum(case when c.type = 'CAR' then c.amount end) car_amount
, sum(case when c.type = 'HOTEL' then c.amount end) hotel_amount
, sum(c.amount) total
from travel_request_item a
left outer join travel_request b
on (b.travel_request_no= a.travel_request_sr_no)
left outer join travel_reimbursements_items c
on ( c.travel_request_no = b.travel_request_no
and c.type in ('CAR','HOTEL')
and c.status = 'APPROVED'
)
group by b.employee_id
Regards,
Rob.
This should work in oracle.
SELECT B.EMPLOYEE_ID,
SUM(CASE WHEN C.type = 'CAR' and C.STATUS='APPROVED' THEN C.AMOUNT ELSE 0 END) as CAR_AMOUNT,
SUM(CASE WHEN C.type = 'HOTEL' and C.STATUS='APPROVED' THEN C.AMOUNT ELSE 0 END) as HOTEL_AMOUNT,
SUM(CASE WHEN C.type IN ('CAR', 'HOTEL') and C.STATUS='APPROVED' THEN C.AMOUNT ELSE 0 END) as TOTAL
FROM TRAVEL_REQUEST_ITEM A
LEFT OUTER JOIN TRAVEL_REQUEST B ON (B.TRAVEL_REQUEST_NO= A.TRAVEL_REQUEST_SR_NO)
LEFT OUTER JOIN TRAVEL_REIMBURSEMENTS_ITEMS C ON (C.TRAVEL_REQUEST_NO = A.TRAVEL_REQUEST_SR_NO)
GROUP BY B.EMPLOYEE_ID
Just do a conditional sum based on the item type.