sql select 5 higest values - sql

I'm trying to select the 5 rows with the highest count value
This is my query:
string sql = "SELECT top 5 count FROM Likes ORDER BY COUNT(*) DESC";
It's just throwing an error code that
Column 'Likes.count' is invalid in the select list because it is not
contained in either an aggregate function or the GROUP BY clause.
It's for a project I've got to present tomorrow...

On SQL Server, simply do this:
SELECT TOP 5 * FROM Likes ORDER BY [Count] DESC
This assumes that your Likes-table already contains a column named [Count] meaning that you don't need to count the records yourself (which is what COUNT(*) does).

You should not use COUNT(*) here for order by.
SELECT top 5 [count] FROM Likes ORDER BY [Count] DESC

count is a reserved word which is why you should stay clear of using them for column names. If you don't want to rename the column you can escape it, different dbms may effect who you do this. In ssms you would use square brackets.
string sql = "SELECT top 5 [count] FROM Likes ORDER BY COUNT(*) DESC";

Related

How do i get only the max value of a column SQL

i'm using this code
SELECT S.id_usuario, C.cnt
FROM promos_usuarios S
INNER JOIN (SELECT id_usuario, count(id_usuario) as cnt
FROM promos_usuarios
GROUP BY id_usuario) C ON S.id_usuario = c.id_usuario
after using this code, i get this
table
And i only need the max value of the column cnt, not the entire count, what can i do to make this work? i'm sorry if this doesnt make any sense, english isnt my main lenguage.
You can use a correlated subquery:
SELECT id_usuario, COUNT(*) as cnt
FROM promos_usuarios
GROUP BY id_usuario
ORDER BY COUNT(*) DESC
FETCH FIRST 1 ROW ONLY;
Note all databases support FETCH FIRST. Your database may use SELECT TOP or LIMIT or something else.

SQL Server - DISTINCT COUNT WITH PRINTING * COLUMNS

I am trying to print all columns "*" and at the end it should display COUNT as DISTINCT and GROUP BY SalesPersonID
I have done many tests with sub queries and main query but it doesnt work
SELECT S.SalesPersonID, COUNT(*)
FROM [AdventureWorks2014].[Sales].Store AS S
GROUP BY S.SalesPersonID
This query gives me accurate counts but I intend to print more columns and as soon as I enter another column the query wont work which I believe it is a GROUP BY SELECT Statment.
How would you do this please.
MySQL 8.0/SQL Server support windowed COUNT(*)
SELECT *,COUNT(*) OVER(PARTITION BY S.SalesPersonID) AS cnt
FROM AdventureWorks2014.Sales.Store AS S

Find Min in rows in SQL table

I am trying to find all the clubs in my database that appear the least number of times in the database and display that club name and how many times it appeared. Below is my solution for what I tried
select club, MIN(num)
from
(select count(club) as num
from member
order by club)
But this code throws an error:
SQL0119N An expression starting with "CLUB" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified.
I am new to SQL - can someone please tell me what is wrong and how I can fix this?
You could use the fetch first n rows only syntax:
SELECT club, COUNT(*)
FROM members
GROUP BY club
ORDER BY 2 ASC
FETCH FIRST 1 ROWS ONLY
You're heading in roughly the right direction. You need to do a double-aggregate.
WITH Member_Count AS (SELECT club, COUNT(*) AS counted
FROM Members
GROUP BY club)
SELECT club, counted
FROM Member_Count
WHERE counted = (SELECT MIN(counted)
FROM Member_Count)

SQL Function to count data

Say I have 100 records in a certain column in a certain table.
All of these pieces of data in that column are random numbers from 1 to 10
What SQL function can I use to count the number that appears the most within those 100 records and it will display that number alone in the column?
How do I do this? Thanks
Assuming you're using mysql (because of question tags):
SELECT n
FROM tablename
GROUP BY n
ORDER BY COUNT(*) DESC
LIMIT 1
Try a query like this to get the count:
select count(*)
from t
group by col
order by count(*) desc
limit 1
This is MySQL syntax. The limit 1 is database-specific. In SQL Server, for instance, it would be select top 1.
And this to get the number in the column:
select col
from t
group by col
order by count(*) desc
limit 1

SQL Server: How can I use the COUNT clause without GROUPing?

I'm looking get two things from a query, given a set of contraints:
The first match
The total number of matches
I can get the first match by:
SELECT TOP 1
ID,
ReportYear,
Name,
SignDate,
...
FROM Table
WHERE
...
ORDER BY ... //I can put in here which one I want to take
And then I can get the match count if I use
SELECT
MIN(ID),
MIN(ReportYear),
MIN(Name),
MIN(SignDate),
... ,
COUNT(*) as MatchCount
FROM Table
WHERE
...
GROUP BY
??? // I don't really want any grouping
I really want to avoid both grouping and using an aggregate function on all my results. This question SQL Server Select COUNT without using aggregate function or group by suggests the answer would be
SELECT TOP 1
ID,
ReportYear,
Name,
SignDate,
... ,
##ROWCOUNT as MatchCount
FROM Table
This works without the TOP 1, but when it's in there, ##ROWCOUNT = number of rows returned, which is 1. How can I get essentially the output of COUNT(*) (whatever's left after the where clause) without any grouping or need to aggregate all the columns?
What I don't want to do is repeat each of these twice, once for the first row and then again for the ##ROWCOUNT. I'm not finding a way I can properly use GROUP BY, because I strictly want the number of items that match my criteria, and I want columns that if I GROUPed them would throw this number off - unless I'm misunderstanding GROUP BY.
Assuming you are using a newish version of SQL Server (2008+ from memory) then you can use analytic functions.
Simplifying things somewhat, they are a way of way of doing an aggregate over a set of data instead of a group - an extension on basic aggregates.
Instead of this:
SELECT
... ,
COUNT(*) as MatchCount
FROM Table
WHERE
...
You do this:
SELECT
... ,
COUNT(*) as MatchCount OVER (PARTITION BY <group fields> ORDER BY <order fields> )
FROM Table
WHERE
...
GROUP BY
Without actually running some code, I can't recall exactly which aggregates that you can't use in this fashion. Count is fine though.
Well, you can use OVER clause, which is an window function.
SELECT TOP (1)
OrderID, CustID, EmpID,
COUNT(*) OVER() AS MatchCount
FROM Sales.Orders
WHERE OrderID % 2 = 1
ORDER BY OrderID DESC
Try next query:
select top 1
*, count(*) over () rowsCount
from
(
select
*, dense_rank() over (order by ValueForOrder) n
from
myTable
) t
where
n = 1