-1

I have a table called Orders which looks like this:

Order_id         Integer    Ex: 10618834, 10618835
Customer_id      Integer    Ex: 231, 234
Order_Timestamp  Date       Ex: 10/25/2016 12:30:00 PM, 10/21/2016 2:32:31 PM
Order_status     Varchar    Ex: Success, Failure

I want to write the queries for these questions:

  1. write a SQL query to get the week on week count of distinct users, count of successful orders for last 6 weeks.

  2. write a SQL query to get the count of distinct users, who made their first successful order in April 2016 and made their 2nd successful order within 30 days from their first order date

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0

Following query might be useful to achieve your first point.

SELECT DISTINCT DATEPART(week,Order_TimeStamp) AS [Week],count( DISTINCT Customer_id) AS Users,count(*) AS Orders FROM Orders WHERE Order_TimeStamp >=DATEADD(WEEK, -6, GETDATE()) AND dbo.Orders.order_status='success' GROUP BY DATEPART(week,Order_TimeStamp)