0

I am trying to select a rows based on a Maximum value of a specific column value in SQL server 2008. I need to select the bookname and year based on the maximum edition.

Current table data:

    BookName    Year    Edition
Latest Science  2013    1
Latest Science  2014    2
Latest Science  2015    3
Easy Maths      2014    1
Easy Maths      2015    2
Magic vs Logic  2015    1

Output should be:

BookName    Year    Edition
Latest Science  2015    3
Easy Maths      2015    2
Magic vs Logics 2015    1

I tried but it result only one rows of entire table

SELECT     Book, year, edition
FROM         Book_info
where edition=(select max(edition) from book_info)

2 Answers 2

1

I would do it this way:

;WITH cte AS (
 SELECT *, 
 ROW_NUMBER() OVER (PARTITION BY BookName ORDER BY Edition DESC) AS rn
 FROM book_info
)
SELECT * FROM cte
WHERE rn=1
Sign up to request clarification or add additional context in comments.

5 Comments

Comma after PARTITION BY is not needed :)
Thanks, I forgot (obviously). : )
I am little confused, could you please explain the code? Is the code start with semicolon?
@Tab Alleman, When I using your code sql showing error, where to call the table name? Kindly check and help.
oops, I left that out. I have edited and corrected it.
0

You should look up group by with having clauses.

Here is the entire script per your requirements which returns the results you're looking for.

USE [Test]

--table

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

SET ANSI_PADDING ON

GO

CREATE TABLE [dbo].[Book](

[Id] [int] IDENTITY(1,1) NOT NULL,

[BookName] [varchar](50) NOT NULL,

[Year] [int] NOT NULL,

[Edition] [int] NOT NULL

) ON [PRIMARY]

GO

SET ANSI_PADDING OFF

GO



USE [Test]

GO

--records

INSERT INTO [dbo].[Book]

([BookName]

,[Year]

,[Edition])

VALUES

('Last Science', 2013 ,1),

('Last Science', 2014 ,2),

('Last Science', 2015 ,3),

('Easy Maths', 2014 ,1),

('Easy Maths', 2015 ,2),

('Magic vs Logic', 2015 ,1)

GO

USE [Test]

GO

--query

SELECT

BookName,

MAX([Year]) AS Year,

MAX(Edition) AS Edition

FROM dbo.Book

GROUP BY

BookName

Order by MAX(Edition) DESC

4 Comments

Thanks @chdev77, group by is really helped me.
But I am facing one error, if I use the year its showing BookName. year is a invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Here I found the Answer for my question but I want to know how to display the corresponding year, SELECT BookName, MAX(Edition) AS Edition FROM Book_info AS Book_info_1 GROUP BY BookName order by Edition
Thanks @chdev77, Its works like a charm, I want one more that I don`t want to display the max of year but I need to show the corresponding year of the maximum edition.
I would remove the max (year) and create an inner join to look up the year value. Hopefully your book table has an id column. I would create a new model honestly. Remember if your queries are hard to write then your table structure may need to change. I would create a book table, book year table and a book edition table, with book.id being a foreign key to year and edition table.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.