Solved

T SQL Select with order by and join

Posted on 2009-07-01
7
580 Views
Last Modified: 2012-05-07
I am trying to write a t-sql query where i am trying to select top 1 column order by desc and making a join with another table. Here's the sample of my desired output along with the table structure:

Table1
--------------------------------
ID      Comments      MyID            Date
1      A                  11            11:34PM
2      B                  22            12:00PM
3      C                  33            01:20AM
1      AA                  11            05:00PM
1      AAA            11            03:00AM


Table2
--------------------------------
ID      Name
1      X
2      Y
3      Z

Result
--------------------------------
ID      Comments      MyID              Name
1      AAA            11            X
2      B                  22            Y
3      C                  33            Z

I want a join from Table1 and Table2 with Table1 returning just top 1 row order by ID desc and join with Table2.
0
Comment
Question by:PuneetKSaxena
7 Comments
 
LVL 2

Expert Comment

by:sunil_mails
ID: 24752956
u ll need a column to uniquely identify each row of table 1
Assuming u r using MS SQL:
Alter Table Table1
add SrNo int identity

select b.*
from (select id,max(SrNo) as SrNo
from table1
group by id) a
inner join table1 b
        on a.SrNo=B.SrNo
inner join table2 C
        on b.id=c.id

In Oracle:
u hv to add a sequence in place of identity column
0
 
LVL 2

Expert Comment

by:sunil_mails
ID: 24752974
Alternatively u can use following query if u r sure that date column is unique and stores time till millisecond.

select b.*
from (select id,max([Date]) as MyDate
from table1
group by id) a
inner join table1 b
        on a.MyDate=B.MyDate
inner join table2 C
        on b.id=c.id
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24753129
can you try this one?


select b.id,a.comment,a.myid,b.name
from table1 as a join table2 as b
on a.id=b.id and a.date=
(
      select dt from
      (select MAX(ta.date) as dt,ta.id from TableA as ta where ta.id=b.id  group by id) as t
)
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 32

Expert Comment

by:awking00
ID: 24755192
I'm not sure I understand your question. You indicate "order by ID desc" but your desired output shows the ID field in ascending order. Also, what is the criteria being used to determine top 1? Is it the earliest time (date) or the longest comments?
0
 
LVL 6

Accepted Solution

by:
IncisiveOne earned 125 total points
ID: 24761704
I am with awking00 ... there is not enough specific info in the question to produce the posted result.  The SQL is easy, once you can specify what you want.
0
 
LVL 32

Expert Comment

by:awking00
ID: 25439713
I've been on two weeks vacation and didn't get the opportunity to see this, but I'm curious why a comment was selected as an accepted solution, especially when the comment included an agreement with my comment?
0

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question