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
)
Related
Is there a way to change "LIMIT 1" and get the same output? I have to get client's name, surname and a quantity of books that has the most books
SELECT stud.skaitytojas.name, stud.skaitytojas.surname,
COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.nr
ORDER BY quantity DESC
LIMIT 1
Postgres supports the ANSI standard FETCH FIRST 1 ROW ONLY, so you can do:
SELECT s.name, s.surname, COUNT(s.nr) AS quantity
FROM stud.egzempliorius e JOIN
stud.skaitytojas s
ON e.client = s.nr
GROUP BY s.name, s.surname
ORDER BY quantity DESC
FETCH FIRST 1 ROW ONLY;
Also notice the use of table aliases and proper JOIN syntax. I also prefer to list the columns in the SELECT in the GROUP BY, although that is optional if s.nr is unique.
You can select the row with the highest quantity using row_number()
SELECT * FROM (
SELECT * , row_number() over (order by quantity desc) rn FROM (
SELECT stud.skaitytojas.name, stud.skaitytojas.surname, COUNT (stud.skaitytojas.nr) AS quantity
FROM stud.egzempliorius , stud.skaitytojas
WHERE stud.egzempliorius.client = stud.skaitytojas.nr
GROUP BY stud.skaitytojas.name, stud.skaitytojas.surname
) t
) t where rn = 1
If you want to include ties for the highest quantity, then use rank() instead.
I want select countries with maximum value of 'Value' for a 'grpid'. Also already selected 'Country' should not be considered for other 'grpid' while checking the maximum. ( ie Country or grpid should not be repeated in the result )
SQL Fiddle
Result:
Country grpid Value Row_number
US 49707 604456458 1
GB 5086 497654945 4
CA 909 353500201 10
JP 231 198291290 15
try this query instead,
WITH OrderedOrders AS
(
SELECT country,grpid,value,ROW_NUMBER() OVER(PARTITION BY country ORDER BY country,value DESC) AS 'RowNumber'
FROM test1
)
select * from OrderedOrders
where RowNumber =1
I believe this is what you're looking for:
SQL Fiddle
;with cte as
(
select
country,
max(value) as MaxVal,
min(row_number) as MinRow
from test1
group by Country
)
select
c.country,
t.grpid,
c.MaxVal,
c.MinRow
from cte c
join test1 t
on t.country = c.country
and t.value = c.MaxVal
and t.row_number = c.MinRow
order by country, grpid
Can you please try out this query
select
country,
value,
grpid,
count(*)
from test1
group by
country,
value,
grpid
order by
country,
value desc
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
I have an SQL table consisting of:
[title]
ElementA
ElementA
ElementB
ElementC
ElementA
ElementA
I am trying to find a way to count the element which occurs the most; in this example, that would be
4 because ElementA has occurred 4 times. The problem is, this table is dynamic, so I can't just say SELECT COUNT(*) WHERE title = 'ElementA';
Does anyone have any idea how to compose a SELECT statement to do this? Conceptually, it seems pretty simple, but I just can't make it work.
Many thanks,
Brett
SELECT TOP 1 Title, COUNT(*) FROM table GROUP BY Title ORDER BY 2 DESC
or
SELECT Title, COUNT(*) FROM table GROUP BY Title ORDER BY 2 DESC LIMIT = 1
depending on the product you're using.
(Edited to correct the ORDER BY clause).
Select TOP 1 Title
FROM
(
Select
Count(title) k, title
FROM
titles
GROUP BY
TITLE
) Count
ORDER BY
k Desc
Try this to get a complete list
SELECT Title, Count (*) as NumOccurences
FROM MyTable
GROUP BY Title
Order BY NumOccurences
To just get the top dog, use the following with some flavor changes to suit your SQL syntax
SELECT TOP 1 Title, NumOccurences
FROM
(
SELECT Title, Count (*) NumOccurences
FROM MyTable
GROUP BY Title
Order BY Count (*) DESC
) AS Titles
Sql Server:
SELECT TOP 1
Title, COUNT(*) AS Count
FROM tbl
GROUP BY Title
ORDER BY Count DESC
Postgresql, MySQL, etc:
SELECT
Title, COUNT(*) AS Count
FROM tbl
GROUP BY Title
ORDER BY Count DESC
LIMIT 1
Use a group by to group the elements followed by a max on der respective count.. Use nested queries to make the solution simpler.. :)
WITH CTE (Title, Counter) as
(SELECT Title, Count(*) Counter FROM #Test GROUP BY Title)
SELECT Top 1 Title, Counter FROM CTE order by Counter Desc
I have the following table:
memberid
2
2
3
4
3
...and I want the following result:
memberid count
2 2
3 1 ---Edit by gbn: do you mean 2?
4 1
I was attempting to use:
SELECT MemberID,
COUNT(MemberID)
FROM YourTable
GROUP BY MemberID
...but now I want find which record which has maximum count. IE:
memberid count
2 2
SELECT memberid, COUNT(*) FROM TheTable GROUP BY memberid
Although, it won't work for your desired output because you have "memberid = 3" twice.
Edit: After late update to question...
SELECT TOP 1 WITH TIES --WITH TIES will pick up "joint top".
memberid, COUNT(*)
FROM
TheTable
GROUP BY
memberid
ORDER BY
COUNT(*) DESC
SELECT MemberID, COUNT(MemberID) FROM YourTable GROUP BY MemberID
What if there is a tie (or more) for the max? Do you want to display one or all?
This is how I would do this
SELECT memberid, COUNT(1)
FROM members
GROUP BY memberid
HAVING COUNT(1) = (
SELECT MAX(result.mem_count)
FROM (
SELECT memberid, COUNT(1) as mem_count
FROM members
GROUP BY memberid
) as result
)
I would love to see a more efficient approach though.
Do it like this:
SELECT memberid, COUNT(memberid) AS [count] FROM [Table] GROUP BY memberid
This should do the trick with no subselects required:
select top 1 memberid, COUNT(*) as counted
from members
group by memberid
order by counted desc
Can be done quite easy:
SELECT TOP 1 MemberId, COUNT(*) FROM YourTable GROUP BY MemberId ORDER By 2 DESC
I believe the original poster requested 2 result sets.
The only way I know of to get this (in SQL Server) is to dump the original records into a temp table and then do a SELECT and MAX on that. I do welcome an answer that requires less code!
-- Select records into a temp table
SELECT
Table1.MemberId
,CNT = COUNT(*)
INTO #Temp
FROM YourTable AS Table1
GROUP BY Table1.MemberId
ORDER BY Table1.MemberId
-- Get original records
SELECT * FROM #Temp
-- Get max. count record(s)
SELECT
Table1.MemberId
,Table1.CNT
FROM #Temp AS Table1
INNER JOIN (
SELECT CNT = MAX(CNT)
FROM #Temp
) AS Table2 ON Table2.CNT = Table1.CNT
-- Cleanup
DROP TABLE #Temp
How about this query:
SELECT TOP 1 MemberID,
COUNT(MemberID)
FROM YourTable
GROUP BY MemberID
ORDER by count(MemberID) desc
SELECT count(column_name)
FROM your_table;
You need to use a subselect:
SELECT MemberID, MAX(Count) FROM
(SELECT MemberID, COUNT(MemberID) Count FROM YourTable GROUP BY MemberID)
GROUP BY MemberID
The second group by is needed to return both, the count and the MemberID.