I have a table with rows:
ID CountryCode Status
----------- ----------- -----------
2 PL 1
3 PL 2
4 EN 1
5 EN 1
and upon request
SELECT *
FROM [TestTable]
WHERE Status = 1 AND CountryCode NOT IN (SELECT CountryCode
FROM [TestTable]
WHERE Status != 1)
I get all national codes that have no status value = 2
ID CountryCode Status
----------- ----------- -----------
4 EN 1
5 EN 1
I feel this request could be simpler and more understandable.
How can I change it?
Regards
EDIT
PL cannot be the result because it has a record with status 2
EDIT
Script to create and populate a table:
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',
1
)
INSERT INTO dbo.TestTable
( CountryCode, Status )
VALUES ( 'PL',
2
)
INSERT INTO dbo.TestTable
( CountryCode, Status )
VALUES ( 'EN',
1
)
INSERT INTO dbo.TestTable
( CountryCode, Status )
VALUES ( 'EN',
1
)
source
share