group by with order by desc - sql

select aaa.BookId from
(SELECT TOP (100) PERCENT Id, BookId,CreatedOnUtc
FROM dbo.Chapter AS c
ORDER BY CreatedOnUtc desc
) as aaa
group by aaa.BookId
how come ORDER BY CreatedOnUtc desc doesn't not have any effect on the result.
I want to get a result like this
BookId (with desc createdonUtc)
3
4
1
...
UPDATED: i go with this
select BookId, Max(CreatedOnUtc)
from Chapter
group by BookId
order by Max(CreatedOnUtc) desc

The ORDER BY is only for your subquery. Your outer query has no order by clause, so the result can be in any order.
Other things:
Why TOP 100 PERCENT?
Why SELECT BookId and GROUP BY BookId but no aggregating column? Maybe you just want SELECT DISTINCT?
Also, what you want to achieve might not be possible. If you have this table with a duplicate BookId:
ID BookId CreatedOnUtc
1 1 1
2 2 2
3 1 3
In what order shall your result be? 1 2 or 2 1?

Related

How to return top 3 rows if last row is tied

Suppose my table contains data like that
ID MovieName Rating
--------------------------------------------
1 The Shawshank Redemption 9.20
2 The Godfather: Part II 9.00
3 12 Angry Men 8.90
4 Pulp Fiction 8.90
5 The Good, the Bad and the Ugly 8.80
I want to select top 3 movies according to highest rating which contains both '12 Angry Men' and 'Pulp Fiction' movies.So query should return 4 rows instead of 3.
Just use TOP WITH TIES
SELECT TOP 3 WITH TIES ID, MovieName, Rating
FROM MyMoviesDB
ORDER BY Rating DESC
Only thing is you have to use ORDER BY
Try:
SELECT A.ID, A.MOVIENAME, A.RATING
FROM
(SELECT ID, MOVIENAME, RATING, DENSE_RANK() OVER (ORDER BY RATING DESC) AS R
FROM YOUR_TABLE) A
WHERE A.R <= 3
ORDER BY A.RATING DESC;
Dense Rank will repeat for same ratings. So if one of the top 3 ranks have more than one occurrences, you will get more than 3 entries in the output.
You would want to select the rows in the moviesdb table where the Rating is in the DISTINCT Top 3 Ratings Ordered BY Ratings in Desc Order:
SELECT *
FROM moviesdb
WHERE Rating IN (SELECT DISTINCT TOP 3 Rating
FROM moviesdb
ORDER BY Rating Desc)
Select * from Table_Name
ORDER BY Rating
LIMIT 4

SQL Show value that appears more times and how many times

I'm trying to show the value that appears more times from a movies table.
For example:
movie_id, tag_id, score
1 1 4
1 3 5
2 1 3
3 2 4
3 3 5
Result:
tag_id, times
1 2
3 2
2 1
That table has the following columns: {movie-id, tag-id, score}.
How I can retrieve the tag-id that appears more times and how many times?
I've tried the following but it shows the same number for each tag-id:
SELECT tagId, COUNT(tagId) AS ocurrence FROM scores GROUP BY tagId ORDER BY ocurrence DESC
I think you're looking for:
SELECT TAGID, COUNT(TAGID)
FROM TABLENAME
GROUP BY TAGID
ORDER BY COUNT(TAGID)
--or you could do a having clause where COUNT(TAG-ID) > 1
SELECT tagId, COUNT(tagId) FROM scores GROUP BY (tagID) HAVING COUNT(tagId) >= ALL
(SELECT COUNT(tagId) FROM scores GROUP BY (tagId))
This will also work.

Sort rows for duplicate ID numbers

I have been trying to find ways to sort rows based on duplicate ID numbers per below but have been unsuccessful.
ID SortOrder PersonID
1 0 100
2 1 100
3 0 200
4 0 200
5 1 200
I am trying to sort the sortOrder column so the number 1 will display at the top for every PersonID. So the end results are like below:
ID SortOrder PersonID
1 1 100
2 0 100
3 1 200
4 0 200
5 0 200
Any Suggestions? Thanks!
You can use CASE like this.
ORDER BY PersonID,CASE WHEN SortOrder = 1 THEN 1 ELSE 2 END
You can change the CASE appropriately, if you want to change how following rows after SortOrder = 1 should be ordered for the same PersonID.
Like ORDER BY PersonID,CASE SortOrder WHEN 1 THEN 1 WHEN 0 THEN 2 ELSE 3 END
One other solution:
select id,SortOrder,PersonId from
(select id,SortOrder,PersonId,row_number() over (partition by PersonId order by SortOrder desc) as rn from table) A
order by A.rn asc
Based on your comments regarding wanting the ID value to stay in constant order, you're likely looking for something like this:
Select Row_Number() Over (Order By PersonID Asc, SortOrder Desc) As ID,
SortOrder,
PersonID
From YourTable
Order By PersonID Asc, SortOrder Desc
But, I'd caution against naming the ROW_NUMBER field ID, as it can be misleading since it's not actually the unique identifier for the stored record.

MAX function without group by

I have the following table:
ID | NUM
1 | 4
2 | 9
3 | 1
4 | 7
5 | 10
I want a result of:
ID | NUM
5 | 10
When I try to use MAX(NUM) I get and error that I have to use GROUP BY in order to use MAX function
Any idea?
As per the error, use of an aggregate like Max requires a Group By clause if there are any non-aggregated columns in the select list (In your case, you are trying to find the MAX(Num) and then return the value(s) associated in the ID column). In MS SQL Server you can get what you want via ordering and limiting the returned rows:
SELECT TOP 1 ID, NUM
FROM [table]
ORDER BY NUM DESC;
In other RDBMS systems the LIMIT offers similar functionality.
Edit
If you need to return all rows which have the same maximum, then use the WITH TIES qualification:
SELECT TOP 1 WITH TIES ID, NUM
FROM [table]
ORDER BY NUM DESC;
May return more than 1 result:
SELECT id, num
FROM table
WHERE num = (SELECT MAX(num) FROM table)
Try this query.
WITH result AS
(
select DENSE_RANK() OVER( ORDER BY NUM desc) AS RowNo,ID,NUM from #emp
)
select ID,NUM from result where RowNo=1
it will return max values even if it has more MAX values like:
ID | NUM
5 | 10
6 | 10
refer below link to know more about RANKING Functions:
http://msdn.microsoft.com/en-us/library/ms189798
How about:
SELECT TOP 1 ID,NUM FROM table ORDER BY NUM DESC;
Do this -
SELECT TOP 1 ID,
NUM
FROM <yourtable>
ORDER BY NUM DESC;
Get all rows have max values but THERE ARE 3 SELECT, It's not good for performance
SELECT id, MAX(num) as num
FROM table
GROUP BY id
ORDER BY MAX(num) DESC
LIMIT (SELECT COUNT(*)
FROM table
WHERE num =(SELECT MAX(num) FROM table)
)

TOP 1 Query from each ID with multiple instances

This query will return the top for all rows in MS Access.
SELECT TOP 1 * FROM [table]
ORDER BY table.[Date] DESC;
I need to return the top date for each id that can have multiple dates.
ID DATE
1 01/01/2001
1 01/12/2011
3 01/01/2001
3 01/12/2011
Should return only the top dates like this.
1 01/12/2011
3 01/12/2011
You'll want to use the MAX function, along with a GROUP BY.
SELECT ID, MAX(DATE)
FROM [table]
GROUP BY ID