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 |
|
rcp
Starting Member
32 Posts |
Posted - 2012-02-07 : 10:56:42
|
| Hi,I need to get a count on the customer_id column. This is the query I have so far:WITHT1CountsAS(select Convert(date,created_date) AS ordDate ,customer_idfrom customer_orderwhere YEAR(created_date) = '2011'and status like 'paid'and PackageDescription not in ('Extended Archive Membership','Extended Archive Access'))SELECT COUNT(T1.customer_id) AS [Loyalty] ,T1.customer_id FROM T1Counts T1 INNER Join (select distinct customer_id from customer_order where MONTH(created_date) = '1' and YEAR(created_date) = '2012' and status like 'paid' and PackageDescription not in ('Extended Archive Membership','Extended Archive Access')) AS b ON T1.customer_id=b.customer_idGROUP BY T1.customer_idHAVING COUNT(T1.customer_id) > 2SAMPLE OF RESULTS:Loyalty customer_id4 14232EAD-D584-11D3-B772-0000E2220F515 8C3E6535-E2C0-11D3-B779-0000E2220F513 074606B8-E34C-11D3-B77A-0000E2220F515 0BF71A1A-F5C9-11D3-B77D-0000E2220F51Any help would be greatly appreciated.Regards,Roland |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-07 : 12:29:55
|
| whats the result you're looking at? whats the field on which you want to count on? explain with some sample data to help us understand grouping------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
X002548
Not Just a Number
15586 Posts |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
|
|
rcp
Starting Member
32 Posts |
Posted - 2012-02-07 : 12:42:29
|
| This query is now causing me pain, I am trying to find out, in a given month, the number of customers that are making their third or more purchase for the previous 12 months. Counting any multiple order in one day as one order. The count needs to be on customer_id. The query I have uploaded shows the number of customers that have made a purchase in January that are making their 3rd or more ordersLoyalty customer_id4 14232EAD-D584-11D3-B772-0000E2220F515 8C3E6535-E2C0-11D3-B779-0000E2220F513 074606B8-E34C-11D3-B77A-0000E2220F515 0BF71A1A-F5C9-11D3-B77D-0000E2220F51All I want is to count how many customers meet this criteria, but it is proving much easier to state it then to come up with an answer.Regards,Roland |
 |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2012-02-07 : 12:48:36
|
quote: Originally posted by rcp I am trying to find out, in a given month, the number of customers that are making their third or more purchase for the previous 12 months. Counting any multiple order in one day as one order. Regards,Roland
So, let me see if I understand.For this Month, I need to add up the previous 12 months of purchases, and see if this Months total is equal to 2/3.00 times that total (or greater?). And all purchases on any give days should count as 1?Is that correct?Brett8-)Hint: Want your questions answered fast? Follow the direction in this linkhttp://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxWant to help yourself?http://msdn.microsoft.com/en-us/library/ms130214.aspxhttp://weblogs.sqlteam.com/brettk/http://brettkaiser.blogspot.com/ |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-07 : 13:55:20
|
quote: Originally posted by rcp This query is now causing me pain, I am trying to find out, in a given month, the number of customers that are making their third or more purchase for the previous 12 months. Counting any multiple order in one day as one order. The count needs to be on customer_id. The query I have uploaded shows the number of customers that have made a purchase in January that are making their 3rd or more ordersLoyalty customer_id4 14232EAD-D584-11D3-B772-0000E2220F515 8C3E6535-E2C0-11D3-B779-0000E2220F513 074606B8-E34C-11D3-B77A-0000E2220F515 0BF71A1A-F5C9-11D3-B77D-0000E2220F51All I want is to count how many customers meet this criteria, but it is proving much easier to state it then to come up with an answer.Regards,Roland
Sounds like thisSELECT COUNT(CustomerID)FROM(SELECT DISTINCT CustomerID,DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0) AS MonthDateFROM customer_orderWHERE created_date >=DATEADD(mm,DATEDIFF(mm,0,GETDATE()),0)AND created_date < DATEADD(mm,DATEDIFF(mm,0,GETDATE())+1,0))mCROSS APPLY (SELECT COUNT(1) AS Cnt FROM customer_order WHERE CustomerID=m.CustomerID AND created_date >=DATEADD(mm,-11,MonthDate)AND created_date < DATEADD(mm,1,MonthDate))nWHERE Cnt >=3 ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rcp
Starting Member
32 Posts |
Posted - 2012-02-08 : 09:21:02
|
| Hi Brett,What I am trying to do is get a count of the unique customers that have made a purchase in a given month eg. Jan 2012. Then I am matching those to the orders placed over the last twelve months before my given month, in this case, Jan 2011 to Dec 2011. I think I have done it, here is my code, let me know what you think.WITHT1CountsAS(SELECT a.customer_id AS LoyalCustomersfrom customer_order aINNER JOIN(SELECT COUNT(a.customer_id) AS [Loyal Customers] ,a.customer_idFROM(select Convert(date,created_date) AS ordDate ,customer_idfrom customer_orderwhere YEAR(created_date) = '2011'and status like 'paid'and PackageDescription not in ('Extended Archive Membership','Extended Archive Access')group by customer_id,Convert(date,created_date)) agroup by a.customer_idhaving COUNT(a.customer_id) > 2) bON a.customer_id=b.customer_idwhere MONTH(a.created_date) = '1'and YEAR(a.created_date) = '2012'and a.status like 'paid'and a.PackageDescription not in ('Extended Archive Membership','Extended Archive Access')group by a.customer_id)SELECT COUNT(T1.LoyalCustomers) [3 Orders or More]FROM T1Counts T1RESULTS3 Orders or More1124There is a little bit more to it. Any customer that places multiple orders on a single day is just counted as one order as this is determined to be an upgraded order.Cheers,Roland |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-02-08 : 13:59:53
|
| what about my suggestion?------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
rcp
Starting Member
32 Posts |
Posted - 2012-02-09 : 05:57:36
|
| Hi Visakh,I tried your suggestion, but it does not take into account all of the other little bits that I had to consider. I did however take the bit about dates and have added it to my query. See below:DECLARE @MONTHSD AS datetime = DATEADD(mm,DATEDIFF(mm,0,'2011-01-01'),0);WITHT1CountsAS(SELECT a.customer_id AS LoyalCustomersfrom customer_order aINNER JOIN(SELECT COUNT(a.customer_id) AS [Loyal Customers] ,a.customer_idFROM(select Convert(date,created_date) AS ordDate ,customer_idfrom customer_orderwhere created_date between DATEADD(mm,-12,DATEADD(mm,DATEDIFF(mm,0,@MONTHSD),0)) and DATEADD(mm,0,DATEADD(mm,DATEDIFF(mm,0,@MONTHSD),0))and status like 'paid'and PackageDescription not in ('Extended Archive Membership','Extended Archive Access','eam','eaa')group by customer_id,Convert(date,created_date)) agroup by a.customer_idhaving COUNT(a.customer_id) >= 2) bON a.customer_id=b.customer_idwhere a.created_date between @MONTHSD and DATEADD(mm,DATEDIFF(mm,0,@MONTHSD)+1,0)and a.status like 'paid'and a.PackageDescription not in ('Extended Archive Membership','Extended Archive Access','eam','eaa')group by a.customer_id)SELECT COUNT(T1.LoyalCustomers) [3 Orders or More]FROM T1Counts T1Thank you for all of your help. I really do appreciate it, I have learnt so much over the last month from sqlteam.comRegards,Roland |
 |
|
|
|
|
|
|
|