Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 2.83 KB | None | 0 0
  1. declare @fromdate datetime, @todate datetime
  2.  
  3. set @fromdate = '7 February 2011'
  4. set @todate = DATEADD(dd, 7, @fromdate)
  5.  
  6. select
  7.     *
  8.     into #tmp
  9. from (
  10. select
  11.     [Date]
  12.     , s.Id as StoreId
  13.     , s.Name as StoreName
  14.     , o.TotalPrice
  15.     , CASE
  16.         WHEN o.OrderStatusId = 2 THEN 'Y'
  17.         ELSE 'N'
  18.       END AS Success
  19.     , o.OrderStatusId
  20.     , CASE WHEN o.IsMobileDevice = 1 THEN 'Y' ELSE 'N' END AS IsMobileDevice
  21.     , u.Fullname as UserName
  22.     , u.MobileNumber as CellNumber
  23.     , u.EmailAddress as EmailAddress
  24.     , CASE WHEN COALESCE(o.Instructions, '') LIKE '%test%' THEN 'Y' ELSE 'N' END AS IsTest
  25.     , o.Instructions
  26.     , o.CardPaymentId
  27.     , o.UserId
  28. from Orders o
  29. LEFT JOIN Stores s on o.StoreId = s.Id
  30. LEFT JOIN UserDetails u on o.UserId = u.UserId
  31. where Date > @fromdate
  32. and Date < @todate
  33. --order by Date Desc
  34. ) t
  35. where IsTest = 'N'
  36. AND OrderStatusId <> 3 -- pending card authorisation -> the user did not continue so these should be ignored
  37.  
  38. SELECT 'Total number of orders: ', COUNT(*) FROM #tmp
  39. UNION
  40. SELECT 'Total number of successful orders: ', COUNT(*) FROM #tmp WHERE Success = 'Y'
  41. UNION
  42. SELECT 'Total number of stores: ', COUNT(DISTINCT StoreName) FROM #tmp
  43. UNION
  44. SELECT 'Stores where errors occurred: ', COUNT(DISTINCT StoreName) FROM #tmp WHERE Success = 'N'
  45. UNION
  46. SELECT 'Credit card orders for the week: ', COUNT(*) FROM #tmp WHERE CardPaymentId is not null
  47. UNION
  48. SELECT 'Number of credit card orders that failed: ', COUNT(*) FROM #tmp WHERE CardPaymentId is not null AND Success = 'N'
  49. UNION
  50. SELECT 'Mobile orders for the week (successful only): ', COUNT(*) FROM #tmp WHERE IsMobileDevice = 'Y' AND Success = 'Y'
  51. UNION
  52. SELECT 'Web site orders for the week (successful only): ', COUNT(*) FROM #tmp WHERE IsMobileDevice = 'N' AND Success = 'Y'
  53. UNION
  54. SELECT 'Total registered users: ', COUNT(*) FROM UserDetails WHERE CreatedOn < @todate
  55. UNION
  56. SELECT 'Users registered this week: ', COUNT(*) FROM UserDetails WHERE CreatedOn > @fromDate and CreatedOn < @todate
  57. UNION
  58. SELECT 'New users who ordered this week: ', COUNT(*) FROM UserDetails WHERE CreatedOn > @fromDate and CreatedOn < @todate and UserId IN (Select UserId from #tmp)
  59. UNION
  60. SELECT 'Total order amount (successful only): ', SUM(TotalPrice) FROM #tmp WHERE Success = 'Y'
  61. UNION
  62. SELECT 'Average order amount (successful only): ', AVG(TotalPrice) FROM #tmp  WHERE Success = 'Y'
  63. UNION
  64. SELECT 'Average order amount (website, successful only): ', AVG(TotalPrice) FROM #tmp WHERE IsMobileDevice = 'N' AND Success = 'Y'
  65. UNION
  66. SELECT 'Average order amount (mobisite, successful only): ', AVG(TotalPrice) FROM #tmp WHERE IsMobileDevice = 'Y' AND Success = 'Y'
  67.  
  68. -- get the 3 busiest stores for the week
  69. SELECT TOP 3
  70. (select name from Stores where Id = t.StoreId) as StoreName,
  71. COUNT(*) as OrderCount
  72. from #tmp t
  73. WHERE Success = 'Y'
  74. group by StoreId
  75. order by COUNT(*) desc
  76.  
  77. drop table #tmp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement