在sql中获取查询外部的变量

在sql中获取查询外部的变量,sql,Sql,我知道我的标题有点误导,但我不确定什么是好标题 Authored有两列,即ID、PubID 无论如何,我都可以将p输出到结果中 我想知道,对于每个各自的ID,PubID对,有多少行具有相同的PubID但不同的ID select a.authorId, P from Authored A WHERE 1 < (Select count(*) as P from Authored B where A.pubId = B.pubId AND A.authorId<> B.au

我知道我的标题有点误导,但我不确定什么是好标题

Authored有两列,即ID、PubID

无论如何,我都可以将p输出到结果中

我想知道,对于每个各自的ID,PubID对,有多少行具有相同的PubID但不同的ID

select a.authorId, P 
from Authored A 
WHERE 1 < 

(Select count(*) as P
from Authored B
where A.pubId = B.pubId
AND A.authorId<> B.authorId)

预期结果
请尝试以下MS Sql server查询:

select 
    *, 
    COUNT(*) over (partition by pubId) 
From Authored  
where authorId<>pubId
选择
*, 
计数(*)超过(按pubId划分)
来自作者
作者ubid在哪里
使用
count()over()更新

select a.AuthorId, count(*) over(partition by pubId) counts
from Authored a 
order by a.AuthorId;

这就是你的意思吗?不清楚你在问什么



你能展示一些数据和预期结果吗?
select 
    *, 
    COUNT(*) over (partition by pubId) 
From Authored  
where authorId<>pubId
select a.AuthorId, count(*) over(partition by pubId) counts
from Authored a 
order by a.AuthorId;
select
    count(ID) P,
    PubID
from
    (select distinct ID, pubID from Authored) d
group by
    PubID
This will give you the number of distinct `ID/authorId` for each `PubID\pubId`