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 |
neo_phyte
Starting Member
10 Posts |
Posted - 2011-02-21 : 06:10:48
|
Hello guys,I have this table for me to create a query. The table called "customer subscription". I have to create a query to count the number of subscription started and to create a query to count the number of subscription reactivated.Here is the scenario:1. Number of subscription started, no previous subscription for the same cstID2. Number of subscription reactivated, at least one previous subscription of the same cstID.prsID cstID prsStartDate prsEndDate 2 1 1/18/2006 1:45:00 PM 1/18/2007 1:45:00 PM3 2 1/20/2006 1:55:00 PM 1/20/2007 1:55:00 PM4 4 2/28/2006 10:42:00 AM 2/28/2007 10:42:00 AM5 6 3/1/2006 8:08:00 PM 3/1/2007 8:08:00 PM6 9 3/8/2006 8:55:00 AM 3/8/2007 8:55:00 AM7 10 3/10/2006 11:33:00 AM 3/10/2007 11:33:00 AM8 11 3/11/2006 2:09:00 PM 3/11/2007 2:09:00 PM9 12 3/11/2006 4:19:00 PM 3/11/2007 4:19:00 PM10 19 3/13/2006 2:50:00 PM 3/13/2007 2:50:00 PM11 21 3/14/2006 11:56:00 AM 3/14/2007 11:56:00 AM12 24 3/30/2006 1:16:00 PM 3/30/2007 1:16:00 PM13 8 4/6/2006 2:33:00 PM 4/6/2007 2:33:00 PM14 34 4/10/2007 2:47:00 PM 4/10/2008 2:47:00 PM15 41 4/11/2007 11:39:00 PM 4/11/2008 11:39:00 PM16 42 6/6/2007 12:13:00 AM 6/6/2008 12:13:00 AM17 4 7/9/2007 12:19:00 PM 7/9/2008 12:19:00 PM18 43 7/23/2007 3:10:00 PM 7/23/2008 3:10:00 PM19 44 8/6/2007 6:32:00 PM 8/6/2008 6:32:00 PM20 4 8/28/2008 7:53:00 PM 8/28/2009 7:53:00 PM21 44 1/3/2009 1:59:00 PM 1/3/2010 1:59:00 PM22 45 3/9/2009 4:35:00 PM 3/9/2010 4:35:00 PM23 47 4/8/2009 1:49:00 AM 4/8/2010 1:49:00 AM24 49 4/8/2009 2:08:00 AM 4/8/2010 2:08:00 AM25 59 7/15/2009 3:57:00 PM 7/15/2011 3:57:00 PM26 60 7/16/2009 4:54:00 AM 7/16/2011 4:54:00 AM27 63 7/16/2009 5:42:00 AM 7/16/2011 5:42:00 AM28 65 7/30/2009 6:29:00 PM 7/30/2011 6:29:00 PM29 57 7/31/2009 1:42:00 PM 7/31/2010 1:42:00 PM30 66 8/1/2009 9:38:00 PM 8/1/2011 9:38:00 PM31 13 8/5/2009 12:43:00 AM 8/5/2010 12:43:00 AM32 26 8/28/2009 1:35:00 AM 8/28/2010 1:35:00 AM33 25 8/28/2009 5:18:00 AM 8/28/2010 5:18:00 AM34 4 8/31/2009 3:55:00 AM 8/31/2010 3:55:00 AM35 46 9/23/2009 12:16:00 PM 9/23/2010 12:16:00 PM36 4 8/31/2010 3:55:00 AM 8/31/2011 3:55:00 AM37 48 7/7/2010 11:26:00 PM 7/7/2011 11:26:00 PM38 62 7/8/2010 5:36:00 PM 7/8/2012 5:36:00 PM39 67 8/20/2010 6:44:00 PM 8/20/2011 6:44:00 PM40 21 2/14/2011 8:42:00 AM 2/14/2012 8:42:00 AM41 67 8/20/2011 6:44:00 PM 2/20/2012 6:44:00 PM42 67 2/20/2012 6:44:00 PM 7/20/2012 6:44:00 PM43 4 8/31/2011 3:55:00 AM 2/29/2012 3:55:00 AM44 25 2/18/2011 7:10:00 AM 2/18/2012 7:10:00 AM45 25 2/18/2012 7:10:00 AM 8/18/2012 7:10:00 AM46 66 8/1/2011 9:38:00 PM 8/1/2012 9:38:00 PM47 66 8/1/2012 9:38:00 PM 8/1/2013 9:38:00 PM48 1 2/18/2011 5:29:00 PM 2/18/2012 5:29:00 PM49 68 2/18/2011 6:35:00 PM 2/18/2012 6:35:00 PM50 69 2/19/2011 1:57:00 PM 2/19/2012 1:57:00 PM51 69 2/19/2012 1:57:00 PM 3/19/2012 1:57:00 PM Please help.Thanks |
|
neo_phyte
Starting Member
10 Posts |
Posted - 2011-02-21 : 08:00:39
|
Tried to query this one:SELECT CUSTOMER_SUBSCRIPTIONS.*FROM CUSTOMER_SUBSCRIPTIONSWHERE CUSTOMER_SUBSCRIPTIONS.prsEndDate > getDate()ORDER BY CUSTOMER_SUBSCRIPTIONS.cstIDI got this result:prsID cstID prsStartDate prsEndDate48 1 2011-02-18 17:29:00 2012-02-18 17:29:0043 4 2011-08-31 03:55:00 2012-02-29 03:55:0036 4 2010-08-31 03:55:00 2011-08-31 03:55:0040 21 2011-02-14 08:42:00 2012-02-14 08:42:0044 25 2011-02-18 07:10:00 2012-02-18 07:10:0045 25 2012-02-18 07:10:00 2012-08-18 07:10:0037 48 2010-07-07 23:26:00 2011-07-07 23:26:0025 59 2009-07-15 15:57:00 2011-07-15 15:57:0026 60 2009-07-16 04:54:00 2011-07-16 04:54:0038 62 2010-07-08 17:36:00 2012-07-08 17:36:0027 63 2009-07-16 05:42:00 2011-07-16 05:42:0028 65 2009-07-30 18:29:00 2011-07-30 18:29:0030 66 2009-08-01 21:38:00 2011-08-01 21:38:0046 66 2011-08-01 21:38:00 2012-08-01 21:38:0047 66 2012-08-01 21:38:00 2013-08-01 21:38:0039 67 2010-08-20 18:44:00 2011-08-20 18:44:0041 67 2011-08-20 18:44:00 2012-02-20 18:44:0042 67 2012-02-20 18:44:00 2012-07-20 18:44:0049 68 2011-02-18 18:35:00 2012-02-18 18:35:0050 69 2011-02-19 13:57:00 2012-02-19 13:57:0051 69 2012-02-19 13:57:00 2012-03-19 13:57:00 Based on the result, it should be like this:Subscription started = 9Subscription reactivated = 5 because those are (cstIDs: 4, 25, 66, 67 and 69) Ignore those expired subscriptions, how I will query to achieve above results. Please help |
 |
