Avatar billede havmaage Juniormester
04. oktober 2009 - 11:26 Der er 8 kommentarer og
1 løsning

find antal dage mellem 2 rækker med samme id

Hejsa
jeg prøver at skrive ne sp der bla. skal finde antal dage mellem hver opdatering af en tabel, mere præcis en posterings tabel med konto nummer og posterings dato.

jeg har prøvet med cursor på følgende måde

declare @antaldage int   
    declare @dato1 date ,@dato date
    declare pegefinger CURSOR for
    select kontonr from renteOpgorelse where kontonr = @kontonr
    open pegefinger

    FETCH  pegefinger into  @kontonr

    WHILE @@FETCH_STATUS = 0 BEGIN
    if not @dato is null
      set @dato = @dato1
       
        set @dato1  = (select dato from renteOpgorelse where kontonr = @kontonr)
        set @antaldage =  datediff(day,@dato,@dato1)
        print @antaldage

        FETCH NEXT FROM pegefinger into @kontonr
    END --WHILE
    close pegefinger
    deallocate pegefinger

END
GO


den compiler godt nok men når jeg vil kalde sp'en fejler den med



Msg 512, Level 16, State 1, Procedure tilskriv_rente, Line 23
Subquery returned more than 1 value. This is not permitted when the subquery follows =, !=, <, <= , >, >= or when the subquery is used as an expression.

fo mig virkder det lidt som om cursoren ikke fungere
Avatar billede terry Ekspert
04. oktober 2009 - 11:40 #1
(Subquery returned more than 1 value)!!!

You can only give @dato1  one value so you need to make sure that teh sub select only returns one!

set @dato1  = (select dato from renteOpgorelse where kontonr = @kontonr)
Avatar billede terry Ekspert
04. oktober 2009 - 11:41 #2
What is the purpose of this select?

select kontonr from renteOpgorelse where kontonr = @kontonr

The result should be the same as @kontonr shouldnt it?
Avatar billede havmaage Juniormester
04. oktober 2009 - 12:19 #3
Hi terry i want to calculate the number of days beween rows in my table.

here i have two rows with the samme kontonr, i just want to calculate the number of days between the to dates

kontonr 1 dato 2009-09-04
kontonr 1 dato 2009-10-01

i need that value to add interests to the accounts in my  program.

Its a study asignment therefore it need to be in sp.
Avatar billede terry Ekspert
04. oktober 2009 - 13:01 #4
Can there only be two records (rows) with the same kontonr?
Avatar billede havmaage Juniormester
04. oktober 2009 - 13:24 #5
no there can be many  over 300 :-)

imagine the i nee to call a function for eatch account where the parametres are

exec calculate_interest
@kontonr, @daysSinceLastPost

on each row.
the idea is that the program are to be executed once in year til calculate interest on all bank account bank account.

in danish it is called rente im not shure if it is the same as interests in english.
Avatar billede terry Ekspert
04. oktober 2009 - 13:54 #6
I'd better say that I dont want to get too involved with your SP, I'm not too familier with SP's or cursors, but if I can help to point you in the right direct then great.

So you want to calculate the number of days between each row where the kontonr is the same and the dates are sorted ascending?

If thats what your after then in the sub select dont you need to select the row with the lowest date and which is > the current date?
Avatar billede havmaage Juniormester
04. oktober 2009 - 14:00 #7
I think i found another way araound it. I was looking for a slution that calculated the days between post in existing rows, but why not do it when i insert new rows into the table,
then i should be possible to do

get max(dato) from the kontonr, calculated that with current date and then insert the row,

i just need to expand the table with the number of days column.,

anyway thonks for  your effort, please make a answer and you get the point for this question,
Avatar billede terry Ekspert
04. oktober 2009 - 14:07 #8
Well that could be another way around it
Avatar billede terry Ekspert
04. oktober 2009 - 16:54 #9
thanks
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