What will I use to find the last record in a SQL as there is no LAST group by as in access?
2007-03-29 09:27:08 UTC
SQL database has lots of data in but I only want one record for the most resent used entry
Seven answers:
Serge M
2007-03-29 23:10:26 UTC
For each card:
Select id_card, max(card_date)
From card_table
Group by id_card
For some card:
Select TOP 1 id_card, card_date
From card_table
Where id_card=some_id_card
Order by card_date DESC
Anna May Noon
2007-03-29 09:36:03 UTC
Could you please explain the problem in more details?
What DBMS are you using (MS Access, SQL Server, Oracle, MySql ??) ?
Also what do you mean by last record?
Do you mean the last record inserted into a table?
Why do you need it? What will you do with it once you know it?
Also is the application single-user or multi-user, i.e. is there a possibility that 2 or more users will use the same application/ same DB at the same time?
2007-03-29 09:34:25 UTC
Use ORDER BY to sort the data set, then return only one record:
SELECT ABCD FROM TABLENAME
WHERE ROWNUM < 2
ORDER BY FIELDNAME ASC;
- Comrade Brin
I was wondering if MSFT had a test database so we could insert it dynamically in Windows applications - something like a small popup - "Test your SQL here" and then it produces the resultset datagrid and we could save the whole or part of the data locally or in BitLocker.com or QuickBase.com both owned by Comrade CIA, Comrade FBI - "Technology Evangelists Microsoft" MINUS "INDIANS" is the equation - Andrew
Memphis
2007-03-29 09:35:43 UTC
"Last used" is vague. If you want to find out the last row inserted you can do so with mysql_insert_id (in MySQL). If it's the last row created, sort by time stamp (if saved). Here is a free SQL tutorial for you http://www.webdevelopersnotes.com/tutorials/sql/
Ralph S
2007-03-29 09:34:34 UTC
Why not just reverse your sorting criteria and get just the first record? You should be able to do something like:
select {blah} from {blah} order by {time used column} desc
?
2016-12-08 18:39:52 UTC
What you prefer probable takes quite a few stages of correlated subselects in one question. although, getting one with as many stages of subcorrelation like this could be a bugger to get actual and probable be an magnificent source hog regardless of in case you probably did. yet another decision (and one hugely much less perplexing to place in writing and debug first of all) is to interrupt it into factors in a technique: define CURSOR c1 AS (choose Reference, SUM(SaveAmt) AS SaveAmt FROM someTable team via Reference) CREATE table temp1 AS (Reference, SubRef, Identifier, ...) OPEN c1 DO till EOF c1 FETCH c1 INTO @Reference, @SaveAmt choose MAX(Identifier) INTO @Identifier FROM someTable the place Reference = @Reference choose (MAX)Date INTO @Date FROM someTable the place Reference = @Reference AND Identifier = @Identifier INSERT INTO temp1 (choose Reference, SubRef, Identifier, SaveAmt, Date,... FROM someTable the place Reference = @Reference AND Identifier = @Identifier AND Date = @Date) EndDo close c1 then you easily can do with temp1 as you prefer
SteveN
2007-03-29 09:43:17 UTC
can you do one of these:
SELECT MAX(column_name) FROM...
SELECT MIN(column_name) FROM...
Perhaps using one of these will let you get the last record, for example if the last record is the highest record ID or if the date is the most recent.
ⓘ
This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.