|
Ifor
Aged Yak Warrior
700 Posts |
Posted - 2011-02-21 : 08:18:26
|
I am not quite sure what you are after, but a good start would be to produce test data in a form people can easily look at it:(Including making sure dates are in ISO format.)CREATE TABLE #t( prsID int NOT NULL ,cstID int NOT NULL ,prsStartDate datetime NOT NULL ,prsEndDate datetime NOT NULL)INSERt INTO #tSELECT 2, 1, '20060118 13:45:00', '20070118 13:45:00'UNION ALL SELECT 3, 2, '20060120 13:55:00', '20070120 13:55:00'UNION ALL SELECT 4, 4, '20060228 10:42:00', '20070228 10:42:00'-- etc |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-02-21 : 11:42:27
|
Try this:CREATE TABLE #CUSTOMER_SUBSCRIPTIONS( prsID int NOT NULL ,cstID int NOT NULL ,prsStartDate datetime NOT NULL ,prsEndDate datetime NOT NULL)INSERt INTO #CUSTOMER_SUBSCRIPTIONSselect 48,1,'2011-02-18','2012-02-18' union allselect 43,4,'2011-08-31','2012-02-29' union allselect 36,4,'2010-08-31','2011-08-31' union allselect 40,21,'2011-02-14','2012-02-14' union allselect 44,25,'2011-02-18','2012-02-18' union allselect 45,25,'2012-02-18','2012-08-18' union allselect 37,48,'2010-07-07','2011-07-07' union allselect 25,59,'2009-07-15','2011-07-15' union allselect 26,60,'2009-07-16','2011-07-16' union allselect 38,62,'2010-07-08','2012-07-08' union allselect 27,63,'2009-07-16','2011-07-16' union allselect 28,65,'2009-07-30','2011-07-30' union allselect 30,66,'2009-08-01','2011-08-01' union allselect 46,66,'2011-08-01','2012-08-01' union allselect 47,66,'2012-08-01','2013-08-01' union allselect 39,67,'2010-08-20','2011-08-20' union allselect 41,67,'2011-08-20','2012-02-20' union allselect 42,67,'2012-02-20','2012-07-20' union allselect 49,68,'2011-02-18','2012-02-18' union allselect 50,69,'2011-02-19','2012-02-19' union allselect 51,69,'2012-02-19','2012-03-19' Select CS.*, Case when Subtab.Rcnt=1 then 'New' else 'ReActivated' end as Status from #CUSTOMER_SUBSCRIPTIONS CSInner join (Select Count(1) as RCnt, Max(prsid) prsid from #CUSTOMER_SUBSCRIPTIONSgroup by CSTID) as subtabon CS.Prsid = Subtab.PrsidRegards,Bohra |
 |
