Repeat Customers Each Year (Retention) - sql

I've been working on this and I don't think I'm doing it right. |D
Our database doesn't keep track of how many customers we retain so we looked for an alternate method. It's outlined in this article. It suggests you have this table to fill in:
Year Number of Customers Number of customers Retained in 2009 Percent (%) Retained in 2009 Number of customers Retained in 2010 Percent (%) Retained in 2010 ....
2008
2009
2010
2011
2012
Total
The table would go out to 2012 in the headers. I'm just saving space.
It tells you to find the total number of customers you had in your starting year. To do this, I used this query since our starting year is 2008:
select YEAR(OrderDate) as 'Year', COUNT(distinct(billemail)) as Customers
from dbo.tblOrder
where OrderDate >= '2008-01-01' and OrderDate <= '2008-12-31'
group by YEAR(OrderDate)
At the moment we just differentiate our customers by email address.
Then you have to search for the same names of customers who purchased again in later years (ours are 2009, 10, 11, and 12).
I came up with this. It should find people who purchased in both 2008 and 2009.
SELECT YEAR(OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE o1.OrderDate BETWEEN '2008-1-1' AND '2009-1-1')
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE o2.OrderDate BETWEEN '2009-1-1' AND '2010-1-1')
--AND o.OrderDate BETWEEN '2008-1-1' AND '2013-1-1'
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
So I'm just finding the customers who purchased in both those years. And then I'm doing an independent query to find those who purchased in 2008 and 2010, then 08 and 11, and then 08 and 12. This one finds 2008 and 2010 purchasers:
SELECT YEAR(OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE o1.OrderDate BETWEEN '2008-1-1' AND '2009-1-1')
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE o2.OrderDate BETWEEN '2010-1-1' AND '2011-1-1')
--AND o.OrderDate BETWEEN '2008-1-1' AND '2013-1-1'
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
So you see I have a different query for each year comparison. They're all unrelated. So in the end I'm just finding people who bought in 2008 and 2009, and then a potentially different group that bought in 2008 and 2010, and so on. For this to be accurate, do I have to use the same grouping of 2008 buyers each time? So they bought in 2009 and 2010 and 2011, and 2012?
This is where I'm worried and not sure how to proceed or even find such data.
Any advice would be appreciated! Thanks!

How about a cross-tab on a per customer basis to help you out...
From this, you can start to analyze a bit more in bulk by comparing a customer's
current year to the previous and have a total customers count for each respective year.
From that you can run whatever percentages you want in your final output
This should get you a whole set of all years in question, and you can just keep adding years as need be for comparison. It should be very quick, especially if you have an index on ( BillEMail, OrderDate ).
The premise is that the inner query just blows through all the records, and on a customer basis sets a flag of 1 if there are ANY orders within the given year (via MAX()). It does it via case/when so each year is detected for a customer. Once that has been determined, the outer query then rolls those up comparing each customer with if they had a sale in one year vs the prior, if so, SUM() 1 vs 0 and you have your counts of retention.
SELECT
SUM( case when PreQry.C2011 = 1 and PreQry.C2012 = 1 then 1 else 0 end ) as Retain2011_2012,
SUM( case when PreQry.C2010 = 1 and PreQry.C2011 = 1 then 1 else 0 end ) as Retain2010_2011,
SUM( case when PreQry.C2009 = 1 and PreQry.C2010 = 1 then 1 else 0 end ) as Retain2009_2010,
SUM( case when PreQry.C2008 = 1 and PreQry.C2009 = 1 then 1 else 0 end ) as Retain2008_2009,
SUM( PreQry.C2012 ) CustCount2012,
SUM( PreQry.C2011 ) CustCount2011,
SUM( PreQry.C2010 ) CustCount2010,
SUM( PreQry.C2009 ) CustCount2009,
SUM( PreQry.C2008 ) CustCount2008
from
( select
O.BillEMail as customer,
MAX( CASE when YEAR( O.OrderDate ) = 2012 then 1 else 0 end ) as C2012,
MAX( CASE when YEAR( O.OrderDate ) = 2011 then 1 else 0 end ) as C2011,
MAX( CASE when YEAR( O.OrderDate ) = 2010 then 1 else 0 end ) as C2010,
MAX( CASE when YEAR( O.OrderDate ) = 2009 then 1 else 0 end ) as C2009,
MAX( CASE when YEAR( O.OrderDate ) = 2008 then 1 else 0 end ) as C2008
from
dbo.tblOrder O
where
O.OrderDate >= '2008-01-01'
AND O.BillEmail NOT LIKE '%#halloweencostumes.com'
AND O.BillEmail NOT LIKE ''
group by
O.BillEMail ) as PreQry
Now, if you wanted to detect how many were "NEW" for a given year, you could just add additional columns such as testing the previous year sale flag = 0 vs current year = 1 such as
SUM( case when PreQry.C2011 = 0 and PreQry.C2012 = 1 then 1 else 0 end ) as NewIn2012,
SUM( case when PreQry.C2010 = 0 and PreQry.C2011 = 1 then 1 else 0 end ) as NewIn2011,
SUM( case when PreQry.C2009 = 0 and PreQry.C2010 = 1 then 1 else 0 end ) as NewIn2010,
SUM( case when PreQry.C2008 = 0 and PreQry.C2009 = 1 then 1 else 0 end ) as NewIn2009

