473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

Simple/General SQL Question

I have a table that we use for audit trail purposes and I need to
evaluate the last row that was enetered for a particular case to see
if it meets certain criteria and if it does I need to return all the
rows that pertain to that case. Thanks in advance for the help.
Jul 20 '05 #1
7 1628
Justin (ju**************@noridian.com) writes:
I have a table that we use for audit trail purposes and I need to
evaluate the last row that was enetered for a particular case to see
if it meets certain criteria and if it does I need to return all the
rows that pertain to that case. Thanks in advance for the help.


Well, that depends on whether there is something in the data in that
row that makes it possible to identify it as the "last row".

Assuming that the primary key is (caseno, rowno) and the rowno reflects
the entered order, this could do:

IF EXISTS (SELECT *
FROM cases c
JOIN (SELECT rowno = MAX(rowno), caseno
FROM cases
GROUP BY caseno) AS M ON c.caseno = m.caseno
WHERE c.caseno = @caseno
AND <your criteria goes here>)
SELECT * FROM cases WHERE caseno = @caseno

--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
"Erland Sommarskog" <es****@sommarskog.se> wrote in message
news:Xn*********************@127.0.0.1...
Justin (ju**************@noridian.com) writes:
I have a table that we use for audit trail purposes and I need to
evaluate the last row that was enetered for a particular case to see
if it meets certain criteria and if it does I need to return all the
rows that pertain to that case. Thanks in advance for the help.


Well, that depends on whether there is something in the data in that
row that makes it possible to identify it as the "last row".

Assuming that the primary key is (caseno, rowno) and the rowno reflects
the entered order, this could do:

IF EXISTS (SELECT *
FROM cases c
JOIN (SELECT rowno = MAX(rowno), caseno
FROM cases
GROUP BY caseno) AS M ON c.caseno = m.caseno
WHERE c.caseno = @caseno
AND <your criteria goes here>)
SELECT * FROM cases WHERE caseno = @caseno


You could also add an identity field, so every time you know which record is
the last by querying

SELECT * from <table> where <identity_field> = max(<identity_field>)
Jul 20 '05 #3
Muzzy (le*******@yahoo.com) writes:
You could also add an identity field, so every time you know which
record is the last by querying

SELECT * from <table> where <identity_field> = max(<identity_field>)


The only difference between an IDENTITY column and the rowno I
suggested, is that the IDENTITY column is a row number on table-wide
basis (and the fact that it has the IDENTITY property is irrelevant),
while my row number is one by case basis. Since the table appears to hold
entries about cases, it appears logical to have the case number as the first
column in the primary key. Of course, Justin's actual table may look com-
pletely different, but since he did not supply information, I worked from
the most reasonable design. If the table does not have a row number, but
a global id, the query would be the as the one I posted.

The one you suggest does not work, and would give a syntax error. Even
if you replace max(<identity_field>) with a subquery to fix the syntax,
it would give you the most recently entered row for any case, and not a
particular case, which was what Justin asked for.


--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4


Thanks for all the help. You're showing paths that have not yet taken.
Sorry to provide you with so little info about my table. I haven't had
good luck with postings so I have a tough time putting the time and
effort into them. Now, on to my problem: This table has several
different columns that I need to return but only need three to evaluate
if I should return it or not. Like I said this is an audit trail table
that tells a story of what happened to a particular case. Column one is
the case number, column two is a sequence number (these two make up the
PK) and column three is a process status. I need to look at the last
entry for a case number (highest sequence number) evaluate the Process
status to see if the case is closed and if it is I need to return the
"story" of the case (all the sequence numbers for that case number.
Thanks again for all the help.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #5
Thanks for all the help. You're showing paths that have not yet taken.
Sorry to provide you with so little info about my table. I haven't had
good luck with postings so I have a tough time putting the time and
effort into them. Now, on to my problem: This table has several
different columns that I need to return but only need three to
evaluate
if I should return it or not. Like I said this is an audit trail table
that tells a story of what happened to a particular case. Column one
is
the case number, column two is a sequence number (these two make up
the
PK) and column three is a process status. I need to look at the last
entry for a case number (highest sequence number) evaluate the Process
status to see if the case is closed and if it is I need to return the
"story" of the case (all the sequence numbers for that case number.
Thanks again for all the help.

Again, forgive me for my lack of knowledge, but I have never used
variables in SQL before. Why are they used in the above example?
Thanks
Jul 20 '05 #6
PROBLEM SOLVED

SELECT *
FROM cases a
WHERE EXISTS
(SELECT *
FROM cases b
WHERE a.case_num = b.case_num AND
(SELECT max(seq_num) most
From cases c
WHERE c.Case_num = b.case_num) = b.seq_num AND
b.Process_Status not in (70, 90, 91, 92))

If there is anymore input or other thoughts please let me know. I've
done some initial testing and it seems to work but I still have more
testing to do.
Jul 20 '05 #7
Justin (ju**************@noridian.com) writes:
Again, forgive me for my lack of knowledge, but I have never used
variables in SQL before. Why are they used in the above example?


Because you said that you were looking for a specific case, I had
to throw that in somewhere. I was assuming that were using a stored
procedure, in which case the case number was a parameter.

But you can see it just as a place holder.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Terry | last post by:
Hi, This is a newbie's question. I want to preload 4 images and only when all 4 images has been loaded into browser's cache, I want to start a slideshow() function. If images are not completed...
7
by: dan | last post by:
hey peeps, i am completely new at c++ and i need some help with an assignment. it is basically about file i/o with fstreams. i understand how to open a file with fstream, but how would you read,...
51
by: Alan | last post by:
hi all, I want to define a constant length string, say 4 then in a function at some time, I want to set the string to a constant value, say a below is my code but it fails what is the correct...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
2
by: Hazzard | last post by:
I just realized that the code I inherited is using all asp.net server controls (ie. webform controls) and when I try to update textboxes on the client side, I lose the new value of the textbox when...
1
by: Chris | last post by:
Sorry to ask such a simple question but here it is, and I'm am new to ASP/WEB I am designing a site and I want to make it general so I can easily change the font/color/sizes of the...
2
by: Allain Bøge | last post by:
It is really a simple question. Visual Basic .NET (2003) I create 2 forms (Form1 and Form2) I create a checkbox in Form1 (checkbox1) I create a checkbox in Form2 (checkbox1) I go to Form1...
7
by: Scott Frankel | last post by:
Still too new to SQL to have run across this yet ... How does one return the latest row from a table, given multiple entries of varying data? i.e.: given a table that looks like this: color...
73
by: Claudio Grondi | last post by:
In the process of learning about some deeper details of Python I am curious if it is possible to write a 'prefix' code assigning to a and b something special, so, that Python gets trapped in an...
17
by: Chris M. Thomasson | last post by:
I use the following technique in all of my C++ projects; here is the example code with error checking omitted for brevity: _________________________________________________________________ /*...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.