Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi , I have posted this before, but it was marked resolved and it wasn't resolved, so I am posting again.

I have a query which is as follows:

SQL
SELECT     Prepaid.RedeemerID, OneTimePin.WithdrawerID, SUM(Prepaid.Amount) AS PrepaidAmountSum, SUM(OneTimePin.Amount) AS OneTimePinSum, SUM(Prepaid.Amount)- SUM(OneTimePin.Amount) AS 'Difference'
FROM         Prepaid, OnetimePin
WHERE   (Prepaid.RedeemerID = OneTimePin.WithdrawerID) AND (Prepaid.IsActive = 1) AND (OneTimePin.IsActive = 1) AND (Prepaid.IsRedeemed = 1) AND (OneTimePin.IsPaid = 1)
GROUP BY  Prepaid.RedeemerID,OneTimePin.WithdrawerID
ORDER BY 'Difference', Prepaid.RedeemerID, OneTimePin.WithdrawerID


but the results are incorrect, it selects and insane amount for the onetimepin sum and also almost double for the prepaid amount sum.

So the results should just show a combination of

WithdrawerID | OneTimePinSum
21689 | 261200.00

AND

RedeemerID | PrepaidAmountSum
21689 | 30800.00

but it shows .....

RedeemerID| WithdrawerID| PrepaidAmountSum| OneTimePinSum| Difference
21689 | 21689 | 61600.00 | 89852800.00| -89791200.00

The redeemerID and withdrawerID are the same thing.

I can't seem to figure out why this is happening...

Please help
Posted
Updated 10-Mar-14 3:04am
v2
Comments
DipsMak 10-Mar-14 8:02am    
Check the datatype of your columns
DipsMak 10-Mar-14 8:03am    
use (SUM(Prepaid.Amount)- SUM(OneTimePin.Amount))
Member 9374423 10-Mar-14 8:08am    
Hi, The datatypes are both decimal, and the Sum() is not the issue, it has to be the join, when it sums just the amounts as normal, it almost calculates to all the records in that table, even though the next record is less, so every record sums up almost all the records of the table

1 solution

can you please try this query..

SQL
SELECT
    Prepaid.RedeemerID,
    OneTimePin.WithdrawerID,
    SUM(ISNULL(Prepaid.Amount,0)) AS PrepaidAmountSum,
    SUM(ISNULL(OneTimePin.Amount,0)) AS OneTimePinSum,
    SUM(ISNULL(Prepaid.Amount,0))- SUM(ISNULL(OneTimePin.Amount,0)) AS 'Difference'
FROM Prepaid
INNER JOIN OnetimePin ON Prepaid.RedeemerID = OneTimePin.WithdrawerID
WHERE  Prepaid.IsActive = 1 AND OneTimePin.IsActive = 1 AND Prepaid.IsRedeemed = 1 AND OneTimePin.IsPaid = 1
GROUP BY  Prepaid.RedeemerID,OneTimePin.WithdrawerID
ORDER BY 'Difference', Prepaid.RedeemerID, OneTimePin.WithdrawerID
 
Share this answer
 
Comments
Member 9374423 10-Mar-14 8:54am    
Hi, thank you for the response, however the same issue still occurs...
Tejas Vaishnav 10-Mar-14 8:58am    
can you please share your data, like what is selection output, and what is your desired output, and also some short of your Prepaid and Onetime Pin table actual data..

just some short of rows, so it will giving except idea, where is your query is breaking...

Use improve question functionality to add your data...
Member 9374423 10-Mar-14 9:01am    
please see the updated question, kind regards
Tejas Vaishnav 10-Mar-14 9:17am    
you have posted just tow column data for both the table, what about IsActive or IsPaid something like this...

Anyway no need to do that, you got your issue because of your where cause...

WHERE Prepaid.IsActive = 1 AND OneTimePin.IsActive = 1 AND Prepaid.IsRedeemed = 1 AND OneTimePin.IsPaid = 1

Can you please try the above query with modified where cause like this...

WHERE Prepaid.RedeemerID = 21689
Member 9374423 10-Mar-14 9:50am    
Hi The specified redeemerID is just an example of one record that returns in the output,

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900