SQL Server - 计数等于另一个表混淆的计数

时间:2018-01-13 13:13:42

标签: sql-server tsql

我无法理解“有计数”这个基本概念。

我们有两个表格如下:

declare @t1 table (id int, num int, name varchar(10))
declare @t2 table (id int, name varchar(10))

insert into @t1 values (1, 10, 'a')
insert into @t1 values (2, 20, 'b')
insert into @t1 values (3, 30, 'c')

insert into @t1 values (4, 10, 'a')
insert into @t1 values (5, 20, 'b')

insert into @t1 values (8, 20, 'b')
insert into @t1 values (9, 30, 'c')


insert into @t2 values (10,'a')
insert into @t2 values (20,'b')
insert into @t2 values (30,'c')

所以它们包含:

table @ t1

id      num     name
-----------------------
1       10      a
2       20      b
3       30      c
4       10      a
5       20      b
8       20      b
9       30      c

table @ t2

id      name
-------------
10      a
20      b
30      c

所以现在我想要得到以下结果:

期望的结果

id      num     name
-----------------------
1       10      a
2       20      b
3       30      c

为此,我写了这五行代码:

select t1.id, t1.num, t1.name
from @t1 t1
inner join @t2 t2 on t2.name = t1.name
group by t1.id, t1.num, t1.name
having count(*) = (select count(*) from @t2)

但这个想法出了点问题,并没有回归任何一行 我想要的是找到包含@ t2名称值的三行@ t1。

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

此查询:

select t1.id, t1.num, t1.name, nm = count(*)
from @t1 t1
inner join @t2 t2 on t2.name = t1.name
group by t1.id, t1.num, t1.name

会给你这个结果:

id  num name cnt
----------------
1   10  a   1
2   20  b   1
3   30  c   1
4   10  a   1
5   20  b   1
8   20  b   1
9   30  c   1

正如您所看到的,您正在尝试查找cnt列等于表@ t2中的总行数的行,即3。这就是输出中没有行的原因。如果你想要问题中描述的输出,你应该写:

 ;with cte as
    (
    select t1.id
            , t1.num
            , t1.name
            , rn = row_number() over (partition by t1.name order by t1.id)
    from @t1 t1
    inner join @t2 t2 on t2.name = t1.name
    )
    select id
           , num
           , name
    from cte
    where rn = 1

此查询使用公用表表达式(cte),它是一个虚拟表,在执行查询时存储在操作内存中,还有一个over子句,您可以在此处阅读https://docs.microsoft.com/en-us/sql/t-sql/queries/select-over-clause-transact-sql

rn = row_number() over (partition by t1.name order by t1.id)
  • 此表达式将根据t1.id。
  • 排序的t1.name对您的行进行编号
相关问题