I have the table with rows:
ID CountryCode Status
----------- ----------- -----------
2 PL 1
3 PL 2
4 EN 1
5 EN 1
and by the query
SELECT [CountryCode]
,MAX([Status])
FROM [TestTable]
GROUP BY CountryCode,Status
I want to get:
CountryCode Status
----------- -----------
PL 2
EN 1
but I get:
CountryCode Status
----------- -----------
EN 1
PL 1
PL 2
What is wrong with this query?
Best regards
EDIT
Ok, Thanx for manz answers, but I didn’t add part of query, which is :
Having Status != 3
so I think I must use Status in group by :/
Script to create and fill 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', -- 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
)
You need to get rid of the group by status. The group by says return a new row for every unique combination of CountryCode and Status, which is not what you want.
You can add the where clause to exclude the rows that you don’t want to consider in your query.
Try: