Advertisement
| Hall of Fame |
|
[x]
Posted via EE Mobile
|
||
Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again. |
||
| Question |
|
[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.
Your Input Matters 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! |
||
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: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: |
/* Here is some data i have created 3 different accounts*/
/* Holds latest info on account
Account status id 1 = Active 2 = Cancelled*/
Create table account
(account_id int not null,
Account_status_date datetime,
Account_status_id smallint not null);
Insert into account values (356488,'2007-07-28 18:34:25.000',2);
Insert into account values (123456,'2008-03-01 18:34:25.000',1);
Insert into account values (548977,'2008-03-01 18:34:25.000',1);
/*stores all hisotry on account*/
Create table account_status_history(
account_id int not null,
account_status_date datetime ,
account_status_id smallint not null);
Insert into account_status_history values (356488,'2007-01-28 18:34:25.000',1);
Insert into account_status_history values (356488,'2007-03-28 18:34:25.000',2);
Insert into account_status_history values (356488,'2007-04-28 18:34:25.000',1);
Insert into account_status_history values (356488,'2007-07-28 18:34:25.000',2);
Insert into account_status_history values (123456,'2007-02-28 18:34:25.000',1)
Insert into account_status_history values (123456,'2007-06-28 18:34:25.000',2)
Insert into account_status_history values (123456,'2008-03-01 18:34:25.000',1)
Insert into account_status_history values (548977,'2007-02-28 18:34:25.000',1)
Insert into account_status_history values (548977,'2008-01-28 18:34:25.000',2)
Insert into account_status_history values (548977,'2008-03-01 18:34:25.000',1)
/* Create Month Table */
DECLARE @StartDate datetime
DECLARE @EndDate datetime
DECLARE @Date datetime
SET @StartDate = '1/1/1900'
SET @EndDate = '1/1/2100'
SET @Date = @StartDate
CREATE TABLE months (
Month datetime PRIMARY KEY CLUSTERED,
ThisYearBegin datetime,
NextYearBegin datetime,
NextMonthBegin datetime)
WHILE (@Date <= @EndDate)
BEGIN
INSERT INTO Months (
Month,
ThisYearBegin,
NextYearBegin,
NextMonthBegin
) VALUES (
@Date,
CONVERT(datetime, FLOOR(CONVERT(real, DATEADD(day, (DATEPART(dayofyear, @Date)-1)*-1, @Date)))),
DATEADD(year, 1, CONVERT(datetime, FLOOR(CONVERT(real, DATEADD(day, (DATEPART(dayofyear, @Date)-1)*-1, @Date))))),
DATEADD(month, 1, CONVERT(datetime, FLOOR(CONVERT(real, DATEADD(day, (DATEPART(day, @Date)-1)*-1, @Date)))))
)
SET @Date = DATEADD(month, 1, @Date)
END
CREATE NONCLUSTERED INDEX IX_Months_ThisYearBegin ON Months (ThisYearBegin)
CREATE NONCLUSTERED INDEX IX_Months_NextYearBegin ON Months (NextYearBegin)
CREATE NONCLUSTERED INDEX IX_Months_NextMonthBegin ON Months (NextMonthBegin)
/* this is my First try where the restuls are coming back wrong I tend to put these results into a maxtix
using SSRS in Visual Studio*/
SELECT StartMonth.Month AS FirstOrderMonth, OrderMonth.Month AS [Month],
(SELECT Count(*) FROM
(SELECT (FirstOrder1.Account_Status_Date) AS FirstOrderDate, FirstOrder1.Account_ID
FROM Account_Status_History FirstOrder1
WHERE FirstOrder1.Account_Status_ID = 1 and FirstOrder1.account_id = 356488
-- GROUP BY FirstOrder1.Account_ID
) FirstOrder
WHERE FirstOrder.FirstOrderDate BETWEEN StartMonth.Month AND StartMonth.NextMonthBegin
AND Account_ID NOT IN
(SELECT Account_ID FROM Account_Status_History AccClosed
WHERE AccClosed.Account_Status_ID = 2
AND AccClosed.Account_Status_Date BETWEEN StartMonth.Month AND OrderMonth.NextMonthBegin)
)
AS RetainedCustomers
FROM Months StartMonth
FULL OUTER JOIN Months OrderMonth
ON OrderMonth.Month>=StartMonth.Month
where StartMonth.Month BETWEEN DATEADD(month, -12, GETDATE()) AND GETDATE()
and OrderMonth.Month BETWEEN DATEADD(month, -12, GETDATE()) AND GETDATE()
/*this is the one that i was helped with.
The issue i have with this query is that in theory it works fine (for showing 2007)
with this example, as there are only 3 accounts in this example, but if i use this and include the years 2004,05,06,07,08
and then my db has 200k accounts in it this query can not handle this. */
SELECT RIGHT(CONVERT(varchar(11), FirstOrderMonth,113),8) AS FirstOrderMonth
,SUM(CASE WHEN month([Month])= 1 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Jan2007
,SUM(CASE WHEN month([Month])= 2 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Feb2007
,SUM(CASE WHEN month([Month])= 3 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Mar2007
,SUM(CASE WHEN month([Month])= 4 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Apr2007
,SUM(CASE WHEN month([Month])= 5 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS May2007
,SUM(CASE WHEN month([Month])= 6 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Jun2007
,SUM(CASE WHEN month([Month])= 7 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Jul2007
,SUM(CASE WHEN month([Month])= 8 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Aug2007
,SUM(CASE WHEN month([Month])= 9 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Sep2007
,SUM(CASE WHEN month([Month])= 10 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Oct2007
,SUM(CASE WHEN month([Month])= 11 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Nov2007
,SUM(CASE WHEN month([Month])= 12 AND YEAR([Month])=2007 THEN 1 ELSE 0 END) AS Dec2007
FROM
(
SELECT M.Month, FirstOrderMonth
FROM months1 M,
(
SELECT [Month] AS FirstOrderMonth, account_id
FROM months1 INNER JOIN
(SELECT account_id,MIN(account_status_date) AS MinDate
FROM account_status_history
GROUP BY account_id) T ON (MinDate >= [Month] AND MinDate < NextMonthBegin)) A
WHERE account_id IN
(SELECT account_id
FROM account_status_history H
WHERE account_status_date = (SELECT MAX(account_status_date) FROM account_status_history WHERE account_id=H.account_id AND account_status_date<M.NextMonthBegin)
AND account_status_id=1)
) T
GROUP BY FirstOrderMonth
|