| Author |
Topic |
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2007-12-04 : 11:46:27
|
| I have members in a database who have paid thru dates. I am creating retention reports I created a cross tab in Crystal (using SQL) that counts records that paid within a certain year. I need to create a script that will let me find when members skip payment for a year. Any ideas?I was thinking of running a count of all paid (Activity) records, but still kind of stuck.DZ |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-04 : 11:49:31
|
Without knowing anything at all about tables and so on, I would guess something like thisSELECT t.*FROM ( SELECT DISTINCT YEAR(DateTimeColumn) AS y FROM Table1 ) AS qLEFT JOIN Table1 AS t ON YEAR(t.DateTimeColumn) = q.yWHERE t.DateTimeColumn IS NULL E 12°55'05.25"N 56°04'39.16" |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2007-12-04 : 12:14:04
|
| Thanks for the quick reply Peso!!This will not work. Let me provide more specific information.There is only one table - ActivityThis table has all of the payment activity for a member.The field I am using for the retention is the Activity.Thru_Date. I am counting and totaling all members by their Activity.Thru_Date.I hope this clarifies - sorry for the vagueness in the first posting.DZ |
 |
|
|
sshelper
Posting Yak Master
216 Posts |
Posted - 2007-12-04 : 12:27:41
|
| Try this query:SELECT MemberIDFROM ActivityGROUP BY MemberIDHAVING MAX(YEAR(Thru_Date)) - MIN(YEAR(Thru_Date)) + 1 != COUNT(*)SQL Server Helperhttp://www.sql-server-helper.com |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-04 : 12:32:45
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (UserID INT, Payment DATETIME)INSERT @SampleSELECT 1, '20050101' UNION ALLSELECT 1, '20060101' UNION ALLSELECT 1, '20070101' UNION ALLSELECT 2, '20050101' UNION ALLSELECT 2, '20070101'-- Show the expected outputSELECT u.UserID, y.y AS PaymentYearFROM ( SELECT DISTINCT YEAR(Payment) AS y FROM @Sample ) AS yCROSS JOIN ( SELECT DISTINCT UserID FROM @Sample ) AS uLEFT JOIN @Sample AS s ON YEAR(s.Payment) = y.y AND s.UserID = u.UserIDWHERE s.UserID IS NULL[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-04 : 12:33:42
|
quote: Originally posted by sshelper SELECT MemberIDFROM ActivityGROUP BY MemberIDHAVING MAX(YEAR(Thru_Date)) - MIN(YEAR(Thru_Date)) + 1 != COUNT(*)
Hmmm...What if MemberID has paid for 2005 and 2006, but not 2007? E 12°55'05.25"N 56°04'39.16" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-04 : 12:36:20
|
[code]-- Prepare sample dataDECLARE @Sample TABLE (UserID INT, Payment DATETIME)INSERT @SampleSELECT 1, '20050211' UNION ALLSELECT 1, '20060115' UNION ALLSELECT 1, '20070422' UNION ALLSELECT 2, '20050102' UNION ALLSELECT 2, '20070317' UNION ALLSELECT 3, '20050201' UNION ALLSELECT 3, '20060202'-- PesoSELECT u.UserID, y.y AS PaymentYearFROM ( SELECT DISTINCT YEAR(Payment) AS y FROM @Sample ) AS yCROSS JOIN ( SELECT DISTINCT UserID FROM @Sample ) AS uLEFT JOIN @Sample AS s ON YEAR(s.Payment) = y.y AND s.UserID = u.UserIDWHERE s.UserID IS NULL-- sshelperSELECT UserIDFROM @SampleGROUP BY UserIDHAVING MAX(YEAR(Payment)) - MIN(YEAR(Payment)) + 1 <> COUNT(*)[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2007-12-04 : 12:42:56
|
| That is exactly what I am trying to catch. I have all of the counts by year, but they want to know when a members skip years for retention purposes.I will have something like this. 2005 2006 2007RegMember Renewed 2000 2600 3300RegMember Skipped 136 135 21 I am trying to figure our how to grab the skipped members (ones that did not pay for that year, but paid for the year before. What does the 3rd script you posted do?DZ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-12-04 : 12:45:20
|
I have absolutely no idea what your data represents!Do you think I am psychic?When you have learned to provide proper and accurate sample data together with expected output based on the provided sample data, I will return to help you. E 12°55'05.25"N 56°04'39.16" |
 |
|
|
dzabor
Posting Yak Master
138 Posts |
Posted - 2007-12-04 : 12:59:05
|
| You are right - this is too hard to explain, but the information you have provided has given me a clearer understanding of how I can make this work and what I need. To really simplify my question:Is there a way to count years or assign a number to each year withing a field. 12/31/2005 returns 112/31/2006 returns 212/31/2007 returns 3when 12/31/2008 come it automatically adds on and returns 4I can even use 12/31/2005 as the start year if one is needed. If not I will try to see if there is another way of doing this directly thru Crystal without throwing in commands from SQL.Thanks for your time.DZ |
 |
|
|
|