Getting last entries from two different tables

75 pts.
Tags:
MSSQL
SQL tables
Hi, I need to get the last entries from two different tables in MSSQL. I can get the last entry for one table by usung the following. SELECT * FROM dbo.Shaft_Scales WHERE dbo.Shaft_Scales.ID = (SELECT max(ID) from dbo.Shaft_Scales ID) The problem is adding another table. Please help

Answer Wiki

Thanks. We'll let you know when a new response is added.

You could try using the in line view like this:

Select table1.col1, table1.col2, table1…., table2.col1, table2.col2, table2….
from
(SELECT *
FROM dbo.Shaft_Scales
WHERE dbo.Shaft_Scales.ID = (SELECT max(ID) from dbo.Shaft_Scales ID) as table1,
(SELECT *
FROM OtherTable
WHERE dbo.OtherTable.ID = (SELECT max(ID) from dbo.OtherTable ID) as table2

—————- another approach from meandyou

This works on [at least some] SQL processors

SELECT T1.* , T2.*
FROM table1 T1, table2 T2
WHERE …

Do we have to discuss why not to use “SELECT * ” in your SQL?<pre>

Try this

SELECT * FROM T1 as a, T2 as b WHERE a.ID = (SELECT max(ID) from T1)
and b.ID = (SELECT max(ID) from T2);</pre>

Discuss This Question: 3  Replies

 
There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when members answer or reply to this question.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
  • carlosdl
    More details are needed here. Do you want to get the last record from each table combined into a single record ? What exactly is the problem when "adding another table" ? what have you tried so far ? Are these tables somehow related ? if not, why do you want to get the last record from each one in a single SQL statement ?
    69,420 pointsBadges:
    report
  • GPNel2006
    Yes, I want both last records from the two tables displayed into a single record. The tables are not related, I just need to get it into a single record for a reporting purpose.
    75 pointsBadges:
    report
  • carlosdl
    Thanks for your answer. Then I think the last answer, by Mjozsa, should do the trick. Please, let us know if it worked.
    69,420 pointsBadges:
    report

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

To follow this tag...

There was an error processing your information. Please try again later.

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Thanks! We'll email you when relevant content is added and updated.

Following