You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
10 KiB

3 months ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_low_txn_report] Script Date: 4/20/2024 8:13:12 AM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. ALTER PROCEDURE [dbo].[proc_low_txn_report] @FLAG VARCHAR(20)
  9. ,@user VARCHAR(30)
  10. ,@toDate VARCHAR(100) = NULL
  11. ,@postCode VARCHAR(100) = NULL
  12. ,@txnCountType VARCHAR(20) = NULL
  13. ,@txnCount INT = NULL
  14. AS
  15. SET NOCOUNT ON;
  16. IF @FLAG = 'txn-report'
  17. BEGIN
  18. SET @txnCountType = CASE WHEN @txnCountType = 'gte' THEN '>='
  19. WHEN @txnCountType = 'lte' THEN '<='
  20. WHEN @txnCountType = 'eq' THEN '='
  21. WHEN @txnCountType = 'lt' THEN '<'
  22. WHEN @txnCountType = 'gt' THEN '>'
  23. ELSE NULL
  24. END;
  25. --WITH CustomerDetails
  26. --AS (
  27. -- SELECT customerId
  28. -- ,membershipId
  29. -- ,(ISNULL(firstName, '') + ' ' + ISNULL(middleName, '') + ' ' + ISNULL(lastName1, '')) AS fullName
  30. -- ,email
  31. -- ,mobile
  32. -- ,zipCode
  33. -- ,address + ', ' + ISNULL(city, '') AS Address1
  34. -- ,createdDate
  35. -- FROM customerMaster
  36. -- WHERE createdDate BETWEEN '2023-11-01'
  37. -- AND GETDATE()
  38. -- )
  39. -- ,TransactionCounts
  40. --AS (
  41. -- SELECT cm.customerId
  42. -- ,ISNULL(COUNT(ts.tranId), 0) AS txnCount
  43. -- ,MAX(rt.createdDate) AS lastTxnDate
  44. -- FROM CustomerDetails cm
  45. -- LEFT JOIN vwTranSenders ts ON cm.customerId = ts.customerId
  46. -- LEFT JOIN vwRemitTran rt ON ISNULL(rt.id, rt.holdTranId) = ts.tranId
  47. -- GROUP BY cm.customerId
  48. -- )
  49. -- ,TranDetails
  50. --AS (
  51. -- SELECT cd.customerId AS customerId
  52. -- ,cd.membershipId
  53. -- ,cd.Address1
  54. -- ,cd.fullName
  55. -- ,cd.email
  56. -- ,cd.mobile
  57. -- ,cd.zipCode
  58. -- ,cd.createdDate
  59. -- ,ISNULL(tc.txnCount, 0) AS txnCount
  60. -- ,tc.lastTxnDate
  61. -- FROM CustomerDetails cd
  62. -- LEFT JOIN TransactionCounts tc ON cd.customerId = tc.customerId
  63. -- )
  64. --SELECT ROW_NUMBER() OVER (
  65. -- ORDER BY txnCount DESC
  66. -- ) AS SN
  67. -- --,customerId
  68. -- ,membershipId
  69. -- ,fullName
  70. -- ,email
  71. -- ,mobile
  72. -- ,zipCode
  73. -- ,Address1
  74. -- ,createdDate
  75. -- ,txnCount
  76. -- ,lastTxnDate
  77. --FROM TranDetails
  78. --WHERE lastTxnDate <= CONVERT(DATE, @toDate)
  79. -- OR lastTxnDate IS NULL
  80. --ORDER BY txnCount DESC
  81. DECLARE @sql NVARCHAR(MAX);
  82. SET @sql = 'WITH CustomerDetails AS (
  83. SELECT customerId,
  84. membershipId,
  85. (ISNULL(firstName, '''') + '' '' + ISNULL(middleName, '''') + '' '' + ISNULL(lastName1, '''')) AS fullName,
  86. email,
  87. mobile,
  88. zipCode,
  89. address + '', '' + ISNULL(city, '''') AS Address1,
  90. createdDate
  91. FROM customerMaster
  92. WHERE createdDate BETWEEN ''2023-11-01'' AND GETDATE()
  93. ),
  94. TransactionCounts AS (
  95. SELECT cm.customerId,
  96. ISNULL(COUNT(ts.tranId), 0) AS txnCount,
  97. MAX(rt.createdDate) AS lastTxnDate
  98. FROM CustomerDetails cm
  99. LEFT JOIN vwTranSenders ts ON cm.customerId = ts.customerId
  100. LEFT JOIN vwRemitTran rt ON ISNULL(rt.id, rt.holdTranId) = ts.tranId
  101. GROUP BY cm.customerId
  102. ),
  103. TranDetails AS (
  104. SELECT cd.customerId AS customerId,
  105. cd.membershipId,
  106. cd.Address1,
  107. cd.fullName,
  108. cd.email,
  109. cd.mobile,
  110. cd.zipCode,
  111. cd.createdDate,
  112. ISNULL(tc.txnCount, 0) AS txnCount,
  113. tc.lastTxnDate
  114. FROM CustomerDetails cd
  115. LEFT JOIN TransactionCounts tc ON cd.customerId = tc.customerId
  116. )
  117. SELECT ROW_NUMBER() OVER (ORDER BY txnCount DESC) AS SN,
  118. membershipId,
  119. fullName,
  120. email,
  121. mobile,
  122. zipCode PostCode,
  123. Address1,
  124. createdDate,
  125. txnCount,
  126. lastTxnDate
  127. FROM TranDetails
  128. WHERE (lastTxnDate <= CONVERT(DATE, '''+ @toDate +''') OR lastTxnDate IS NULL) ORDER BY lastTxnDate DESC'
  129. IF ISNULL(@postCode,'') <> ''
  130. BEGIN
  131. SET @postCode = REPLACE(@postCode, ' ', '')
  132. SET @sql = @sql + ' AND (LEFT(zipCode, 3) = LEFT(''' + @postCode + ''', 3) OR zipCode = ''' + @postCode + ''')'
  133. END
  134. IF ISNULL(@txnCountType,'') <> ''
  135. BEGIN
  136. SET @sql = @sql + ' AND txnCount ' + @txnCountType + ' ' + CAST(@txnCount AS VARCHAR(10))
  137. END
  138. PRINT(@sql);
  139. --EXEC(@sql);
  140. EXEC sp_executesql @sql,
  141. N'@toDate VARCHAR(10), @postCode VARCHAR(100), @txnCount INT',
  142. @toDate,
  143. @postCode,
  144. @txnCount;
  145. EXEC proc_errorHandler '0'
  146. ,'Report has been prepared successfully.'
  147. ,NULL
  148. SELECT 'To Date' head
  149. ,@toDate VALUE
  150. SELECT 'Low Txn report' title
  151. END