Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12»»

status on a given date or date range Expand / Collapse
Author
Message
Posted Wednesday, March 20, 2013 2:18 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 25, 2013 1:22 PM
Points: 4, Visits: 77
I have a situation where we need to be able to query the status a claim was at any given time.

For example, I have a table in (2008R2):

CREATE TABLE ClaimStatus(
ClaimID int NOT NULL,
StatusDate datetime NOT NULL,
StatusCode int NOT NULL)

INSERT INTO ClaimStatus VALUES (7150,'2013-03-11 10:41:29.823',100)
INSERT INTO ClaimStatus VALUES (7150,'2013-03-12 07:20:41.720',300)
INSERT INTO ClaimStatus VALUES (7150,'2013-03-15 13:35:50.000',310)
INSERT INTO ClaimStatus VALUES (7148,'2013-03-01 10:41:29.780',100)
INSERT INTO ClaimStatus VALUES (7148,'2013-03-10 07:21:26.557',300)
INSERT INTO ClaimStatus VALUES (7148,'2013-03-20 13:35:50.000',310)
INSERT INTO ClaimStatus VALUES (7149,'2013-02-01 01:19:20.110',100)
INSERT INTO ClaimStatus VALUES (7149,'2013-02-14 07:21:26.557',300)
INSERT INTO ClaimStatus VALUES (7149,'2013-03-14 00:35:50.000',310)
INSERT INTO ClaimStatus VALUES (7147,'2013-02-01 01:19:20.110',100)
INSERT INTO ClaimStatus VALUES (7147,'2013-02-14 07:21:26.557',300)
INSERT INTO ClaimStatus VALUES (7147,'2013-03-10 00:35:50.000',310)

Let's say I need a query that would tell me which claims were at a status of 300 on 3/14/2013. Some of these claims were, in fact, at a status of 300 on that day but how do I query that since there is no entry for the specific date of 3/14/2013? ( ie: claim 7148 was changed to a status of 300 on 3/10 and was not changed to another status until 3/20, so I would need that claim pulled back in my query as it would have still been status 300 on 3/14.)
Post #1433479
Posted Wednesday, March 20, 2013 2:33 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:11 PM
Points: 20,861, Visits: 32,887
Something like this:


CREATE TABLE ClaimStatus(
ClaimID int NOT NULL,
StatusDate datetime NOT NULL,
StatusCode int NOT NULL);

INSERT INTO ClaimStatus VALUES (7150,'2013-03-11 10:41:29.823',100);
INSERT INTO ClaimStatus VALUES (7150,'2013-03-12 07:20:41.720',300);
INSERT INTO ClaimStatus VALUES (7150,'2013-03-15 13:35:50.000',310);
INSERT INTO ClaimStatus VALUES (7148,'2013-03-01 10:41:29.780',100);
INSERT INTO ClaimStatus VALUES (7148,'2013-03-10 07:21:26.557',300);
INSERT INTO ClaimStatus VALUES (7148,'2013-03-20 13:35:50.000',310);
INSERT INTO ClaimStatus VALUES (7149,'2013-02-01 01:19:20.110',100);
INSERT INTO ClaimStatus VALUES (7149,'2013-02-14 07:21:26.557',300);
INSERT INTO ClaimStatus VALUES (7149,'2013-03-14 00:35:50.000',310);
INSERT INTO ClaimStatus VALUES (7147,'2013-02-01 01:19:20.110',100);
INSERT INTO ClaimStatus VALUES (7147,'2013-02-14 07:21:26.557',300);
INSERT INTO ClaimStatus VALUES (7147,'2013-03-10 00:35:50.000',310);

declare @StatusDate date = '20130314',
@StatusCode int = 300;

with BaseData as (
select
ClaimID,
StatusDate,
StatusCode,
rn = row_number() over (partition by ClaimID order by StatusDate desc)
from
ClaimStatus
where
cast(StatusDate as date) <= @StatusDate
)
select * from BaseData where rn = 1 and StatusCode = @StatusCode;

drop table ClaimStatus;





Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1433489
Posted Wednesday, March 20, 2013 2:42 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 25, 2013 1:22 PM
Points: 4, Visits: 77
That works! Thanks very much!
Post #1433493
Posted Wednesday, March 20, 2013 2:44 PM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
I think that the above would miss Claim 7149 which as I read your issue should show up on the list. This is easy enough to correct by changing @StatusDate to a datetime and the where clause to StatusDate <= @StatusDate

_______________________________________________________________

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 Moden's 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)
Post #1433496
Posted Wednesday, March 20, 2013 2:46 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Thursday, July 25, 2013 1:22 PM
Points: 4, Visits: 77
Good call. Those times really get in the way sometimes. :) Thank you!
Post #1433497
Posted Wednesday, March 20, 2013 3:54 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:11 PM
Points: 20,861, Visits: 32,887
Sean Lange (3/20/2013)
I think that the above would miss Claim 7149 which as I read your issue should show up on the list. This is easy enough to correct by changing @StatusDate to a datetime and the where clause to StatusDate <= @StatusDate


Actually, Sean, my code works as is. If you change the value in @StatusCode from 300 to 210 it catches Claim 7149 just fine.

If you look closely, I cast the Datetime column to a date value (this is SQL Server 2008) and do a <+. Else wise you need to add 1 day to the date you are using, for 2013-03-14 you would want < '2013-03-15' if looking at the full datetime value in StatusDate.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1433525
Posted Wednesday, March 20, 2013 3:57 PM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:11 PM
Points: 20,861, Visits: 32,887
Dena Mansfield (3/20/2013)
Good call. Those times really get in the way sometimes. :) Thank you!


Look carefully at my code, I convert the datetime value to a date only value since this is a SQL Server 2008 forum.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1433528
Posted Thursday, March 21, 2013 7:23 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
Lynn Pettis (3/20/2013)
Sean Lange (3/20/2013)
I think that the above would miss Claim 7149 which as I read your issue should show up on the list. This is easy enough to correct by changing @StatusDate to a datetime and the where clause to StatusDate <= @StatusDate


Actually, Sean, my code works as is. If you change the value in @StatusCode from 300 to 210 it catches Claim 7149 just fine.

If you look closely, I cast the Datetime column to a date value (this is SQL Server 2008) and do a <+. Else wise you need to add 1 day to the date you are using, for 2013-03-14 you would want < '2013-03-15' if looking at the full datetime value in StatusDate.


Lynn, claim 7149 was in a status of 300 until 00:35:50.000 on 2013-03-14 and the code you posted does not return it.


_______________________________________________________________

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 Moden's 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)
Post #1433772
Posted Thursday, March 21, 2013 7:35 AM


SSC-Insane

SSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-InsaneSSC-Insane

Group: General Forum Members
Last Login: Today @ 9:11 PM
Points: 20,861, Visits: 32,887
Sean Lange (3/21/2013)
Lynn Pettis (3/20/2013)
Sean Lange (3/20/2013)
I think that the above would miss Claim 7149 which as I read your issue should show up on the list. This is easy enough to correct by changing @StatusDate to a datetime and the where clause to StatusDate <= @StatusDate


Actually, Sean, my code works as is. If you change the value in @StatusCode from 300 to 210 it catches Claim 7149 just fine.

If you look closely, I cast the Datetime column to a date value (this is SQL Server 2008) and do a <+. Else wise you need to add 1 day to the date you are using, for 2013-03-14 you would want < '2013-03-15' if looking at the full datetime value in StatusDate.


Lynn, claim 7149 was in a status of 300 until 00:35:50.000 on 2013-03-14 and the code you posted does not return it.


Based on the OP's original post the status changed on that date from 300 to 310, so this record would not be shown. His requirement simply specified that on a given date was a claim still in a given state, not if it had been in that state. The claim changed state on that date.


Let's say I need a query that would tell me which claims were at a status of 300 on 3/14/2013. Some of these claims were, in fact, at a status of 300 on that day but how do I query that since there is no entry for the specific date of 3/14/2013? ( ie: claim 7148 was changed to a status of 300 on 3/10 and was not changed to another status until 3/20, so I would need that claim pulled back in my query as it would have still been status 300 on 3/14.)



Nothing above indicated that he wanted to see those claims that changed status on that date from 300, but that were still in a status of 300 on a given date and had not changed yet. Nothing indicates that time is relevant to the query.

Trust me, if time had been relevant to the query I would have accounted for it.



Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
Post #1433781
Posted Thursday, March 21, 2013 7:39 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Yesterday @ 2:05 PM
Points: 13,327, Visits: 12,820
Lynn Pettis (3/21/2013)
Sean Lange (3/21/2013)
Lynn Pettis (3/20/2013)
Sean Lange (3/20/2013)
I think that the above would miss Claim 7149 which as I read your issue should show up on the list. This is easy enough to correct by changing @StatusDate to a datetime and the where clause to StatusDate <= @StatusDate


Actually, Sean, my code works as is. If you change the value in @StatusCode from 300 to 210 it catches Claim 7149 just fine.

If you look closely, I cast the Datetime column to a date value (this is SQL Server 2008) and do a <+. Else wise you need to add 1 day to the date you are using, for 2013-03-14 you would want < '2013-03-15' if looking at the full datetime value in StatusDate.


Lynn, claim 7149 was in a status of 300 until 00:35:50.000 on 2013-03-14 and the code you posted does not return it.


Based on the OP's original post the status changed on that date from 300 to 310, so this record would not be shown. His requirement simply specified that on a given date was a claim still in a given state, not if it had been in that state. The claim changed state on that date.


Let's say I need a query that would tell me which claims were at a status of 300 on 3/14/2013. Some of these claims were, in fact, at a status of 300 on that day but how do I query that since there is no entry for the specific date of 3/14/2013? ( ie: claim 7148 was changed to a status of 300 on 3/10 and was not changed to another status until 3/20, so I would need that claim pulled back in my query as it would have still been status 300 on 3/14.)



Nothing above indicated that he wanted to see those claims that changed status on that date from 300, but that were still in a status of 300 on a given date and had not changed yet. Nothing indicates that time is relevant to the query.

Trust me, if time had been relevant to the query I would have accounted for it.


I guess that comes down to interpretation and what the OP wants. It did have a status of 300 on that day, for at least part of it. I was trying to point out a possible issue depending on what the OP needs. It seems to me that the claim in question should show up for that day with a status of either 300 or 310 because it did in fact have both of those values on that day.


_______________________________________________________________

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 Moden's 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)
Post #1433784
« Prev Topic | Next Topic »

Add to briefcase 12»»

Permissions Expand / Collapse