Advertisement

03.14.2008 at 10:41AM PDT, ID: 23242475
[x]
Attachment Details

How to join previous entry to each database result row?

Asked by SWB-Consulting in MS SQL Server, SQL Server 2005

Tags: MSSQL, Standard SQL

Hi,
I have a database table with entries looking like that

id | email | type | datetime
1 | a@b.com | x | 2007-03-24
3 | a@b.com | x | 2008-03-10
4 | x@y.com | y | 2007-04-27
8 | a@b.com | y | 2006-12-19
9 | x@y.com | y | 2006-11-05

Now I did a group by looking like this
SELECT MAX(datetime), email, type FROM table GROUP BY email, type
Additionally, I need the information about the previous entry, here
id | email | type | latest_date | previous_date
3 | a@b.com | x | 2008-03-10 | 2007-03-24
4 | x@y.com | y | 2007-04-27 | 2006-11-05
8 | a@b.com | y | 2006-12-19 | NULL

Any suggestions? Maybe with a VIEW?
A Sub-Select with the attached structure didn't work
(The multi-part identifier "t2.latest_date" could not be bound. [but t2.latest_date is select-able])
Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
-- join previous entry
LEFT JOIN
	(SELECT
		MAX(t11.DateTime_Stamp) AS second_latest_date,
		t11.client_email, 
		t11.type
	FROM
		account_activity AS t11
	WHERE t11.DateTime_Stamp < t2.latest_date
	GROUP BY
		t11.client_email,
		t11.type) AS t12
ON t12.client_email = t3.client_email
	AND t12.type = t3.type
[+][-]03.14.2008 at 10:55AM PDT, ID: 21127743

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zones: MS SQL Server, SQL Server 2005
Tags: MSSQL, Standard SQL
Sign Up Now!
Solution Provided By: derekkromm
Participating Experts: 1
Solution Grade: A
 
 
[+][-]03.14.2008 at 11:36AM PDT, ID: 21128163

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03.14.2008 at 11:41AM PDT, ID: 21128206

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628