Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.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 server 从一组相关记录中不存在值的表中选择行_Sql Server - Fatal编程技术网

Sql server 从一组相关记录中不存在值的表中选择行

Sql server 从一组相关记录中不存在值的表中选择行,sql-server,Sql Server,很抱歉没有提供任何SQL,我的SQL技能是基本的,我以前从未遇到过这种需求 使用如下表所示的表格: 桌子 如何构造返回以下结果的SQL语句 结果 记录按产品代码分组。在每个组中,我只寻找那些不包含大小为'92'的组。。。并希望显示每个组中大小为“91”的单个记录。91号始终存在于每个组中 这里有一个使用not exists的通用版本: 您正在查找不存在于上的不存在 不确定MySql,但这将适用于Sql server: SELECT T1.Id, T1.Size, T1.BatchCode, T1

很抱歉没有提供任何SQL,我的SQL技能是基本的,我以前从未遇到过这种需求

使用如下表所示的表格:

桌子

如何构造返回以下结果的SQL语句

结果


记录按产品代码分组。在每个组中,我只寻找那些不包含大小为'92'的组。。。并希望显示每个组中大小为“91”的单个记录。91号始终存在于每个组中

这里有一个使用not exists的通用版本:

您正在查找不存在于上的不存在


不确定MySql,但这将适用于Sql server:

SELECT T1.Id, T1.Size, T1.BatchCode, T1.ProductCode
FROM Table T1
LEFT JOIN
(
    SELECT ProductCode
    FROM Table 
    WHERE Size = 92
) T2 ON T1.ProductCode = T2.ProductCode 
WHERE T1.Size = 91
AND T2.ProductCode IS NULL

SQL Server或mysql?这一个将只提供来自每个组的一个完美,谢谢!我的代表支持率太低,无法投票,但也因为提供了SQL Fiddle演示而备受赞誉。
id    size   batch code   product code
--------------------------------------
1       91      55555       BigD Red
7       91      33333       BigD Orange
select *
from yourtable y1
where size = 91 and not exists (
    select 1
    from yourtable y2
    where y1.productcode = y2.productcode and y2.size = 92)
select * 
  from myTable t
 where t.size=91
   and not exists (select 1 from myTable t1 
                    where t1.productCode=t2.productCode
                      and t1.size=92)
SELECT T1.Id, T1.Size, T1.BatchCode, T1.ProductCode
FROM Table T1
LEFT JOIN
(
    SELECT ProductCode
    FROM Table 
    WHERE Size = 92
) T2 ON T1.ProductCode = T2.ProductCode 
WHERE T1.Size = 91
AND T2.ProductCode IS NULL
Select * from myTable t
Where id =
   (Select Min(id) from myTable   -- < need this to ensure only one 
    where product code = t.product code
        and size = 91 
        and not exists 
           (Select * from myTable
            Where product code = t.product code
               and size = 92))
select * from myTable where size!=92
group by productcode having size=91