简单重构sql查询

简单重构sql查询,sql,sql-server-2005,refactoring,Sql,Sql Server 2005,Refactoring,我有一张有行的桌子: ID CountryCode Status ----------- ----------- ----------- 2 PL 1 3 PL 2 4 EN 1 5 EN 1 通过查询 SELECT * FROM [TestTable] WHERE Status = 1 AND CountryCode

我有一张有行的桌子:

ID          CountryCode Status
----------- ----------- -----------
2           PL          1
3           PL          2
4           EN          1
5           EN          1
通过查询

SELECT *
  FROM [TestTable]
  WHERE Status = 1 AND CountryCode NOT IN (SELECT CountryCode
  FROM [TestTable]
  WHERE Status != 1)
我得到了所有没有状态值=2的国家代码

ID          CountryCode Status
----------- ----------- -----------
4           EN          1
5           EN          1
我觉得这个问题可以更简单、更清楚

我怎样才能改变它

致意

编辑

PL不能在结果中,因为有状态为2的记录

编辑

创建和填充表的脚本:

USE [DatabaseName]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[TestTable](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [CountryCode] [nvarchar](2) NOT NULL,
    [Status] [int] NOT NULL
) ON [PRIMARY]

INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'PL', -- CountryCode - nvarchar(2)
            1  -- Status - int
            )

INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'PL', -- CountryCode - nvarchar(2)
            2  -- Status - int
            )

INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'EN', -- CountryCode - nvarchar(2)
            1  -- Status - int
            )
INSERT INTO dbo.TestTable
          ( CountryCode, Status )
  VALUES  ( 'EN', -- CountryCode - nvarchar(2)
            1  -- Status - int
            )

如果您想要状态值不为2的所有条目,请尝试以下操作:

SELECT *
  FROM [TestTable]
 WHERE Status != 2
编辑:若要防止国家/地区代码中的任何不同条目具有不需要的值,请尝试使用GROUP BY和HAVING子句:

第一:不要在常用代码中使用SELECT*。特别是在生产方面。说出你的专栏

肥皂盒盖上

注意:我还没有尝试过这个,而且我目前还没有安装ManagementStudio,所以我无法测试它。但我想你想要这样的东西:

Select Id, CountryCode, Status
From [TestTable] t
Where Status <> 2
And Not Exists(select status from [TestTable] t2 
                             where t2.Status = 2 
                             and t2.CountryCode = tt.CountryCode)

至少,您的想法是正确的:如果您只希望任何记录上没有与Status=2对应的CountryCodes,则需要获取状态为1的所有内容,然后排除状态为2的匹配行的任何现有行。但是,我的Not Exists的特定语法可能不正确。

我用一个查询更新了答案,该查询避免了显式嵌套查询和无法工作的连接,因为T-SQL要求在聚合函数中使用group by列,或在group by子句中列出group by列。你认为MySQL在哪里?耶,团队合作!我没有SQL server 2005,我可以在上面进行测试,但是一点协作会有很大帮助:-1和2是唯一的状态吗。。。或者,是否还有其他可能导致最小/最大应用程序失败的情况。。。
select T1.*
from TestTable as T1
  left outer join
    (
      select distinct CountryCode
      from TestTable as T1
      where Status <> 1  
    ) as T2
    on T1.CountryCode = T2.CountryCode
where
  T1.Status = 1 and
  T2.CountryCode is null
Select Id, CountryCode, Status
From [TestTable] t
Where Status <> 2
And Not Exists(select status from [TestTable] t2 
                             where t2.Status = 2 
                             and t2.CountryCode = tt.CountryCode)
select T1.*
from TestTable as T1
  left outer join
    (
      select distinct CountryCode
      from TestTable as T1
      where Status <> 1  
    ) as T2
    on T1.CountryCode = T2.CountryCode
where
  T1.Status = 1 and
  T2.CountryCode is null