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.
Author |
Topic |
eire1130
Starting Member
6 Posts |
Posted - 2010-01-21 : 13:53:52
|
Hi,I need some help with this query. I have set of data that looks like this:Closing dateClosing date+1Closing date+2Closing date+3deactivated date+4deactivated date+5Closing date+6Closing date+7Where the +X dates are just later dates.What I need to do is show the minimum date and ignore everything before the deactivated date.The below query will show the minimum date for the current stage (closing) but it sees the stages prior to deactivation as well, giving me an erroneously early date. This does ignore any stage that is not "closing" for example, but I want it to ignore any stage that has happened if a deactivation has happened (think of deactivated as like a tabula rasa, but we need to keep the history for other reasons)Any idea how I can accomplish this?Key:Opportunity.Oppo_Status this will reflect the current statusOpportunityProgress.Oppo_Status this is contains all past status's and can have many of the same entry type (see table above).(SELECT Oppo_OpportunityId, Oppo_Status, MIN(Oppo_CreatedDate) Oppo_CreatedDate From OpportunityProgress GROUP BY Oppo_OpportunityId, Oppo_Status) OpportunityProgress ON Opportunity.Oppo_OpportunityId = OpportunityProgress.Oppo_OpportunityId AND Opportunity.Oppo_Status = OpportunityProgress.Oppo_Status |
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-21 : 14:18:18
|
How do we know if a deactivation has happened? What field does it represent?We need the folllowing informationTable Structure,Sample data and Expected Output. |
|
|
eire1130
Starting Member
6 Posts |
Posted - 2010-01-21 : 14:46:46
|
quote: Originally posted by vijayisonly How do we know if a deactivation has happened? What field does it represent?We need the folllowing informationTable Structure,Sample data and Expected Output.
I'm not really sure how to answer table structure,Sample data, see aboveExpected output, in the above case, would be +6. I do not want to see +0Right now it is pulling the +0 |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-21 : 15:05:57
|
Please see this and re-state your question.http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx |
|
|
eire1130
Starting Member
6 Posts |
Posted - 2010-01-21 : 15:20:37
|
quote: Originally posted by vijayisonly Please see this and re-state your question.http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspx
1.What I need to do is show the minimum date and ignore everything before the deactivated date.The query will show the minimum date for the current stage (closing) but it sees the stages prior to deactivation as well, giving me an erroneously early date. This does ignore any stage that is not "closing" for example, but I want it to ignore any stage that has happened if a deactivation has happened (think of deactivated as like a tabula rasa, but we need to keep the history for other reasons)2.3. I have a set of data that looks like this:Closing, date+0Closing, date+1Closing, date+2Closing, date+3deactivated, date+4deactivated, date+5Closing, date+6Closing, date+7Where the +X dates are just later dates.4.Key:Opportunity.Oppo_Status this will reflect the current statusOpportunityProgress.Oppo_Status this is contains all past status's and can have many of the same entry type (see table above).(SELECT Oppo_OpportunityId, Oppo_Status, MIN(Oppo_CreatedDate) Oppo_CreatedDateFrom OpportunityProgressGROUP BY Oppo_OpportunityId, Oppo_Status) OpportunityProgressON Opportunity.Oppo_OpportunityId = OpportunityProgress.Oppo_OpportunityIdAND Opportunity.Oppo_Status = OpportunityProgress.Oppo_StatusThis pulls date +05.In the above case, Date +6 should be the expected value, in the above matrix. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-21 : 15:43:25
|
Replace you inner select with this and try...Since you haven't provided table structure...check if the column names are correct.SELECT a.Oppo_OpportunityId, a.Oppo_Status, MIN(a.Oppo_CreatedDate) as Oppo_CreatedDateFrom OpportunityProgress a left join(SELECT Oppo_OpportunityId, Oppo_Status, MAX(Oppo_CreatedDate) AS Oppo_CreatedDate FROM OpportunityProgress where [status] = 'deactivated' GROUP BY Oppo_OpportunityId, Oppo_Status) bon a.Oppo_OpportunityId = b.Oppo_OpportunityId and a.Oppo_Status = b.Oppo_Statuswhere a.Oppo_CreatedDate > coalesce(b.Oppo_CreatedDate,0)GROUP BY a.Oppo_OpportunityId, a.Oppo_Status |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-21 : 15:44:59
|
Sample datadeclare @t table (Oppo_OpportunityId int,Oppo_Status int,[status] varchar(20),Oppo_CreatedDate datetime)insert @tselect 1,1,'closing','2010-01-01'union all select 1,1,'closing','2009-01-02'union all select 1,1,'deactivated','2009-01-03'union all select 1,1,'deactivated','2009-01-04'union all select 1,1,'closing','2009-01-05'union all select 1,1,'closing','2009-01-06'union all select 2,1,'closing','2009-01-02'union all select 2,1,'closing','2009-01-05'union all select 2,1,'closing','2009-01-06' QuerySELECT a.Oppo_OpportunityId, a.Oppo_Status, MIN(a.Oppo_CreatedDate) as Oppo_CreatedDateFrom @t a left join(SELECT Oppo_OpportunityId, Oppo_Status, MAX(Oppo_CreatedDate) AS Oppo_CreatedDate FROM @t where [status] = 'deactivated' GROUP BY Oppo_OpportunityId, Oppo_Status) bon a.Oppo_OpportunityId = b.Oppo_OpportunityId and a.Oppo_Status = b.Oppo_Statuswhere a.Oppo_CreatedDate > coalesce(b.Oppo_CreatedDate,0)GROUP BY a.Oppo_OpportunityId, a.Oppo_Status ResultOppo_OpportunityId Oppo_Status Oppo_CreatedDate------------------ ----------- -----------------------1 1 2009-01-05 00:00:00.0002 1 2009-01-02 00:00:00.000 |
|
|
eire1130
Starting Member
6 Posts |
Posted - 2010-01-22 : 14:42:48
|
quote: Originally posted by vijayisonly Sample datadeclare @t table (Oppo_OpportunityId int,Oppo_Status int,[status] varchar(20),Oppo_CreatedDate datetime)insert @tselect 1,1,'closing','2010-01-01'union all select 1,1,'closing','2009-01-02'union all select 1,1,'deactivated','2009-01-03'union all select 1,1,'deactivated','2009-01-04'union all select 1,1,'closing','2009-01-05'union all select 1,1,'closing','2009-01-06'union all select 2,1,'closing','2009-01-02'union all select 2,1,'closing','2009-01-05'union all select 2,1,'closing','2009-01-06' QuerySELECT a.Oppo_OpportunityId, a.Oppo_Status, MIN(a.Oppo_CreatedDate) as Oppo_CreatedDateFrom @t a left join(SELECT Oppo_OpportunityId, Oppo_Status, MAX(Oppo_CreatedDate) AS Oppo_CreatedDate FROM @t where [status] = 'deactivated' GROUP BY Oppo_OpportunityId, Oppo_Status) bon a.Oppo_OpportunityId = b.Oppo_OpportunityId and a.Oppo_Status = b.Oppo_Statuswhere a.Oppo_CreatedDate > coalesce(b.Oppo_CreatedDate,0)GROUP BY a.Oppo_OpportunityId, a.Oppo_Status ResultOppo_OpportunityId Oppo_Status Oppo_CreatedDate------------------ ----------- -----------------------1 1 2009-01-05 00:00:00.0002 1 2009-01-02 00:00:00.000
Thanks for the response,I tried using your suggestion, and I am still getting the wrong date:Here is the code: LEFT JOIN(SELECT a.Oppo_OpportunityId, a.Oppo_Status, MIN(a.Oppo_CreatedDate) Oppo_CreatedDateFrom OpportunityProgress a Left Join ( SELECT Oppo_OpportunityId, Oppo_Status, Oppo_Stage, MAX(Oppo_CreatedDate) Oppo_CreatedDate FROM OpportunityProgress Where Oppo_Stage = '%Deactivated%' Group By Oppo_OpportunityId, Oppo_Status, Oppo_Stage ) b ON a.Oppo_OpportunityId = b.Oppo_OpportunityIdWhere a.Oppo_CreatedDate > COALESCE(b.Oppo_CreatedDate,0)Group By a.Oppo_OpportunityId, a.Oppo_Status) c ON Opportunity.Oppo_OpportunityId = c.Oppo_OpportunityId AND Opportunity.Oppo_Status = c.Oppo_StatusIn the inner group / select, I made a few slight changes, I deleted out the "and a.Oppo_Status = b.Oppo_Status"as this would restrict to all non-deactivated casesI added Oppo_Stage to the inner group, because it shows the word "deactivated" while status shows the following format "deactivated - Some Reason"Using your table from above, it is pulling the 2009-01-02When what I want is the 2009-01-05.Any ideas on what I am doing wrong? |
|
|
eire1130
Starting Member
6 Posts |
Posted - 2010-01-22 : 15:49:10
|
I think I just fixed it!All I did was switch the inner Left join to an Inner Join. |
|
|
eire1130
Starting Member
6 Posts |
Posted - 2010-01-22 : 16:00:19
|
quote: Originally posted by eire1130 I think I just fixed it!All I did was switch the inner Left join to an Inner Join.
Never mind, i spoke to soon. It's still broken. |
|
|
vijayisonly
Master Smack Fu Yak Hacker
1836 Posts |
Posted - 2010-01-22 : 19:40:09
|
You need to tell us why its broken...I have given you sample data and the output that I got..where is it failing? |
|
|
|
|
|
|
|