Avatar billede mortvader Nybegynder
17. april 2003 - 22:48 Der er 6 kommentarer og
1 løsning

Beregnet sum felt i sql

Hvordan opretter jeg et sumfelt i SQL?
Eksempel:
2 tabeller, konto og post

konto:
kontonr:int
navn:str
balance:decimal (dette skal være beregnet)

post:
lbnr:int
kontonr:int
beloeb:decimal

noget ala:
select sum(beloeb) from post where post.kontonr = konto.kontonr

Jeg regner med at man kan skrive noget i formular feltet i tabledesigneren, men hvordan?
Avatar billede terry Ekspert
17. april 2003 - 23:07 #1
Try making a view, you can NOT do this in table design!
Avatar billede mortvader Nybegynder
17. april 2003 - 23:10 #2
Ja, jeg vil gerne undgå at bruge views, eller gøre det vha mit kald..
Det jeg kigger efter er et kalkuleret felt PÅ tabellen...

Men det kan man måske slet ikke på den her måde?
Avatar billede terry Ekspert
17. april 2003 - 23:30 #3
WHERE are you using the result? If you make a view then this will give you the SQL you need, and this can be used in your program! What program are you using?
Avatar billede mortvader Nybegynder
18. april 2003 - 20:10 #4
I'm writing a Delphi application, and are using the Tdbgrid to view the results.

I am used to work with db engines which support calculated fields in this manner, so I assumed it would be possible in SQL also..

By the way, if i make a select statement like i proposed in my original question, the result will only return accounts which has posts in it.. I don't understand why.. can you help here?

My actual SQL query looks like this:

SELECT
t_finanskonto.Nr,
t_finanskonto.Navn,
t_finanskonto.Beskrivelse,
t_finanskonto.Startdato,
t_finanskonto.Slutdato,
t_finanskonto.Type,
t_finanskonto.Sumfra,
t_finanskonto.Sumtil,
t_finanskonto.Spaerret,
SUM(t_finanspost.beloeb) AS balance
  FROM
  t_finanskonto
  INNER JOIN
  t_finanspost ON
    t_finanskonto.Nr = t_finanspost.kontonr
  GROUP BY
    t_finanskonto.Nr,
    t_finanskonto.Navn,
    t_finanskonto.Beskrivelse,
    t_finanskonto.Startdato,
    t_finanskonto.Slutdato,
    t_finanskonto.Type,
    t_finanskonto.Sumfra,
    t_finanskonto.Sumtil,
    t_finanskonto.Spaerret
Avatar billede terry Ekspert
19. april 2003 - 10:23 #5
Just to make sure I understand where we are, does this SQL work appart from the fact that it only returns accounts with "posts" in it?
If so then you problem is more than likley this part

INNER JOIN
  t_finanspost ON
    t_finanskonto.Nr = t_finanspost.kontonr

INNER JOIN means that it will only select records where there are matching records in both tables. You need to make this to an OUTER JOIN but this can be a LEFT OUTER or RIGHT OUTER. The easiest way to do this is in the Design View window. You will have two tables with a line (realtionship) between them. Right click on the line and you should see two lines Select All Rows from ....
choose the one which best suits your needs. This will make the correct outer join for you. But It may also give other problems! SUM() will maybe fail because there are no values to sum up!
Avatar billede mortvader Nybegynder
20. april 2003 - 17:15 #6
Ah! great help there!

Thanks alot terry!
Avatar billede terry Ekspert
20. april 2003 - 17:21 #7
:o)
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
Computerworld tilbyder specialiserede kurser i database-management

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