简体   繁体   English

SQL 从给定日期选择前 3 条记录

[英]SQL select first 3 records from given date

I have two tables namely Customers and Transactions.我有两个表,即客户和交易。 There is offer that starts from the 1st of the current month.有从当月 1 日开始的报价。

  1. New Customers (created after 1st of this month) will get 100% cashback for their first transaction.新客户(在本月 1 日之后创建)将在第一笔交易中获得 100% 的现金返还。
  2. Old Customers (created before 1st of this month) get 50% on their first and second transaction since 1st of this month.老客户(在本月 1 日之前创建)自本月 1 日以来的第一次和第二次交易获得 50%。
  3. Customers in 1st point of the offer are eligible for second point as well.优惠第一点的客户也有资格获得第二点。

Customers Table客户表

Customer_ID   Email       Created_Date
1             abc@g.com   2015-08-14 12:25:55
2             xyz@s.com   2016-01-23 18:16:34
.
.
n             ags@h.com   2016-05-05 23:25:43

Transactions Table交易表

Trans_ID      Customer_ID   Trans_Date            Amount
asd654qwe     2             2015-09-25 13:15:56   1200
dfg123xcv     56            2016-03-22 21:26:52   100
.
.
rty321cvb     4125          2016-05-05 08:42:06   500

I need to select only first 3 transactions of all customers after 1st of current month and then if the customer is new his first transaction is eligible for 100% cashback.我只需要选择当月 1 日之后所有客户的前 3 笔交易,然后如果客户是新客户,他的第一笔交易有资格获得 100% 现金返还。 New customers second and third transaction is eligible for 50% cashback.新客户第二次和第三次交易有资格获得50%的现金返还。 If customer is old his first and second transaction after 1st of this month is eligible for 50% cashback each.如果客户老了,他在本月 1 日之后的第一次和第二次交易有资格获得 50% 的现金返还。

I need to generate the report on daily basis for transaction done yesterday and share it to accounts team.我需要每天为昨天完成的交易生成报告,并将其分享给客户团队。 SQL is not my primary task and due to shortage i need to look into it. SQL 不是我的主要任务,由于短缺,我需要研究它。 I am doing all this manually using excel.我正在使用 excel 手动完成所有这些操作。 It is very time consuming.这是非常耗时的。 Can anyone please let me know a query that could give me the expected results?任何人都可以让我知道一个可以给我预期结果的查询吗?

Expected Result预期结果

c.Email   c.Created_Date  t.Trans_ID   t.Trans_Date   t.Amount   Offer_Type
record    record          record       record         record     First
record    record          record       record         record     Repeate
record    record          record       record         record     Repeate

Is this what you're looking for?这是你要找的吗?

 SET @limit3 := 0, @cust := '';

 --outer query determines offer type and limits the offer to three transactions per customer
 SELECT Email, Created_Date, Trans_ID, Trans_Date, Amount
 CASE WHEN Created_Date > First_Day THEN 'First'
      ELSE 'Repeate'
 END CASE AS Offer_Type,
 @limit3 := if (@cust = Customer_ID, @limit3 + 1, 1) AS rowcount,
 @cust := Customer_ID


 --Inner query selects applicable fields, creates First_Day field, 
 --filters it to transactions done yesterday
 FROM
 (
   SELECT c.Email, c.Created_Date, t.Trans_ID, t.Trans_Date, t.Amount, c.Customer_ID
      DATE_ADD(LAST_DAY(c.Created_Date), interval 1 DAY), interval -1 MONTH) as First_Day
   FROM Customers c
   JOIN Transactions t
   ON c.Customer_ID = t.Customer_ID
   WHERE DATE(Trans_Date) = SUBDATE(NOW(), 1)
 )  AS sub

 GROUP BY c.Customer_ID
 HAVING rowcount <= 3
 ORDER BY c.Created_Date

Sort of convoluted to do in mysql在 mysql 中要做的事情有点复杂

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM