Sql server Sql server查询澄清

Sql server Sql server查询澄清,sql-server,Sql Server,我有下面这样的桌子 Txn_Id Txn_Type ___________________ 1 101 1 102 1 103 1 104 2 101 2 102 2 104 3 101 3 104 我想要只有101和104型txn_的行。例如,对于上述数据,我应该只获得Txn_Id“3” 我像下面这样试了试,得到了结果。是否可以通过单个查询来实

我有下面这样的桌子

Txn_Id    Txn_Type
___________________
1         101
1         102
1         103
1         104
2         101
2         102
2         104
3         101
3         104
我想要只有101和104型txn_的行。例如,对于上述数据,我应该只获得Txn_Id“3”

我像下面这样试了试,得到了结果。是否可以通过单个查询来实现这一点

Select txn_id from Txn where txn_id in (Select txn_id from Txn where txn_id = 101) and txn_id =104.
选择2

    Select txn_id from Txn where (txn_type = 101 OR txn_type=104)
只得到“3”

选择2

    Select txn_id from Txn where (txn_type = 101 OR txn_type=104)
只得到“3”


您好,根据您的上述评论,您只需要txn_id=3(最大值)

请在下面查找代码

DECLARE @Table1  TABLE 
    (txn_id int, Txn_Type int)
;

INSERT INTO @Table1
     (txn_id , Txn_Type )
VALUES
    (1, 101),
    (1, 102),
    (1, 103),
    (1, 104),
    (2, 101),
    (2, 102),
    (2, 104),
    (3, 101),
    (3, 104)
  ;


Select max(txn_id ),Txn_Type 
  from @Table1 where item in (101,104)
  group by Txn_Type 

您好,根据您的上述评论,您只需要txn_id=3(最大值)

请在下面查找代码

DECLARE @Table1  TABLE 
    (txn_id int, Txn_Type int)
;

INSERT INTO @Table1
     (txn_id , Txn_Type )
VALUES
    (1, 101),
    (1, 102),
    (1, 103),
    (1, 104),
    (2, 101),
    (2, 102),
    (2, 104),
    (3, 101),
    (3, 104)
  ;


Select max(txn_id ),Txn_Type 
  from @Table1 where item in (101,104)
  group by Txn_Type 

正如balaji指出的,@Ayush解决方案是不灵活的,因为如果您(例如)在表(4101)和(4104)中添加另一对记录,@Ayush解决方案将返回错误的结果。在IMO中,您必须将表连接到自身以进行一些筛选,例如:

DECLARE @Table1  TABLE 
    (txn_id int, Txn_Type int);

INSERT INTO @Table1
     (txn_id , Txn_Type )
VALUES
    (1, 101),
    (1, 102),
    (1, 103),
    (1, 104),
    (2, 101),
    (2, 102),
    (2, 104),
    (3, 101),
    (3, 104),
    (4, 101),
    (4, 104);

select t1.*
from @Table1 t1
inner join (select txn_id, count(*) as total
        from @Table1 
        group by Txn_id
        having count(*) < 3
        ) t2 on t2.txn_id = t1.txn_id
where t1.Txn_Type in (101,104)
DECLARE@Table1表
(txn_id int,txn_Type int);
插入@Table1
(txn_id,txn_类型)
价值观
(1, 101),
(1, 102),
(1, 103),
(1, 104),
(2, 101),
(2, 102),
(2, 104),
(3, 101),
(3, 104),
(4, 101),
(4, 104);
选择t1*
来自@Table1 t1
内部联接(选择txn_id,计数(*)作为总计
来自@Table1
按Txn\U id分组
计数(*)小于3的
)t2上的t2.txn\u id=t1.txn\u id
其中t1.Txn_输入(101104)

正如balaji指出的,@Ayush解决方案是不灵活的,因为如果您(例如)在表(4101)和(4104)中添加另一对记录,将返回错误的结果。在IMO中,您必须将表连接到自身以进行一些筛选,例如:

DECLARE @Table1  TABLE 
    (txn_id int, Txn_Type int);

INSERT INTO @Table1
     (txn_id , Txn_Type )
VALUES
    (1, 101),
    (1, 102),
    (1, 103),
    (1, 104),
    (2, 101),
    (2, 102),
    (2, 104),
    (3, 101),
    (3, 104),
    (4, 101),
    (4, 104);

select t1.*
from @Table1 t1
inner join (select txn_id, count(*) as total
        from @Table1 
        group by Txn_id
        having count(*) < 3
        ) t2 on t2.txn_id = t1.txn_id
where t1.Txn_Type in (101,104)
DECLARE@Table1表
(txn_id int,txn_Type int);
插入@Table1
(txn_id,txn_类型)
价值观
(1, 101),
(1, 102),
(1, 103),
(1, 104),
(2, 101),
(2, 102),
(2, 104),
(3, 101),
(3, 104),
(4, 101),
(4, 104);
选择t1*
来自@Table1 t1
内部联接(选择txn_id,计数(*)作为总计
来自@Table1
按Txn\U id分组
计数(*)小于3的
)t2上的t2.txn\u id=t1.txn\u id
其中t1.Txn_输入(101104)

我觉得OP想要那些只有txn_101和104型的行。生成的txn_id中不应包含任何其他txn_id。我们运行您的查询,您将获得所有txn_id(即1,2,3)。但是OP只需要txn_id 3。是的,我只需要txn_id 3。我觉得OP需要那些只有txn_类型101和104的行。结果txn\u id不应包含任何其他txn\u id。我们您运行您的查询,您得到所有txn_id(即1,2,3)。但OP只需要txn_id 3。是的,我只需要txn_id 3。-请参阅下面的注释并澄清我是否正确?-请参阅下面的注释并澄清我是否正确?巧合的是,此解决方案可能有效,但如果txn_id 2只有txn_类型101104怎么办。在这种情况下,您的查询不会返回所需的输出…只是想知道如何通过两次联接表来实现,但我确实理解OP希望它位于单个查询中…巧合的是,此解决方案可能会起作用,但如果txn_id 2只有txn_类型101104会怎么样。在这种情况下,您的查询不会返回所需的输出…只是想知道如何通过两次连接表来实现,但我确实理解OP希望它位于单个查询中。。。。