同一天,同一客户但不同分行交易
Same day, same customer but different branch transactions
我有一个 table,其中包含帐号、交易日期、交易分行和金额。
我想生成一个包含以下信息的列:
if that specific customer, made a transaction from different branches on the same day.
结果示例如下:
AccountNumber Transaction_branch tran_Date Amount Different_Branch_Tran
11452 331 20/07/2020 500 no
11452 331 21/07/2020 500 no
5432 14 22/07/2020 500 no
5432 14 22/07/2020 500 no
11452 14 24/07/2020 500 yes
11452 420 24/07/2020 500 yes
11452 14 26/07/2020 500 no
我有一个代码可以显示客户是否在同一天进行了相同数量的交易。但是我不知道如何修改或更改此代码以获得我想要的结果。
select
a.*,
case when count(*) over(partition by trandate, accountnumber, amount) > 1 then 'Yes' else 'No' end SameAmountSameDay
from Tran_table a
where trandate> '20190701'
一种方法是使用 EXISTS
和子查询,因为您不能在窗口 COUNT
:
中使用 DISTINCT
SELECT TT.AccountNumber,
TT.Tranasction_branch,
TT.tran_date,
TT.Amount,
CASE WHEN EXISTS(SELECT 1
FROM dbo.Tran_table sq
WHERE sq.AccountNumber = TT.AccountNumber
AND sq.Tran_Date = TT.Tran_Date
AND sq.Tranasction_branch != TT.Transaction_branch) THEN 'Yes' ELSE 'No' END AS Different_Branch_Tran
FROM dbo.Tran_table TT
WHERE TT.trandate > '20190701'; --This is called Tran_Date in your sample data, are these different columns?
.....
case when
min(Transaction_branch) over(partition by AccountNumber, tran_Date, Amount)
=
max(Transaction_branch) over(partition by AccountNumber, tran_Date, Amount)
then 'No' else 'Yes'
end as SameAmountSameDayDifferentBranch
.........
我有一个 table,其中包含帐号、交易日期、交易分行和金额。
我想生成一个包含以下信息的列:
if that specific customer, made a transaction from different branches on the same day.
结果示例如下:
AccountNumber Transaction_branch tran_Date Amount Different_Branch_Tran
11452 331 20/07/2020 500 no
11452 331 21/07/2020 500 no
5432 14 22/07/2020 500 no
5432 14 22/07/2020 500 no
11452 14 24/07/2020 500 yes
11452 420 24/07/2020 500 yes
11452 14 26/07/2020 500 no
我有一个代码可以显示客户是否在同一天进行了相同数量的交易。但是我不知道如何修改或更改此代码以获得我想要的结果。
select
a.*,
case when count(*) over(partition by trandate, accountnumber, amount) > 1 then 'Yes' else 'No' end SameAmountSameDay
from Tran_table a
where trandate> '20190701'
一种方法是使用 EXISTS
和子查询,因为您不能在窗口 COUNT
:
DISTINCT
SELECT TT.AccountNumber,
TT.Tranasction_branch,
TT.tran_date,
TT.Amount,
CASE WHEN EXISTS(SELECT 1
FROM dbo.Tran_table sq
WHERE sq.AccountNumber = TT.AccountNumber
AND sq.Tran_Date = TT.Tran_Date
AND sq.Tranasction_branch != TT.Transaction_branch) THEN 'Yes' ELSE 'No' END AS Different_Branch_Tran
FROM dbo.Tran_table TT
WHERE TT.trandate > '20190701'; --This is called Tran_Date in your sample data, are these different columns?
.....
case when
min(Transaction_branch) over(partition by AccountNumber, tran_Date, Amount)
=
max(Transaction_branch) over(partition by AccountNumber, tran_Date, Amount)
then 'No' else 'Yes'
end as SameAmountSameDayDifferentBranch
.........