SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Need to find out the latest STATUS of each Consumers on a given date from their Activities


Need to find out the latest STATUS of each Consumers on a given date from their Activities

Author
Message
sql1411
sql1411
SSC Veteran
SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)

Group: General Forum Members
Points: 222 Visits: 96
Hi there,

I've a ConsumerActivity table that records all the activities. Now, I need to find out the latest status for each Consumers on a given day.
For example, I need to write a query that gives me ConsumerID and latest Status on '2012-04-01 00:00:00'.
In this table, there is no activity on this date and as such I won't get any result. However, I want to get the result as:

ConsumerID Status
101 ACTIVE
102 PREMIUM


Below is my simple scenario and query:

USE [Sample]
GO
-- Create Table
CREATE TABLE [dbo].[ConsumerActivity](
[ConsumerID] [varchar](10) NOT NULL,
[ActivityDate] [datetime] NULL,
[Status] [varchar](10) NULL
) ON [PRIMARY]

GO

-- Insert Some Data into this table
GO
INSERT INTO dbo.ConsumerActivity (ConsumerID, ActivityDate, Status)

SELECT 101, '2012-01-10 00:00:00', 'INACTIVE'
UNION ALL
SELECT 101, '2012-05-20 00:00:00', 'ACTIVE'
UNION ALL
SELECT 102, '2012-02-10 00:00:00', 'BASE'
UNION ALL
SELECT 102, '2012-03-15 00:00:00', 'PREMIUM'

GO


Thanks.
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62787 Visits: 17959
Excellent job posting ddl, sample data and an explanation.

This should work based on your sample.


select *
from
(
select *, ROW_NUMBER() over (partition by ConsumerID order by ActivityDate desc) as RowNum
from ConsumerActivity
) x
where x.RowNum = 1



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
CodeMuddler
CodeMuddler
SSC-Enthusiastic
SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)SSC-Enthusiastic (117 reputation)

Group: General Forum Members
Points: 117 Visits: 244
I'm confused by your expected results.

You said you want to get the "latest status as of April 1, 2012." And state that you would expect ConsumerID 101 to be returned as "ACTIVE." Yet the dates in your sample data indicate that ConsumerID 101 is first inserted on January 10th, 2012 with an "INACTIVE" status, and the new status of "ACTIVE" is not inserted until May 20, 2012 (after your "as-of" report date). So I would have thought you would have expected ConsumerID 101 to be returned as "INACTIVE" based on your requirements.

Can you clarify on this point?

blog: http://www.codemuddler.com
twitter: http://www.twitter.com/CodeMuddler
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62787 Visits: 17959
I guess I missed the bit about the date part. Just add a where clause to your query and you should be good to go.


declare @SearchDate datetime = '2012-04-01 00:00:00'

select *
from
(
select *, ROW_NUMBER() over (partition by ConsumerID order by ActivityDate desc) as RowNum
from ConsumerActivity
where ActivityDate < @SearchDate
) x
where x.RowNum = 1



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
sql1411
sql1411
SSC Veteran
SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)

Group: General Forum Members
Points: 222 Visits: 96
@SQL_FS...... You got it right. Thanks for catching my mistake. Yes, for 101 the LatestStatus should be INACTIVE on April04,2012,

@Sean......thanks for your query. Your query can give me the LatestStatus. Sorry, if I confused you from my example as I pointed out by "SQL_FS". I want to know the LatestStatus for a member on a given date and I want to a parameter in this date so that the user can select any date and not just the latest date.

Let me know if I need to clarify further.

Thanks.
sql1411
sql1411
SSC Veteran
SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)

Group: General Forum Members
Points: 222 Visits: 96
Thanks Sean .....This should do it Smile

Thanks again
sql1411
sql1411
SSC Veteran
SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)

Group: General Forum Members
Points: 222 Visits: 96
Hi Sean,

There seems to be a small problem I guess. The query seems to work for a April 04,2012. However, if I pick any ActivityDate as @SearchDate then it gives the previous status and not the status on that given date. For instance, if I set @SearchDate = '2012-05-20 00:00:00' , then for CustomerID 101 I get the Status as 'INACTIVE'.

Is there a way we can fix this??

Thanks
sql1411
sql1411
SSC Veteran
SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)SSC Veteran (222 reputation)

Group: General Forum Members
Points: 222 Visits: 96
Never mind. I just had to add "<=" sign. Smile Thanks again Smile
Sean Lange
Sean Lange
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62787 Visits: 17959
sql1411 (3/8/2013)
Never mind. I just had to add "<=" sign. Smile Thanks again Smile


Glad you figured it out. This brings up a comment. If you store time information in your table you may want to modify your code slightly. Otherwise you will not the results you want if you pass in.

@SearchDate = '2012-05-20 11:11:00'

It really somewhat depends on what you want to do here. If you want to capture data from anytime on that day just modify the where clause a little to

where ActivityDate < dateadd(day, 1, @SearchDate)



_______________________________________________________________

Need help? Help us help you.

Read the article at http://www.sqlservercentral.com/articles/Best+Practices/61537/ for best practices on asking questions.

Need to split a string? Try Jeff Modens splitter.

Cross Tabs and Pivots, Part 1 – Converting Rows to Columns
Cross Tabs and Pivots, Part 2 - Dynamic Cross Tabs
Understanding and Using APPLY (Part 1)
Understanding and Using APPLY (Part 2)
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search