|
neo_phyte
Starting Member
10 Posts |
Posted - 2011-02-21 : 16:23:14
|
problem solved... |
 |
|
neo_phyte
Starting Member
10 Posts |
Posted - 2011-02-21 : 18:17:46
|
I have still problem, my solution is this...SELECTSUM ( CASE WHEN FirstDate = LastDate AND EndDate > GETDATE () THEN 1 ELSE 0 END ) AS NewSubscriptions ,SUM ( CASE WHEN FirstDate < LastDate AND EndDate > GETDATE () THEN 1 ELSE 0 END ) AS ReactivatedSubscriptionsFROM( SELECT MIN ( prsStartDate ) AS FirstDate ,MAX ( prsStartDate ) AS LastDate ,MAX ( prsEndDate ) AS EndDate FROM CUSTOMER_SUBSCRIPTIONSGROUP BYcstId ) Subscriptions ; but when I used BETWEEN, it display now then uncorrect results, please advised...SELECTSUM ( CASE WHEN FirstDate = LastDate AND EndDate > GETDATE () THEN 1 ELSE 0 END ) AS NewSubscriptions ,SUM ( CASE WHEN FirstDate < LastDate AND EndDate > GETDATE () THEN 1 ELSE 0 END ) AS ReactivatedSubscriptionsFROM( SELECT MIN ( prsStartDate ) AS FirstDate ,MAX ( prsStartDate ) AS LastDate ,MAX ( prsEndDate ) AS EndDate FROM CUSTOMER_SUBSCRIPTIONSWHERE CUSTOMER_SUBSCRIPTIONS . prsStartDateBETWEEN '1/1/2010 12:00:00 AM' AND '1/31/2011 11:59:59 PM'GROUP BYcstId ) Subscriptions ; |
 |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2011-02-21 : 23:05:01
|
It gives four records and as per data it is correct.Can you tell us what the output you are expecting (how many records and the prsid ). |
 |
|
|
|
|
|
|