sql - 当数据字段组合与相同和不同列匹配时选择记录
问题描述
我有一张表,其中包含类似于以下的交易数据:
TransactionID |FirstTType |FirstTDate |SecondTType |SecondTDate |
T1 |A | 01/01/2019 |
T2 |C | 01/02/2019 |
T3 |E | 01/01/2019 |
T4 |D | 02/02/2019 |
T5 |C | 01/02/2019 |
T6 |Z | 01/02/2019 |
从上面的示例中,我在 T2 行中,FirstTType 和 FirstTDate 都具有与 T5 行相同的数据。
我希望得到以下回报
TransactionID |FirstTType |FirstTDate |SecondTType |SecondTDate |
T2 |C | 01/02/2019 |
T5 |C | 01/02/2019 |
我想知道在 SQL Server 中是否有一个好方法可以做到这一点
谢谢
解决方案
我想你想要exists
:
select t.*
from t
where exists (select 1
from t t2
where t2.FirstTType = t.FirstTType and
t2.FirstTDate = t.FirstTDate and
t2.TransactionID <> t.TransactionID
);
推荐阅读
- java - 我在使用 REST Assured API POST 请求时遇到问题
- database - 如何为sqlite中的层次结构数据逐级生成序列号?
- android - 使用导航抽屉和片段未出现工具栏菜单
- opengl - 与 glDrawArrays 相比,glDrawElements 会导致运行更少的顶点着色器吗?
- java - 组合框不显示数据库中的数据
- r - 如何使用 num_range 选择在一个特定列中都包含相同前 4 位数字的行?(希望使用 dplyr/tidyverse)
- python - 在 alpine docker 容器中安装 multidict 时如何修复 gcc 错误?
- android - 如何在屏幕上随机移动图像?
- sql - 执行顺序和哪个更快
- java - 如何在spring security oauth2中分离访问令牌和刷新令牌端点