Avatar billede smsaid Nybegynder
28. september 2004 - 17:57 Der er 13 kommentarer og
1 løsning

SQL SELECT SUM Problem

Hi All

I have problem with this SQL statement

SELECT ((SUM(S.LLQD) + (SUM(S.KM)) - (SUM(P.LLQD) + (B.AMO))
FROM Send S, Pay P, Bank B
WHERE (P.Paid = True);

what i need is like this (20+80) - (50+45) result must be 5
but I get a result which is much bigger what I expectd or much smaller than I expect, I get s/th like 148575,25.

I am using MSAccess

Help me

Thanks in advance
Avatar billede arne_v Ekspert
28. september 2004 - 18:03 #1
You are aware of that the SUM function accumulates for all rows in the table
where the WHERE condition is true ?
Avatar billede smsaid Nybegynder
28. september 2004 - 18:07 #2
Hi arne

Yes I am aware and what I need is that to add all rows in that table where there is true in paid

Thanks
Avatar billede arne_v Ekspert
28. september 2004 - 18:12 #3
Are you missing some JOIN conditions ?
Avatar billede arne_v Ekspert
28. september 2004 - 18:13 #4
WHERE (P.Paid = True) AND (S.afield = P.afield) AND (S.anotherfield = B.anotherfield)

?
Avatar billede smsaid Nybegynder
28. september 2004 - 18:24 #5
Hi
Are you missing some JOIN conditions ? Yes ... No... May be
but I am trying to make SQL Query that calculates data from three Tables these 3 tables hav't any relations between them so I don't think it should good idea to make any JOIN.

ex. take two fileds from Send table, take one field from Pay and one from Bank
add the first two and substract the other two that i have allready added together.

Thanks
Avatar billede pjotre Novice
28. september 2004 - 18:29 #6
You must join all 3 tables. Otherwise your result will be garbish!
Avatar billede arne_v Ekspert
28. september 2004 - 18:33 #7
If it is SQLServer or similar SQL dialect you may be able to do something like:

SELECT (((SELECT SUM(LLQD) FROM Send) + (SELECT SUM(KM) FROM S)) - ((SELECT SUM(LLQD) FROM PAY WHERE Paid) + B.AMO))

[untested - check use og brackets]
Avatar billede arne_v Ekspert
28. september 2004 - 18:33 #8
I forgot the last:

SELECT (((SELECT SUM(LLQD) FROM Send) + (SELECT SUM(KM) FROM S)) - ((SELECT SUM(LLQD) FROM PAY WHERE Paid) + (SELECT AMO FROM Bank)))
Avatar billede smsaid Nybegynder
28. september 2004 - 18:39 #9
Hi

I will try that and reply latter on

Thanks in advance
Avatar billede smsaid Nybegynder
30. september 2004 - 17:19 #10
Hi

It did n't work but Í solved the problem in other way

Thanks for your time and suggestions you can have the Points arene v

Thanks
Avatar billede arne_v Ekspert
30. september 2004 - 18:25 #11
then I will put in an answer
Avatar billede arne_v Ekspert
17. oktober 2004 - 18:08 #12
Time to accept the answer ?
Avatar billede smsaid Nybegynder
25. oktober 2004 - 01:11 #13
I clicked the accept button so many times but stil it seems that the question is not closed yet, but anyway i accepted comments/answers of arne_V.

Thanks
Avatar billede arne_v Ekspert
25. oktober 2004 - 07:18 #14
It is accepted now.

What usuallly tricks peoples is that ven though there are only one
answerer, then just clicking accept do not have any effect. You still need
to mark that one answerer and then click accept.
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester