Microsoft SQL Server
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
 
User Name:
Password:
Remember me
Go Back   ASP Free ForumsDatabaseMicrosoft SQL Server

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread ASP Free Forums Sponsor:
  #1  
Old January 26th, 2005, 04:37 PM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
How to return the previous record?

Select lid,

datelastmodified,

datecompleted,

statusid,

statusrecordid

from loan_status

where statusrecordid -1 and statusid = 11

Do you guys have any idea, if stating -1 gives you the previous record?

Reply With Quote
  #2  
Old January 26th, 2005, 05:05 PM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
No, won't work, but it would help if you explained what you were trying to do.

Reply With Quote
  #3  
Old January 26th, 2005, 05:14 PM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
Well, now I think I would need a cursor, but I have a user is pulling the following information from a table:

Select lnid, statusid, statusrecordid, date from loan_status where statusid = 11 and year(datecompleted)= 1800 or
statusid = 12 and year(datecompleted)= 1800

He wants to add another condition to pull or display the previous record, for instance if the statusrecordid is 10, he wants it to display statusrecordid 9 and its associated information (data).

Reply With Quote
  #4  
Old January 26th, 2005, 05:18 PM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
However, I really don't want to use cursors.

Reply With Quote
  #5  
Old January 26th, 2005, 07:27 PM
Lauramc's Avatar
Lauramc Lauramc is offline
SQL Slarentice
ASP Free Intermediate (1500 - 1999 posts)
 
Join Date: Nov 2004
Location: In My Happy Place
Posts: 1,785 Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level)Lauramc User rank is General 2nd Grade (Above 100000 Reputation Level) 
Time spent in forums: 2 Weeks 1 Day 16 h 14 m 43 sec
Reputation Power: 1096
Well I am not sure exactly what you are trying to do, but maybe if you have a variable store the ID number of the "current" record.

If it is always true that the previous record is one less than the current you could then execute a query where the id number is current - 1.

I have not tried anything like this, but it might work.

Reply With Quote
  #6  
Old January 27th, 2005, 09:24 AM
Memnoch's Avatar
Memnoch Memnoch is offline
Unholy Moderator
ASP Free God 14th Plane (11500 - 11999 posts)
 
Join Date: Oct 2003
Location: In hell, where did you think?
Posts: 11,776 Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level)Memnoch User rank is Lieutenant Colonel (40000 - 50000 Reputation Level) 
Time spent in forums: 3 Weeks 5 Days 8 h 27 m 42 sec
Reputation Power: 470
The only issue you may run into if something like this.

you have records 1,2,3,4,5 in the database.
Someone deletes record 3.

Someone views record 4, then wants to view the previous record, but 3 has been deleted.
Now the issue is, there is no way to determine the previous record, because record 3 doesn't exist anymore.

Reply With Quote
  #7  
Old January 27th, 2005, 01:08 PM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
I see what you are saying regarding the issue. I am still working on it, when I get a chance I will post what I have then if you have any ideas, I would love to get your opinion(s).

Reply With Quote
  #8  
Old January 28th, 2005, 02:08 AM
Scorpions4ever Scorpions4ever is offline
Mad Rater
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 126 Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 31 m 44 sec
Reputation Power: 8
You could do something like this:
Code:
SELECT TOP 1 field1, field2, field3
FROM    tablename
WHERE   blah = foo
AND       bar = somethingelse
AND       statusrecordid < 11
ORDER    BY statusrecordid DESC

The TOP 1 will ensure that only one record is selected and the "AND statusrecordid < 11 ORDER BY statusrecordid DESC" part will ensure that only the previous record is selected. The nice part about this is if someone has deleted the record where statusrecordid = 10, this will pick the one previous to that (i.e. 9).
__________________
Up the Irons
What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.

Reply With Quote
  #9  
Old January 28th, 2005, 10:03 AM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
Thanks that was helpful and started more ideas and error-checking. Here's the thing,

I'm finding that knowing the statusrecordid is not important because that is dynamic. Meaning, I don't hardcode the statusrecordid in the script to 11 or 12 because it can be 6 or 7, etc. Another thing, I think I will have to do a self join because I need to compare lnids.

So ideally, where statusid is 11 or 12, compare lnids and check the statusrecordid of the lnids.
For instance, and I know this sounds confusing but please bare with me.

Statusid is the final status 11 or 12 of a record. What I want to know what was the status (statusid) of the record before going to 11 or 12. I hope this makes sense.

lnid datelastmodified datecompleted statusid statusrecordid Status description
000286 2004-06-28 15:47:17.000 2004-06-28 15:51:20.000 12 6 Closed
000286 2004-08-10 09:59:51.000 1800-01-01 00:00:00.000 12 8 Closed

Select /*top 1*/ lnd, datelastmodified,datecompleted,statusid, statusrecordid
From ln_status
where statusid = 11 and year(datecompleted)= 1800 or
statusid = 12 and year(datecompleted)= 1800
/*** I would need to modify this to address the change from record to record***/
and statusrecordid < 11 or statusrecordid < 12
ORDER BY statusrecordid DESC


Does this make better sense? Any insights?

Reply With Quote
  #10  
Old January 28th, 2005, 12:05 PM
Scorpions4ever Scorpions4ever is offline
Mad Rater
ASP Free Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 126 Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level)Scorpions4ever User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 11 h 31 m 44 sec
Reputation Power: 8
Don't know what language you are using, but just about every language I've used (ASP, VB, Delphi, Python etc.) allow you to create parameters with your SQL statement. Briefly, you could do something like this (pseudocode):
Code:
    Dim oConn As ADODB.Connection
    Dim oCmd As ADODB.Command
    Dim Params(1) As Long
    Dim oRS As ADODB.Recordset
    
    Set oConn = CreateObject("ADODB.Connection")
    oConn.ConnectionString = "Provider=SQLOLEDB.1; Data Source=localhost;uid=foo;pwd=testthis;database=Nor  thwind"
    Call oConn.Open
    
    Set oCmd = CreateObject("ADODB.Command")
    oCmd.ActiveConnection = oConn
    oCmd.CommandType = adCmdText
    oCmd.CommandText = "Select /*top 1*/ lnd, datelastmodified,datecompleted,statusid, statusrecordid
From ln_status
where statusid = 11 and year(datecompleted)= 1800 or
statusid = 12 and year(datecompleted)= 1800
and statusrecordid < ?  <--- Note the ?
ORDER BY statusrecordid DESC"

    MyVal = 11
   Set oRs = oCmd.Execute(,MyVal) <--- I'm passing stuff as a parameter here

Reply With Quote
  #11  
Old January 28th, 2005, 02:40 PM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
This is what I have so far: (tsql)

Select ls1.loanrecordid, ls1.datelastmodified,ls1.datecompleted, ls1.statusid,
ls1.statusrecordid, setups_loanstatus.statusdescription
from loan_status ls1 INNER Join
(select loanrecordid, statusrecordid from loan_status) ls2
ON ls1.loanrecordid = ls2.loanrecordid and ls1.statusrecordid = ls2.statusrecordid
join setups_loanstatus ON
ls1.statusid = setups_loanstatus.statusid
join loan_main on ls1.loanrecordid = loan_main.loanrecordid
where
ls1.statusid = 11 or ls1.statusid = 12 and ls1.loanrecordid = ls2.loanrecordid
Order by ls1.loanrecordid

This gives me:

loanrecordid datelastmodified datecompleted statusid statusrecordid Status description

1000161 2004-04-20 00:00:00.000 2004-04-20 00:00:00.000 12 34 Withdrawn
1000161 2005-01-12 10:55:10.000 1800-01-01 00:00:00.000 11 36 Declined

What I want is the above and the previous statusid and statusrecordid for loanrecordid 1000161. Wouldn't it be statusrecordid = statusrecordid - 1 or something like that to give me the previous record(s) for 1000161? Or would I need to create a function and cursor?

Reply With Quote
  #12  
Old February 2nd, 2005, 09:37 AM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
Do you have an example?

I'm back working on this and so far I haven't been able to illustrate this suggestion.




Quote:
Originally Posted by Lauramc
Well I am not sure exactly what you are trying to do, but maybe if you have a variable store the ID number of the "current" record.

If it is always true that the previous record is one less than the current you could then execute a query where the id number is current - 1.

I have not tried anything like this, but it might work.

Reply With Quote
  #13  
Old February 2nd, 2005, 09:45 AM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
Set nocount on
GO
Declare @storecurrent int
set @storecurrent = ls1.statusrecordid

Select ls1.loanrecordid, ls1.datelastmodified,ls1.datecompleted, ls1.statusid,
ls1.statusrecordid, setups_loanstatus.statusdescription
from loan_status ls1 INNER Join
(select loanrecordid, statusrecordid from loan_status) ls2
ON ls1.loanrecordid = ls2.loanrecordid and ls1.statusrecordid = ls2.statusrecordid

join setups_loanstatus ON
ls1.statusid = setups_loanstatus.statusid
join loan_main on ls1.loanrecordid = loan_main.loanrecordid

where
ls1.statusid = 11 or ls1.statusid = 12 and ls1.loanrecordid = ls2.loanrecordid and ls1.statusrecordid = @storecurrent - 1
Order by ls1.loanrecordid

Syntactially, how can I set @storecurrent to the select stmt pulling the statusrecordid? Since I am setting the variable to ls1.statusrecordid, it does not recognize ls1 because I haven't establish my join yet. Any ideas?

Reply With Quote
  #14  
Old February 2nd, 2005, 10:27 AM
samcneal samcneal is offline
Contributing User
ASP Free Newbie (0 - 499 posts)
 
Join Date: Dec 2004
Posts: 75 samcneal User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 26 m 2 sec
Reputation Power: 4
I made some additional changes but no luck. I think I am grasping at straws. Would you suggest creating a temp table to store information?


Declare @storecurrent int, @counter int
set @storecurrent = (select statusrecordid from loan_status)
select @counter = 1

While @counter <= @@rowcount

Begin
Select ls1.loanrecordid, ls1.datelastmodified,ls1.datecompleted, ls1.statusid,
ls1.statusrecordid, setups_loanstatus.statusdescription
from loan_status ls1 INNER Join
(select loanrecordid, statusrecordid from loan_status) ls2
ON ls1.loanrecordid = ls2.loanrecordid and ls1.statusrecordid = ls2.statusrecordid
join setups_loanstatus ON ls1.statusid = setups_loanstatus.statusid
join loan_main on ls1.loanrecordid = loan_main.loanrecordid
where ls1.statusid = 11 or ls1.statusid = 12 and ls1.loanrecordid = ls2.loanrecordid
set @counter = @counter - 1
end
--Order by ls1.loanrecordid
--@storecurrent = @storecurrent - 1

Reply With Quote
Reply

Viewing: ASP Free ForumsDatabaseMicrosoft SQL Server > How to return the previous record?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump