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/9/2024 3:54:37 PM ******/
  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. ),
  93. TransactionCounts AS (
  94. SELECT cm.customerId,
  95. ISNULL(COUNT(ts.tranId), 0) AS txnCount,
  96. MAX(rt.createdDate) AS lastTxnDate
  97. FROM CustomerDetails cm
  98. LEFT JOIN vwTranSenders ts ON cm.customerId = ts.customerId
  99. LEFT JOIN vwRemitTran rt ON ISNULL(rt.id, rt.holdTranId) = ts.tranId
  100. GROUP BY cm.customerId
  101. ),
  102. TranDetails AS (
  103. SELECT cd.customerId AS customerId,
  104. cd.membershipId,
  105. cd.Address1,
  106. cd.fullName,
  107. cd.email,
  108. cd.mobile,
  109. cd.zipCode,
  110. cd.createdDate,
  111. ISNULL(tc.txnCount, 0) AS txnCount,
  112. tc.lastTxnDate
  113. FROM CustomerDetails cd
  114. LEFT JOIN TransactionCounts tc ON cd.customerId = tc.customerId
  115. )
  116. SELECT ROW_NUMBER() OVER (ORDER BY txnCount DESC) AS SN,
  117. membershipId,
  118. fullName,
  119. email,
  120. mobile,
  121. zipCode PostCode,
  122. Address1,
  123. createdDate,
  124. txnCount,
  125. lastTxnDate
  126. FROM TranDetails
  127. WHERE (lastTxnDate <= CONVERT(DATE, '''+ @toDate +''') OR lastTxnDate IS NULL) ORDER BY lastTxnDate DESC'
  128. IF ISNULL(@postCode,'') <> ''
  129. BEGIN
  130. SET @postCode = REPLACE(@postCode, ' ', '')
  131. SET @sql = @sql + ' AND (LEFT(zipCode, 3) = LEFT(''' + @postCode + ''', 3) OR zipCode = ''' + @postCode + ''')'
  132. END
  133. IF ISNULL(@txnCountType,'') <> ''
  134. BEGIN
  135. SET @sql = @sql + ' AND txnCount ' + @txnCountType + ' ' + CAST(@txnCount AS VARCHAR(10))
  136. END
  137. PRINT(@sql);
  138. --EXEC(@sql);
  139. EXEC sp_executesql @sql,
  140. N'@toDate VARCHAR(10), @postCode VARCHAR(100), @txnCount INT',
  141. @toDate,
  142. @postCode,
  143. @txnCount;
  144. EXEC proc_errorHandler '0'
  145. ,'Report has been prepared successfully.'
  146. ,NULL
  147. SELECT 'To Date' head
  148. ,@toDate VALUE
  149. SELECT 'Low Txn report' title
  150. END