简体   繁体   English

SQL 首尾交易报告

[英]SQL First and Last Transaction Report

I have a table in SQL, that looks a little similar to the table below:我在 SQL 有一张表,看起来有点类似于下表:

[enter image description here] [在此输入图片描述]1个

My goal is to generate a first and Last Transaction report.我的目标是生成第一个和最后一个交易报告。 I want to know when did customer X make their first purchase and what is the date of their most recent purchase.我想知道客户 X 是什么时候第一次购买的,以及他们最近一次购买的日期是什么时候。 I would like to group my results by Store and add all the transaction if they happen on the same day.我想按商店对我的结果进行分组,如果它们发生在同一天,则添加所有交易。

For instance, if John do made 2 expenses at walmart on Jan 15th and that's their most recent transaction, I would like those two expenses to be Summed in my report.例如,如果约翰在 1 月 15 日确实在沃尔玛支付了 2 笔费用,这是他们最近的交易,我希望在我的报告中汇总这两项费用。

Here is the final result I'd expect from a table like on the example above:这是我期望从上例中的表格中得到的最终结果:

[enter image description here] [在此输入图片描述]2个

With what I have tried so far, I am only getting 1 value back到目前为止我已经尝试过,我只得到 1 个值

The SQL looks a little similar to SQL 看起来有点类似于

Select 
SN
, SID
, CustomerName
, BankAccount
, Min(TransDate)
, Max(TransDate)
, price
, store
From transaction
GROUp by 
SN
, SID
, CustomerName
, BankAccount
, Min(TransDate)
, Max(TransDate)
, price
, store

I know I have to use some types of nested query to get the result(maybe) but I have been unsuccessful.我知道我必须使用某些类型的嵌套查询来获取结果(可能),但我一直没有成功。

I have a table in SQL, that looks a little similar to the table below:我在 SQL 中有一张表,看起来有点类似于下表:

[enter image description here] [在此处输入图像描述]1

My goal is to generate a first and Last Transaction report.我的目标是生成第一个和最后一个交易报告。 I want to know when did customer X make their first purchase and what is the date of their most recent purchase.我想知道客户 X 什么时候第一次购买以及他们最近一次购买的日期是什么时候。 I would like to group my results by Store and add all the transaction if they happen on the same day.我想按商店对我的结果进行分组,并添加所有交易(如果它们发生在同一天)。

For instance, if John do made 2 expenses at walmart on Jan 15th and that's their most recent transaction, I would like those two expenses to be Summed in my report.例如,如果约翰在 1 月 15 日确实在沃尔玛支付了 2 笔费用,这是他们最近的一笔交易,我希望将这两项费用汇总在我的报告中。

Here is the final result I'd expect from a table like on the example above:这是我希望从上面示例中的表格中得到的最终结果:

[enter image description here] [在此处输入图像描述]2

With what I have tried so far, I am only getting 1 value back到目前为止,我已经尝试过,我只获得了 1 个价值

The SQL looks a little similar to SQL 看起来有点像

Select 
SN
, SID
, CustomerName
, BankAccount
, Min(TransDate)
, Max(TransDate)
, price
, store
From transaction
GROUp by 
SN
, SID
, CustomerName
, BankAccount
, Min(TransDate)
, Max(TransDate)
, price
, store

I know I have to use some types of nested query to get the result(maybe) but I have been unsuccessful.我知道我必须使用某些类型的嵌套查询来获得结果(也许),但我没有成功。

I would try and pull out the customer info you need to filter out the rest of the data.我会尝试提取您需要的客户信息,以过滤掉数据的 rest。 I would start with a CTE to get the MIN and MAX dates for each customer.我将从 CTE 开始,以获取每个客户的 MIN 和 MAX 日期。 I would then JOIN that back up to the original table and just get the records for the Customer that are on that MAX date.然后我会 JOIN 备份到原始表并只获取该 MAX 日期的客户记录。

I would assume you have something that is more Unique then the customer name, and if so I would use that instead.我会假设您有比客户名称更独特的东西,如果是这样,我会改用它。 But here is what I came up with given the data you provided:但这是我根据您提供的数据得出的结论:

CREATE TABLE #tmp(SerialNumber int, SearialID int,CustomerName varchar(100), BankAccount int, 
                    TranDate date, Price decimal(10,2),TracerID int,Store varchar(20))

INSERT INTO #tmp VALUES
(2,2,'Peter Smith',14564,'1/1/2021',10,756,'Kroger'),
(1,1,'John Do',12345,'1/1/2021',10,156,'Walmart'),
(1,1,'John Do',12345,'1/15/2021',5,148,'Walmart'),
(1,1,'John Do',12345,'1/15/2021',15,148,'Walmart'),
(2,2,'Peter Smith',14564,'1/12/2021',12,756,'Kroger')

;WITH CTE AS
    (
    SELECT Min(TranDate) FirstDate, Max(TranDate) LastDate, CustomerName
    FROM #tmp
    GROUP BY CustomerName
    )
SELECT T.SerialNumber,T.SearialID,T.CustomerName,t.BankAccount,C.FirstDate FirstTrans, C.LastDate,SUM(t.Price) Price,T.Store 
    FROM #tmp T
    INNER JOIN CTE C on t.CustomerName = c.CustomerName and t.TranDate = c.LastDate
GROUP BY T.SerialNumber,T.SearialID,T.CustomerName,t.BankAccount,C.FirstDate,C.LastDate,T.Store 

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

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