Advertisement

09.11.2008 at 09:48AM PDT, ID: 23723663
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

8.1

T-SQL Query

Asked by Putoch in SQL Query Syntax, PL / SQL, SQL Server 2005

Hi i would like to be able to find out for how many months an account is active.
Now an account can change from different status through out its life time

So for example say I have an account who is currently cancelled.

They started as Active in 29-04-06
then they went to another status in 02-04-2007
then they went to Active in 2008-08-01
then cancelled in 2008-09-03
So the total amount of months that this account was active for was 12

I would like to be able to ideally see the account and then the amount of months they were active for
 but i would like to spilt this up by year so something like this using the example above:
2006 8 months active
2007 4 months active
2008 1 month active



I have attached some tables and the query i am using so far to return the account and the amount of months they are active
for.
I have split the report into 2 pieces because in my db i need to be able to calculate a scanero if someone has not actually gone to the system active but with no status recorded until they are then cancelled so i calculate
how many months they are active for by using teh date they were entered into the system to teh date they were cancelled.

but the problem i am having is really trying to layout the results so i can split the report into years with how many months that they were active for.

Can anyone help me with thie please.
Regards,
putoch

Start Free Trial
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
--current status on account
Create table account
(account_id int,
date_entered datetime, 
account_Status_id int, 
account_status_date datetime)
 
insert into account values (3905,'2004-09-22 00:00:00.000',1,'2007-05-11 14:06:37.000')
insert into account values (127304,'2006-04-29 12:48:37.000',3,'2007-10-03 15:28:57.000')
 
 
--historic status on account
create table account_status_history(
account_status_history_id int,
account_id int, 
account_status_id int, 
account_status_date datetime)
 
insert into account_status_history values (337699,3905,1,'2007-05-11 14:06:37.000')
insert into account_status_history values (228446,3905,2,'2007-01-04 10:43:37.000')
insert into account_status_history values (100,3905,1,'2006-04-29 12:48:37.000')
 
 
insert into account_status_history values(435449,127304,3,'2007-10-03 15:28:57.000')
insert into account_status_history values (1837,127304,1,'2006-04-29 12:48:37.000')
 
 
 
select 
 
--a.account_id,
--Active_Months.months_active,
--Prior06Act.MONTHS_ACTIVE1,
a.account_id,Active_Months.Months_Active
,Prior06Act.Months_active1
 
from 
account a 
--If they were active after the 29-04-06 then they will be accounted for here 
LEFT JOIN (select a.account_id , a.account_status_date,-- n.account_status_id, 
			n.account_status_date as end_date,
				sum(datediff(m,a.Account_status_Date,isnull(n.Account_Status_Date,getdate()))) as MONTHS_ACTIVE
				  from account_status_history a
				  left join account_status_history n
					on n.account_id = a.account_id
				   and n.account_status_history_id = (select top 1 i.account_status_history_id from account_status_history i where i.account_id = a.account_id and i.account_status_history_id > a.account_status_history_id order by i.account_status_date asc )
				 where a.account_status_id = 1
				group by a.account_id , a.account_status_date,  n.account_status_date 
						) Active_Months  on   Active_Months.account_id = a.account_id
 
 
--If they were active before the 29-04-06 then they will be accounted for here
LEFT JOIN (select a.account_id, a.date_entered, b.account_status_date,
		sum(datediff(m,a.date_entered,isnull(b.Account_Status_Date,getdate()))) as MONTHS_ACTIVE1
		From account a 
		left join account_Status_history b  on b.account_id = a.account_id
		and b.account_status_history_id = (select top 1 g.account_status_history_id from account_Status_history g where g.account_id  = a.account_id and g.account_status_date > a.date_entered order by g.account_status_date asc)
		where a.account_status_id = 3 
		group by a.account_id,a.date_entered, b.account_status_date
	 )Prior06Act on Prior06Act.account_id = a.account_id
[+][-]09.11.2008 at 05:45PM PDT, ID: 22456322

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.01.2008 at 12:21PM PDT, ID: 22618015

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.07.2008 at 09:40AM PDT, ID: 22661132

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.07.2008 at 10:22AM PDT, ID: 22661503

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 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.07.2008 at 10:25AM PDT, ID: 22661525

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 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.07.2008 at 10:26AM PDT, ID: 22661536

View this solution now by starting your 30-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: SQL Query Syntax, PL / SQL, SQL Server 2005
Sign Up Now!
Solution Provided By: DanielWilson
Participating Experts: 2
Solution Grade: A
 
 
[+][-]11.14.2008 at 04:28PM PST, ID: 22965089

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

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

 
 
Loading Advertisement...
20081112-EE-VQP-44 / EE_QW_2_20070628