Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 仅计算特殊行的数据_Sql_Sql Server - Fatal编程技术网

Sql 仅计算特殊行的数据

Sql 仅计算特殊行的数据,sql,sql-server,Sql,Sql Server,我有一张桌子: ID count OperationID 100 111 1 100 55 2 100 55 3 100 66 4 99 69 1 99 33 2 99 11 14 98 33 1 98 47 2 必须显示每个“ID”中的“OperationID”的数据 例如:产出: ID count Op

我有一张桌子:

ID    count  OperationID 
100   111       1
100    55       2
100    55       3
100    66       4 
99     69       1
99     33       2
99     11       14
98     33       1
98     47       2
必须显示每个“ID”中的“OperationID”的数据

例如:产出:

ID    count  OperationID 
100   111       1
100    55       2
99     69       1
99     33       2
98     33       1
98     47       2
也许可以使用join table自身帮助?但它不起作用…

您可以使用它

DECLARE @MyTable TABLE ( ID INT, [count] INT,OperationID  INT)

INSERT INTO @MyTable
VALUES
(100,111,1),
(100,55,2),
(100,55,3),
(100,66,4 ),
(99,69,1),
(99,33,2),
(99,11,14),
(98,33,1),
(98,47,2)

SELECT  * 
FROM @MyTable 
WHERE 
    OperationID IN 
    (SELECT 
        OperationID 
    FROM @MyTable
    GROUP BY OperationID
    HAVING COUNT(*) = (SELECT COUNT(DISTINCT ID) FROM @MyTable)
 )
结果:

ID          count       OperationID
----------- ----------- -----------
100         111         1
100         55          2
99          69          1
99          33          2
98          33          1
98          47          2

好的,您可以试试这个,但是在您的数据集中有一些考虑事项,例如,如果有一行没有公共OpID,该怎么办

declare @tbl table (ID int,Cnt int, OperationID int)
insert into @tbl
select  100,111,1 union
select  100, 55,2 union
select  100, 55,3 union
select  100, 66,4 union 
select  99 , 69,1 union
select  99 , 33,2 union
select  99 , 11,14 union
select  98 , 33,1 union
select  98,47,2

 select * from @tbl where OperationID in (
 select OperationID from @tbl
 where id in (
select a.id from 
(
select top 1 id, count (distinct OperationID) as opCnt from @tbl
group by id
order by opCnt
) a
)
)
结果

98  33  1
98  47  2
99  33  2
99  69  1
100 55  2
100 111 1

我相信这不是最好的办法

with cte1 as (
select distinct id
from Operations
), 

cte2 as (
select distinct operationID
from Operations
)
, cte3 as (
select *
from cte1 cross join cte2
)

SELECT *
FROM operations
WHERE operationid NOT IN(

SELECT cte3.operationid
FROM operations a
right JOIN cte3 ON cte3.id = a.id
AND cte3.operationid = a.operationid
WHERE a.id IS NULL) 
你可以试试

DECLARE @table TABLE ( id INT, cnt INT, operationid INT ) 

INSERT INTO @table VALUES
 (100,111 , 1 ) 
,(100, 55 , 2 ) 
,(100, 55 , 3 ) 
,(100, 66 , 4 ) 
,(99 , 69 , 1 ) 
,(99 , 33 , 2 ) 
,(99 , 11 , 14)  
,(98 , 33 , 1 )  
,(98 , 47 , 2 )

;WITH cte
AS (
SELECT
    Count(distinct id)  TotalId
FROM
    @table
)
,cte1  as 
(select 
 operationid
,Count( distinct id)  operationid_By_Id
FROM
    @table
Group by
operationid )
SELECT * From 
@table c
WHERE EXISTS (
SELECT 1 FROM (
    SELECT 
        * 
    FROM cte1 A 
    WHERE EXISTS (SELECT 1 FROM cte b WHERE a.operationid_By_Id = b.TotalId) 
    ) d where d.operationid = c.operationid)
Order by id desc , operationid

为什么结果集中包含
OperationId=1
?它不在
id
90中。对不起,这是我的错误。98而不是90。我修正了你到目前为止所做的尝试,以及你得到的错误或意外结果。请告诉我一些想法。我不知道(