I am wanting to list names and the number of times they have done a certain action. I then want to order the names by the most amount of times.
I have the below code so far but I keep getting errors:
select name, count(*) as NoOfTimes
from CustName
group by count(*);
order by count(*) asc;
I should note that if you want the most times at the beginning of the result set, the you want a descending sort:
select name, count(*) as NoOfTimes
from CustName
group by name
order by count(*) desc;
In order to show count by name, you must group by name
select name, count(*) as NoOfTimes
from CustName
group by name
order by NoOfTimes desc
Order by index also a good idea:
select name, count(*) as NoOfTimes
from CustName
group by name
order by 2 DESC
Related
I have a table in SQL like this. Now I want to find sum of score grouped by column ID & Name, and show just two highest sums for each ID as below, so how can I solve this?
Can you try something like this:
Select ID, Name, Score From (
Select ID, Name, SUM(Score) score, row_number() over (partition by ID,Name order by SUM(Score) desc) rn
from Table
group by ID,Name) allScores
where rn > 2
You can Try This:
select top(2) Id , name , sum(Score) as sumScore
from table
group by id , name
order by sum(Score) desc
I wanted to get the maximum count of a column for which I used Count() and wanted to order the column highest to lowest value without using max or order by desc.
I tried
SELECT COUNT(subject) AS CNT, student
FROM AUTHOR
GROUP BY student AU1
WHERE not exists (SELECT * FROM AUTHOR AU2
WHERE AU2.student <> AU1.student AND AU2.subject > AU1.CNT)
but it doesn't return the desired output.
The desired output is the same as
SELECT COUNT(subject) AS CNT, student
FROM AUTHOR
GROUP BY Student
ORDER BY CNT DESC
but, without the order by desc or MAX.
You can use ORDER BY ASC:
SELECT COUNT(subject) AS CNT, student
FROM AUTHOR
GROUP BY Student
ORDER BY (- CNT) ASC;
If you want the results in a particular order, you need to use ORDER BY. That is one of the rules of using SQL.
OK, so DESC is not allowed and MAX is not allowed.
SELECT
COUNT(subject) AS CNT, student
FROM
AUTHOR
GROUP BY
Student
ORDER BY
-CNT
This does not work in each database software.
An other version:
SELECT * FROM (
SELECT
COUNT(subject) AS CNT, student
FROM
AUTHOR
GROUP BY
Student
) t
ORDER BY
-CNT
I have a table "well". It contains a column app_rate_unit (type: nvarchar).
My goal is to count every distinct value in the table and let the DBMS (MS Server 2005) give me the most occurring one.
This is my code:
SELECT MAX(app_rate_unit) AS MAX_APP
FROM (SELECT app_rate_unit, COUNT(*) AS co
FROM dbo.well AS w
GROUP BY app_rate_unit
) AS derivedtbl_1
The poblem with it is however, that my DBMS actually delivers the lowest count to me.
SideQuestion: How do I filter for a foreign key (in the table) and NOT NULL (in app_rate_unit) when counting?
select top 1 app_rate_unit, count(*) from dbo.well
group by app_rate_unit
order by count(*) desc
Try this
SELECT
COUNT(app_rate_unit)AS MAX_APP ,
app_rate_unit
FROM
dbo.well
WHERE
app_rate_unit IS NOT NULL
GROUP BY
app_rate_unit
ORDER BY
MAX_APP DESC
The above script will give you the count and the item. You can change the count if you are not sure only one item will have the maximum number of occurrence.
select top 1 count(*) as co from dbo.well as w group by app_rate_unit
order by count(*) desc
In PostgreSQL we can write query which using max of count as
select max(count) from (
select count(id) from Table _name group by created_by,status_id having status_id = 6 ) as Alias
eg
select max(count) from (
select count(id) from orders group by created_by,status_id having status_id = 6 ) as foo
what is the sql query to find the duplicate records and display in descending, based on the highest count and the id display the records.
for example:
getting the count can be done with
select title, count(title) as cnt from kmovies group by title order by cnt desc
and the result will be like
title cnt
ravi 10
prabhu 9
srinu 6
now what is the query to get the result like below:
ravi
ravi
ravi
...10 times
prabhu
prabhu..9 times
srinu
srinu...6 times
If your RDBMS supports the OVER clause...
SELECT
title
FROM
(
select
title, count(*) OVER (PARTITION BY title) as cnt
from
kmovies
) T
ORDER BY
cnt DESC
You can do it in a single query:
Select t.Id, t.title, z.dupCount
From yourtable T
Join
(select title, Count (*) dupCount
from yourtable
group By title
Having Count(*) > 1) z
On z.title = t.Title
order By dupCount Desc
This query uses the Group By and and Having clauses to allow you to select (locate and list out) for each duplicate record. The As clause is a convenience to refer to Quantity in the select and Order By clauses, but is not really part of getting you the duplicate rows.
Select
Title,
Count( Title ) As [Quantity]
From
Training
Group By
Title
Having
Count( Title ) > 1
Order By
Quantity desc
select distinct title, (
select count(title)
from kmovies as sub
where sub.title=kmovies.title) as cnt
from kmovies
group by title
order by cnt desc
You can't do it as a simple single query, but this would do:
select title
from kmovies
where title in (
select title
from kmovies
group by title
order by cnt desc
having count(title) > 1
)
SELECT username, (SUM(rating)/count(*)) as TheAverage, count(*) as TheCount
FROM ratings
WHERE month ='Aug' AND TheCount > 1
GROUP BY username
ORDER BY TheAverage DESC, TheCount DESC
I know that's really close (I think) but it's saying 'TheCount' doesn't exist in the WHERE clause and the ORDER clause.
The table is:
id, username, rating, month
And I'm trying to work out the average rating for each user then order the results by average rating and number of ratings.
SELECT username, (SUM(rating)/count()) as TheAverage, count() as TheCount
FROM ratings
WHERE month ='Aug'
GROUP BY username
HAVING TheCount > 1
ORDER BY TheAverage DESC, TheCount DESC
EDIT:
Seems I didn't look closely enough.
I think it'll work now.
If you group and count, you need having:
SELECT username, (SUM(rating)/COUNT(*)) as TheAverage, Count(*) as TheCount
FROM rating
WHERE month='Aug'
GROUP BY username
HAVING TheCount > 1
ORDER BY TheAverage DESC, TheCount DESC
You could use the AVG aggregate:
SELECT username, month, AVG(rating) as TheAverage, COUNT(*) as TheCount
FROM ratings
WHERE month ='Aug'
GROUP BY
username
HAVING COUNT(*) > 1
ORDER BY
TheAverage DESC, TheCount DESC
Grouping by month is innesessary in MySQL, since your month is filtered and MySQL supports selecting an ungrouped column in a SELECT list of a GROUP BY query (returning a random value within the group).