If I understand your problem right, then you sounds like you've gotten mixed up in the details. It depends on what you want your definition of 'retain' to be. How about 'also bought in some previous year'? Then, for year X, a customer is retained if they also bought from you in a previous year.
For 2012, for example:
SELECT YEAR(OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE o1.OrderDate BETWEEN '2012-1-1' AND '2013-1-1')
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE o2.OrderDate < '2012-1-1')
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
Does this work?
Edit
You can take this a step farther and abstract out the year so 1 query will suffice:
SELECT YEAR(O.OrderDate) as 'Year',COUNT(distinct(billemail)) as Customers
FROM dbo.tblOrder o with (nolock)
WHERE o.BillEmail IN (SELECT DISTINCT o1.BillEmail
FROM dbo.tblOrder o1 with (nolock)
WHERE year(o1.OrderDate)=YEAR(O.OrderDate)
AND o.BillEmail IN (SELECT DISTINCT o2.BillEmail
FROM dbo.tblOrder o2 with (nolock)
WHERE year(o2.OrderDate) < year(o.orderdate)
AND o.BillEmail NOT LIKE '%#halloweencostumes.com'
AND o.BillEmail NOT LIKE ''
GROUP BY YEAR(OrderDate)
This should give you, for each year in which you had orders, the count of distinct customers, and the count of customers who also purchased in a previous year. However, it's not in the same format as the table you want to populate.

Related

SUM and Grouping by date and material

Still learning SQL forgive me.
I have 3 tables. a material table, a material_req table and a material_trans table. I want to group by material and then group columns by year.
so it would be [material, 2019, 2018, 2017, 2016, total (total being the total qty used for each material.
I have tried to place the date in the select statement, and grouped by the date also. but then the returned result is a lot of the same material with a lot of dates. I only need the year. maybe try the same and return just the year?
SELECT material_req.Material
-- , Material_Trans_Date
, SUM(-1 * material_trans.Quantity) AS 'TOTAL'
,Standard_Cost
FROM
Material_Req inner join Material_Trans
ON
Material_Req.Material_Req = Material_Trans.Material_Req
LEFt JOIN Material
ON
Material.Material = Material_Req.Material
WHERE
material_trans.Material_Trans_Date between '20180101' AND GETDATE()
-- Material_Trans_Date between '20180101' AND '20181231'
-- Material_Trans_Date between '20170101' AND '20171231'
-- Material_Trans_Date between '20160101' AND '20161231'
GROUP BY
material_req.Material ,Standard_Cost
ORDER BY
Material_Req.Material, Standard_Cost
expected results should by grouped by material, 2019, 2018, 2017,2016, Standard_Cost. the years column will have the sum of qty for each material for that year.
results look like this current_results
If you are using SQL Server then you might try this:
SELECT material_req.Material
, SUM(CASE WHEN DATEPART(YEAR, Material_Trans_Date) = '2019' THEN material_trans.Quantity ELSE 0 END) [2019 TOTAL]
, SUM(CASE WHEN DATEPART(YEAR, Material_Trans_Date) = '2018' THEN material_trans.Quantity ELSE 0 END) [2018 TOTAL]
,Standard_Cost
FROM
Material_Req inner join Material_Trans
ON
Material_Req.Material_Req = Material_Trans.Material_Req
LEFt JOIN Material
ON
Material.Material = Material_Req.Material
WHERE
material_trans.Material_Trans_Date between '20180101' AND GETDATE()
GROUP BY
material_req.Material ,Standard_Cost
ORDER BY
Material_Req.Material, Standard_Cost

Can my query be made more efficient, and how about adding a GROUP BY clause?

I am trying to write a query that returns:
The total amount of transactions that occurred before a date range, for a particular customer.
The total amount of transactions that occurred within a date range, for a particular customer.
The total amount of payments that occurred before a date range, for a particular customer.
The total amount of payments that occurred within a date range, for a particular customer.
To that end, I've come up with the following query.
declare #StartDate DATE = '2016-08-01'
declare #EndDate DATE = '2016-08-31'
declare #BillingCategory INT = 0
select c.Id, c.Name, c.StartingBalance,
(select coalesce(sum(Amount), 0) from Transactions t where t.CustomerId = c.Id and t.[Date] < #StartDate) xDebits,
(select coalesce(sum(Amount), 0) from Transactions t where t.CustomerId = c.Id and t.[Date] >= #StartDate and t.[Data] <= #EndDate) Debits,
(select coalesce(sum(Amount), 0) from Payments p where p.CustomerId = c.Id and p.[Date] < #StartDate) xCredits,
(select coalesce(sum(Amount), 0) from Payments p where p.CustomerId = c.Id and p.[Date] >= #StartDate and p.[Date] <= #EndDate) Credits
from customers c
where c.BillingCategory in (0,1,2,3,4,5)
This query seems to give the results I want. I used the subqueries because I couldn't seem to figure out how to accomplish the same thing using JOINs. But I have a few questions.
Does this query retrieve the transaction and payment data for every single customer before filtering it according to my WHERE condition? If so, that seems like a big waste. Can that be improved?
I'd also like to add a GROUP BY to total each payment and transaction column by BillingCategory. But how can you add a GROUP BY clause here when the SELECTed columns are limited to aggregate functions if they are not in the GROUP BY clause?
The Transactions and Payments tables both have foreign keys to the Customers table.
Sample data (not real)
Customers:
Id Name BillingCategory
----- ------- ---------------
1 'ABC' 0
2 'DEF' 1
3 'GHI' 0
Transactions:
Id CustomerId Date Amount
----- ---------- ------------ ------
1 2 '2016-08-01' 124.90
2 2 '2016-08-04' 37.23
3 1 '2016-08-27' 450.02
Payments:
Id CustomerId Date Amount
----- ---------- ------------ ------
1 1 '2016-09-01' 50.00
2 1 '2016-09-23' 75.00
3 2 '2016-09-01' 100.00
You could build your sums seperately for Transactions and Payments in a CTE and then join them together:
WITH
CustomerTransactions AS
(
SELECT CustomerId,
SUM(CASE WHEN [Date] < #StartDate THEN 1 ELSE 0 END * COALESCE(Amount, 0)) AS xDebits,
SUM(CASE WHEN [Date] >= #StartDate THEN 1 ELSE 0 END * COALESCE(Amount, 0)) AS Debits
FROM Transactions
GROUP BY CustomerId
),
CustomerPayments AS,
(
SELECT CustomerId,
SUM(CASE WHEN [Date] < #StartDate THEN 1 ELSE 0 END * COALESCE(Amount, 0)) AS xCredits,
SUM(CASE WHEN [Date] >= #StartDate THEN 1 ELSE 0 END * COALESCE(Amount, 0)) AS Credits
FROM Payments
GROUP BY CustomerId
)
SELECT C.Id, c.Name, c.StartingBalance,
COALESCE(T.xDebits, 0) AS xDebits,
COALESCE(T.Debits, 0) AS Debits,
COALESCE(P.xCredits, 0) AS xCredits,
COALESCE(P.Credits, 0) AS Credits
FROM Custormers C
LEFT OUTER JOIN CustomerTransactions T ON T.CustomerId = C.Id
LEFT OUTER JOIN CustomerPayments P ON P.CustomerId = C.Id
WHERE C.BillingCategory IN(0, 1, 2, 3, 4, 5);
You can do with sub-queries to be more efficient. Pre-query grouped by each customer for only those customers who qualify by the categories in question. These sub-queries will always result in an at-most, 1 record per customer so you don't get a Cartesian result. Get that for your debits and credits and re-join back to your master list of customers with a left-join in case one side or the other (debits/credits) may not exist.
declare #StartDate DATE = '2016-08-01'
declare #EndDate DATE = '2016-08-31'
declare #BillingCategory INT = 0
select
c.ID,
c.Name,
c.StartingBalance,
coalesce( AllDebits.xDebits, 0 ) DebitsPrior,
coalesce( AllDebits.Debits, 0 ) Debits
coalesce( AllCredits.xCredits, 0 ) CreditsPrior,
coalesce( AllCredits.Credits, 0 ) Credits
from
customers c
LEFT JOIN
( select t.CustomerID,
sum( case when t.[Date] < #StartDate then Amount else 0 end ) xDebits,
sum( case when t.[Date] >= #StartDate then Amount else 0 end ) Debits
from
customers c1
JOIN Transactions t
on c1.CustomerID = t.CustomerID
where
c1.BillingCategory in (0,1,2,3,4,5)
group by
t.CustomerID ) AllDebits
on c.CustomerID = AllDebits.CustomerID
LEFT JOIN
( select p.CustomerID,
sum( case when p.[Date] < #StartDate then Amount else 0 end ) xCredits,
sum( case when p.[Date] >= #StartDate then Amount else 0 end ) Credits
from
customers c1
JOIN Payments p
on c1.CustomerID = p.CustomerID
where
c1.BillingCategory in (0,1,2,3,4,5)
group by
p.CustomerID ) AllCredits
on c.CustomerID = AllCredits.CustomerID
where
c.BillingCategory in (0,1,2,3,4,5)
COMMENT ADDITION
With respect to Thomas's answer, yes they are close. My version also adds the join to the customer table for the specific billing category and here is why. I don't know the size of your database, how many customers, how many transactions. If you are dealing with a large amount that DOES have performance impact, Thomas's version is querying EVERY customer and EVERY transaction. My version is only querying the qualified customers by the billing category criteria you limited.
Again, not knowing data size, if you are dealing with 100k records may be no noticeable performance. If you are dealing with 100k CUSTOMERS, could be a totally different story.
#JonathanWood, correct, but my version has each internal subquery inclusive of the cus

how to use multiple sum and group by in subquery?

ER DIAGRAM SNAP have to Find the Order Amount Total for 2 type of orders for each year.
table is SalesOrderHeader
Subtotal + TaxAmt gives the total order amount
OnlineOrderFlag(0/1 is direct/online respectively)
1>Display the results in the below formats
OrderYear Direct Online
2001 75423 344344
2002
2003
2004
SQL:
select year(a.OrderDate),
( select SUM(SubTotal+TaxAmt) FROM SalesOrderHeader b WHERE OnlineOrderFlag = 0 group by year(b.OrderDate) ) as tot ,
( select SUM(SubTotal+TaxAmt) FROM SalesOrderHeader c WHERE OnlineOrderFlag = 1 group by year(c.OrderDate) ) as tt
FROM SalesOrderHeader a inner join SalesOrderHeader b
on b.SalesOrderID = a.SalesOrderID
inner join
SalesOrderHeader c on c.SalesOrderID = a.SalesOrderID
can someone please tel how to proceed further? i'm stuck at this
2> and also how to find it in the below format ?
OnlineFlag Year TotalAmt
Direct 2001
Direct 2002
Direct 2003
Direct 2004
Online 2001
Online 2002
Online 2003
Online 2004
This should work. Added the inner subquery to make it clear where the group by needs to go and to add the subtotal and tax for each record before summarizing.
select a.year
,SUM(Case when a.OnlineOrderFlag = 1 THEN a.total else null end) as Direct
,SUM(Case when a.OnlineOrderFlag = 0 THEN a.total else null end) as Online
FROM (select year(OrderDate) as year, (SubTotal+TaxAmt) as total, OnlineOrderFlag
from SalesOrderHeader) a
Group by a.year
SELECT YEAR(ORDERDATE), ONLINEORDERFLAG,
SUM(SUBTOTAL) SUBTOTAL,SUM(TAXAMT) TAXAMT,
SUM(SUBTOTAL + TAXAMT) TOTALDUE
FROM SALESORDERHEADER
WHERE ONLINEORDERFLAG = 1
GROUP BY YEAR(ORDERDATE) WITH ROLLUP
UNION
SELECT YEAR(ORDERDATE), ONLINEORDERFLAG,
SUM(SUBTOTAL) ,SUM(TAXAMT),
SUM(SUBTOTAL + TAXAMT)
FROM SALESORDERHEADER
WHERE ONLINEORDERFLAG = 0
GROUP BY YEAR(ORDERDATE) WITH ROLLUP
;

how to return total sold p / year for several years in columnar format?

The SQL below give me the columns for Account, Name and the Total Sale for the year of 2015.But how, if possible, can I add another column for the previous year of 2014 ?
select a.AcctNo, b.Name, Sum(a.TotSold) as [ Total Sold ]
from Orders as A
Join Accounts as b on a.AcctNo = b.AcctNo
where (a.PurchaseDate between '1/1/2015' and '12/31/2015' )
Group by a.AcctNo, b.Name
You can use conditional aggregation:
select o.AcctNo, a.Name,
Sum(case when year(PurchaseDate) = 2015 then TotSold else 0 end) as tot_2015,
Sum(case when year(PurchaseDate) = 2014 then TotSold else 0 end) as tot_2014
from Orders o Join
Accounts a
on a.AcctNo = o.AcctNo
where PurchaseDate >= '2014-01-01'
PurchaseDate < '2016-01-01'
Group by o.AcctNo, a.Name ;
Notes:
Use ISO standard formats for date constants.
When using dates, try not to use between. It doesn't work as expected when there is a time component. The above inequalities work regardless of a time component.
Use table aliases that are abbreviations of the table name. That makes the query easier to follow.

SQL query of sales by customer over multiple years for top 50 customers in one year

I have a query in SQL server that successfully returns the top 50 customers for a given year by sales. I want to expand it to return their sales for the additional years when they may or may not be in the top 50.
SELECT TOP 50 CU.CustomerName, SUM(ART.SalesAnalysis) AS '2011'
FROM ARTransaction AS ART, Customer AS CU
WHERE ART.CustomerN = CU.CustomerN AND ART.PostingDate BETWEEN '2010-12-31' AND '2012-01-01'
GROUP By CU.CustomerName
ORDER BY SUM(ART.SalesAnalysis) DESC
I tried adding nested SELECT statements but they return strange results and I'm not sure why (might not ever work, but the results have me flabbergasted anyway). When included the values of every row is changed and customers are duplicated.
(SELECT SUM(ART.SalesAnalysis)
WHERE ART.PostingDate BETWEEN '2011-12-31' AND '2013-01-01') AS '2012'
I tried to put a TOP statement in a nested SELECT in HAVING but that tells me
"Msg 8114, Level 16, State 5, Line 1
Error converting data type varchar to numeric."
SELECT CU.CustomerName, SUM(ART.SalesAnalysis) AS '2011'
FROM ARTransaction AS ART
JOIN Customer AS CU ON ART.CustomerN = CU.CustomerN
GROUP BY CU.CustomerNAme
HAVING CU.CustomerNAme IN
(SELECT TOP 50 CU.CustomerName
FROM ARTransaction
JOIN Customer ON ARTransaction.CustomerN = Customer.CustomerN
WHERE ARTransaction.SalesAnalysis BETWEEN '2010-12-31' AND '2012-01-01'
GROUP BY Customer.CustomerN
ORDER BY SUM(ART.SalesAnalysis) DESC)
If I understand correctly, you are looking for the top 50 sales for customers based on 2011 data - and want to see all years data for those top 50 from 2011, regardless of those customers being in the top 50 for other years.
Try this, it might need to be tweaked a bit as I don't know the schema, but if I understand the question correctly, this should do the trick.
WITH Top50 AS (
SELECT TOP 50
CU.CustomerN
,SUM(ART.SalesAnalysis) AS SalesTotal
FROM
ARTransaction art
INNER JOIN
Customer cu
ON cu.CustomerN = art.CustomerN
WHERE
ART.PostingDate BETWEEN CAST('2011-01-01' AS DATETIME)
AND CAST('2011-12-31' AS DATETIME)
GROUP BY
CU.CustomerN
ORDER BY
SUM(ART.SalesAnalysis) DESC)
SELECT
c.CustomerName
,SUM(a.SalesAnalysis) AS TotalSales
,YEAR(a.PostingDate) AS PostingDateYear
FROM
ARTransaction a
INNER JOIN
Customer c
ON c.CustomerN = a.CustomerN
INNER JOIN
Top50 t
ON t.CustomerN = a.CustomerN
GROUP BY
c.CustomerName
,YEAR(a.PostingDate)
ORDER BY
PostingDateYear
you could use something like below... you will be looking at the all the data for all the years you want to look at and then just getting the top 50 for the 2011 year
SELECT TOP 50
CU.CustomerName,
SUM(case when year(ART.PostingDate) = 2011 -- or you could use case when ART.PostingDate BETWEEN '2011-01-01' AND '2011-12-31'
then ART.SalesAnalysis
else 0 end) AS 2011,
SUM(case when year(ART.PostingDate) = 2012
then ART.SalesAnalysis
else 0 end) AS 2012
FROM
ARTransaction ART,
inner join Customer CU
on ART.CustomerN = CU.CustomerN
WHERE ART.PostingDate BETWEEN '2011-01-01 AND '2012-12-31'
GROUP By CU.CustomerName
ORDER BY
SUM(case when year(ART.PostingDate) = 2011
then ART.SalesAnalysis
else 0 end) DESC