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 BhatRank #2,496 · 563 reputationPosted Jul 30, 2011, 2:42 AM EDT
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'
Jiteendra SampathiraoRank #585 · 4.1K reputationPosted Jul 29, 2011, 11:27 PM EDT
what does this mean if TX03 exists 1 in the table
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 29, 2011, 11:30 PM EDT
Suthish NairRank #92 · 22.8K reputationPosted Jul 30, 2011, 1:34 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 30, 2011, 1:37 AM EDT
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
Satish BhatRank #2,496 · 563 reputationPosted Jul 30, 2011, 1:42 AM EDT
SELECT SUM(CASE WHEN TaxTypeCode = 'TX03' THEN (Amount*2) ELSE Amount END)
FROM
tblTest
WHERE
FedTaxID = '888888888'
AND
TaxTypeCode IN ('TX02', 'TX03')
Suthish NairRank #92 · 22.8K reputationPosted Jul 30, 2011, 1:48 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 30, 2011, 1:52 AM EDT
Suthish NairRank #92 · 22.8K reputationPosted Jul 30, 2011, 1:56 AM EDT
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 30, 2011, 1:57 AM EDT
Satish BhatRank #2,496 · 563 reputationPosted Jul 30, 2011, 2:10 AM EDT
EMPID
What is your expected result?
Is it 2000 + 1000 + (2000 * 2) + (1500 * 2)
Dorababu MekaRank #457 · 5.5K reputationPosted Jul 30, 2011, 2:11 AM EDT
as per your post i need as follows when i have multiple TX03
2000 + 1000 + 2000 + 1500 =6500
Suthish NairRank #92 · 22.8K reputationPosted Jul 30, 2011, 2:39 AM EDT
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 MekaRank #457 · 5.5K reputationPosted Jul 30, 2011, 3:06 AM EDT