Solved

SQL join question

Posted on 2011-02-14
15
282 Views
Last Modified: 2012-05-11
Novice SQL user here. Need help.

I have two tables with the same ID in them that I need to join. The second table has multiple instances of that ID with different timestamps for events. I need to join the two on that ID and return the most recent timestamp. Example:

T1:

ID     Name
1      John
2      Steve

T2:

ID     EventTime
1      2011-02-14
1      2011-02-13
2      2011-02-12
2      2011-02-14

RESULTS:

ID     Name             EventTime
1      John              2011-02-14
2      Steve            2011-02-14

The timestamps are BIGINT. Not sure if that matters.
0
Comment
Question by:MrVault
  • 7
  • 4
  • 2
  • +2
15 Comments
 
LVL 13

Expert Comment

by:dwkor
ID: 34891160
Something like that:

;with CTE(ID, EventTime)
as
(
	select ID, MAX(EventTime) as EventTime
	from T2
	group by ID
)
select t1.ID, t1.Name, CTE.EventTime
from T1 join CTE on 
	T1.ID = CTE.ID

Open in new window

0
 
LVL 40

Assisted Solution

by:Sharath
Sharath earned 200 total points
ID: 34891235
select ID,Name,EventTime
  from (
select T1.ID,T2.Name,T2.EventTime,
       row_number() over (partition by T1.ID order by T2.EventTime desc) rn
  from T1
  join T2 on T1.ID = T2.ID) t1
 where rn = 1

Open in new window

0
 

Author Comment

by:MrVault
ID: 34891258
Sorry, I already have a select statement I'm working with. I'm trying to get the "max" value inside it.

My statement is essentially:

select a, b, c, d, e, f, g
from table1 t1
  inner join table2 t2 on t1.a = t2.a
  inner join table3 t3 on t2.h = t3.h
where a = 'someString'

So I'd rather not mix in a "with" or "as", etc.

this table has billions of rows, so the fastest method the better
0
Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

 

Author Comment

by:MrVault
ID: 34891264
Sharath can you try to incorporate that version into the type of statement I already have above?
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34891268
Try row_number. check my query.
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34891276
which is your EventTime column in the above query?
0
 

Author Comment

by:MrVault
ID: 34891301
I'm confused. Are you suggesting the order in which the rows appear determines the most recent? or the ID? I don't think either would necessarily be the case due to updates to the rows.
0
 
LVL 13

Expert Comment

by:dwkor
ID: 34891326
If you don't want to use CTE, you can move it to the join
select t1.ID, t1.Name, CTE.EventTime
from 
	T1 join (
		select ID, MAX(EventTime) as EventTime
		from T2
		group by ID
	) CTE on T1.ID = CTE.ID

Open in new window

0
 

Author Comment

by:MrVault
ID: 34891388
dwkor what is cte?

I was looking at windows functions in sql 2008. anyone have experience with those? things like rank().
0
 
LVL 40

Expert Comment

by:Sharath
ID: 34891830
I already proposed row_number function (window function). Did you try this. To incorporate that in your actual query, I should know the column EventTime and which table has the column.
0
 
LVL 4

Accepted Solution

by:
Ztinel earned 300 total points
ID: 34894717
hi, try this:
select T1.ID, [Name], EventTime
FROM T1 JOIN
(SELECT ID, EventTime, row_number() OVER (PARTITION BY ID ORDER BY ID DESC) as RANK from T2) as T2
ON T1.ID = T2.ID
WHERE T2.RANK = 1
0
 

Author Comment

by:MrVault
ID: 34896702
Ztinel, I'm getting the following error:

An expression of non-boolean type specified in a context where a condition is expected
0
 

Author Comment

by:MrVault
ID: 34898567
Ztinel, that statement worked great. Thank you so much. Kept the query simple and inline. Sharath it was pretty close to yours, just used rank instead of row number.
0
 

Expert Comment

by:Bill_Mac
ID: 34901253
CTE stands for "Common Table Expression".  SQL Server didn't have CTE's until 2005.  CTE's are fabulous.  I have stopped using inline views.  As far as I know, there is no performance penalty for using an CTE vs an inline view.  

The CTE syntax seems a little intimidating at first (at least it did to me), but after you use it a bit, it's easy.

Basic syntax:

WITH <CTE_1_name> AS
(
SELECT . . .
)
,
<CTE_2_name> AS
(
SELECT . . .
)
SELECT
<column list>
FROM
<CTE_1_name>  AS x
 INNER JOIN
<CTE_2_name>  AS y
 ON x.<column> = y.<column> (etc)
WHERE . . .

The CTE has two big advantages over the inline view:

1. It makes your code much easier to debug and maintain.  You define all the CTE's (you can have more than one) first, then finish with a normal looking query that just references the CTE's.  This means the "data returning" part of the query isn't all junked up with subqueries in the FROM clause.  If you want to tweak parts of your query during development, it's a lot easuer with a CTE.   I also find it easier to do maintenance on an existing query that uses CTE's, because I am not visually overwhelmed with all the additional SELECTs of the subqueries.

2.  If you have to do a self join, you can use the same CTE twice.  With Inline Views, you have to redefine the view for each instance of the join.  

A little tip:  The "WITH" keyword that introduces the CTE must be the first statement in query batch.  So if you are declaring variables or something before the CTE, just make sure to finish the line right before the CTE with a semi-colon.

0
 

Author Comment

by:MrVault
ID: 34997183
Thanks Bill for the tips. I appreciate it.
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sql Query Datatype 2 24
SQL Server Count where two id types exist in column 8 27
SQL Server 2012 r2 - Sum totals 2 25
date diff with Fiscal Calendar 4 31
In SQL Server, when rows are selected from a table, does it retrieve data in the order in which it is inserted?  Many believe this is the case. Let us try to examine for ourselves with an example. To get started, use the following script, wh…
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.
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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