Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Can this be done as a simple query?

Author  Topic 

groston
Yak Posting Veteran

61 Posts

Posted - 2005-03-14 : 14:51:18
In my database, there are two tables:

UserAccount:
UserAccountID int pk
UserID int
AccountID int

ActiveLog:
Record int -- the pk value of the table being tracked, in this case, UserAccountID
EnableDate datetime
DisableDate datetime

Each user can have access to multiple accounts and each account may have been activated/deactivated multiple times.

(In table ActiveLog, if the Record is still active, DisableDate is null. Thus, there can be an unlimited number of rows with the same Record and both EnableDate and DisableDate being null, but only one record with EnableDate not null and DisableDate null. Also note that in the actual table, there is an integer column that serves as the pk for this table.)

What I would like to do is to write a simple query (i.e., one without a cursor - that even I can do) that will return a single row for each account associated with a specified user along with the newest EnableDate and its associated DisableDate.

Just to be clear, here is a example:

UserAccount
1 1 2
2 1 4
3 1 7

ActiveLog
1 1/1/2005 2/1/2005
2 11/5/2004 null
1 3/1/2005 null
3 12/3/2004 2/20/2005

The query would return (assuming UserID = 1):

2 3/1/2005 null
4 11/5/2004 null
7 12/3/2004 2/20/2005

Thanks for your help!

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-03-14 : 15:16:55
I think this will do it:


-----------------------------------------------------------
--create table variables with sample data to test the statement below
set nocount on
declare @UserAccount Table
(UserAccountID int
,UserID int
,AccountID int)

declare @ActiveLog Table
(Record int
,EnableDate datetime
,DisableDate datetime)

insert @UserAccount
Select 1, 1, 2 union all
Select 2, 1, 4 union all
Select 3, 1, 7

insert @ActiveLog
Select 1, '1/1/2005', '2/1/2005' union all
Select 2, '11/5/2004', null union all
Select 1, '3/1/2005', null union all
Select 3, '12/3/2004', '2/20/2005'
-----------------------------------------------------------

--Test Select statement against table variables and sample data

Select ua.AccountID
,agg.EnableDate
,al.disableDate
From @ActiveLog as al
JOIN @UserAccount as ua
ON al.Record = ua.UserAccountID

JOIN (--derived table with accountID and latest enabled date
Select accountID
,max(EnableDate) EnableDate
From @ActiveLog al2
JOIN @UserAccount ua2 ON al2.Record = ua2.UserAccountID
Where UserID = 1
Group by AccountID
)
as agg
--Join to derived table to only include latest
--enableDate record for each account
ON agg.accountID = ua.accountID
and agg.EnableDate = al.EnableDate
order by 1


Be One with the Optimizer
TG
Go to Top of Page

groston
Yak Posting Veteran

61 Posts

Posted - 2005-03-14 : 15:23:07
TG,

I will ponder this, try to understand it, then try to use it. Thanks!
Go to Top of Page

X002548
Not Just a Number

15586 Posts

Posted - 2005-03-14 : 15:31:03
I'm still pondering the initial question...

What's this for?



Brett

8-)
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2005-03-14 : 15:35:05
I edited my original post to include a few comments, if it will help the pondering process.

Be One with the Optimizer
TG
Go to Top of Page
   

- Advertisement -