EMPID | FedTaxID | TaxType | Amount |
1059 | 888888888 | TX02 | 2000 |
1059 | 888888888 | TX02 | 1000 |
1059 | 888888888 | TX03 | 2000 |
I write a query to sum up the amount as follows
select SUM(Amount)
FROM
tblTest
WHERE
FedTaxID = '888888888'
AND
TaxTypeCode IN ('TX02', 'TX03')
But what i need is if TX03 exists 1 in the table i would like to add the amount multiple times means if i sum the total i will get 5000 but i would like to get 7000. If TX03 exists 1 time in the table i would like to add multiple times any idea please

Satish BhatPosted Jul 30, 2011, 6:42 AM
SELECT
CASE WHEN (SELECT COUNT(TaxTypeCode) FROM tblTest WHERE TaxTypeCode = 'TX03' AND FedTaxID = '888888888') = 1
THEN
SUM(CASE WHEN TaxTypeCode = 'TX03' THEN (Amount*2) ELSE Amount END)
ELSE
SUM(Amount)
END
FROM
tblTest
WHERE
FedTaxID = '888888888'
Dorababu MekaPosted Jul 30, 2011, 7:06 AM
Suthish NairPosted Jul 30, 2011, 6:39 AM
with taxtype as (
select EMPID, COUNT(1) cnt from #test where TaxType = 'TX03' group by EMPID
),
taxtype1 as (
select ROW_NUMBER() over (order by EMPID) rn, EMPID, amount from #test where TaxType = 'TX03'
)
select case when tt.cnt = 1 then tt1.amount + sum(t.Amount) else sum(t.Amount) end Amount
FROM #test t inner join taxtype tt
on t.EMPID = tt.EMPID
and t.FedTaxID = '888888888'
inner join taxtype1 tt1
on t.EMPID = tt1.EMPID
and tt1.rn = tt.cnt
group by tt.cnt, tt1.amount
Dorababu MekaPosted Jul 30, 2011, 6:11 AM
as per your post i need as follows when i have multiple TX03
2000 + 1000 + 2000 + 1500 =6500
Satish BhatPosted Jul 30, 2011, 6:10 AM
EMPID
What is your expected result?
Is it 2000 + 1000 + (2000 * 2) + (1500 * 2)
Dorababu MekaPosted Jul 30, 2011, 5:57 AM
Suthish NairPosted Jul 30, 2011, 5:56 AM
Dorababu MekaPosted Jul 30, 2011, 5:52 AM
Suthish NairPosted Jul 30, 2011, 5:48 AM
Satish BhatPosted Jul 30, 2011, 5:42 AM
SELECT SUM(CASE WHEN TaxTypeCode = 'TX03' THEN (Amount*2) ELSE Amount END)
FROM
tblTest
WHERE
FedTaxID = '888888888'
AND
TaxTypeCode IN ('TX02', 'TX03')
Dorababu MekaPosted Jul 30, 2011, 5:37 AM
As i have
TX02 values as 2000 and 1000
and TX03 as 2000 as per my query i will get 5000 as output but i would like to get 7000
Suthish NairPosted Jul 30, 2011, 5:34 AM
Dorababu MekaPosted Jul 30, 2011, 3:30 AM
Jiteendra SampathiraoPosted Jul 30, 2011, 3:27 AM
what does this mean if TX03 exists 1 in the table