SQL Server 2008,条件选择查询

时间:2017-03-31 14:41:13

标签: sql sql-server-2008

请帮我构建此查询。

我有一张这样的表:

Row Product   Role            
-------------------
1   ABC-1     Self
2   ABC-2     Self
3   ABC-2     Joint
4   ABC-3     Self
5   ABC-3     Joint
6   ABC-4     Self

我想只获取那些只有Role is Self的产品行。换句话说,从给定的表中,SQL查询应该只输出第1行和第1行。 6.输出应该如下所示

Row Product   Role            
-------------------
1   ABC-1     Self
6   ABC-4     Self

谢谢

2 个答案:

答案 0 :(得分:1)

您可以使用NOT EXISTS

SELECT *
FROM dbo.YourTable t
WHERE NOT EXISTS(SELECT 1 FROM dbo.YourTable
                 WHERE Product = t.Product
                 AND Role <> 'Self');

答案 1 :(得分:1)

您可以使用HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Transfer-Encoding: chunked Content-Type: application/vnd.ms-word Expires: -1 Server: Microsoft-IIS/10.0 Set-Cookie: ASP.NET_SessionId=ezjup0451sjuzmzb55; path=/; HttpOnly content-disposition: attachment;filename=test.docx X-AspNet-Version: 2.0.50727 X-SourceFiles: =?UTF-8?B?QzpcdGZzXFBvbGljeVByb2NlZHVyZSAtIENvcHlcY2NhY1xIb21lXFNlcnZpY2VEZXNrXFJlcXVlc3RcRG93bmxvYWRBdHRhY2htZW50LmFzcHg=?= Persistent-Auth: true X-Powered-By: ASP.NET X-UA-Compatible: IE=EmulateIE7 WWW-Authenticate: Negotiate oRswGaADCgEAoxIEEAEAAADugcuCwl9cxgAAAAA= Date: Fri, 31 Mar 2017 15:15:25 GMT

WHERE NOT EXISTS