Faster select Max SQL Query - sql

Here is the query to select the Config Type that has the Max number of rows.
Is there another way that is just as fast as the second query to select the data?
select CONFIG_TYPE, MAX(COUNTING) FROM
(select CONFIG_TYPE, COUNT(*) as COUNTING FROM NOTIFICATION_CONFIG GROUP BY CONFIG_TYPE)
WHERE COUNTING =
(select MAX(COUNTING) FROM
(select COUNT(*) as COUNTING FROM NOTIFICATION_CONFIG GROUP BY CONFIG_TYPE)
)GROUP BY CONFIG_TYPE
This second query will do the job in 1 search, unlike the other that does it in 2.
select CONFIG_TYPE, COUNTING FROM
(select CONFIG_TYPE, COUNT(*) as COUNTING FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE ORDER BY COUNTING DESC FETCH FIRST 1 ROW ONLY)
just wondering if there was another way to do this.

Use window functions!
SELECT c.*
FROM (SELECT CONFIG_TYPE, COUNT(*) as COUNTING,
RANK() OVER (ORDER BY COUNT(*) DESC) as seqnum
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
) c
WHERE seqnum = 1;
This uses RANK(), so it will return all maximum values (in the case of ties). If you want one arbitrary maximum returned in the case of ties, then use ROW_NUMBER() instead. Or, if you don't want ties, you can do:
SELECT CONFIG_TYPE, COUNT(*) as COUNTING
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
ORDER BY COUNT(*) DESC
FETCH FIRST 1 ROW ONLY;

this is just messed up
select CONFIG_TYPE, MAX(COUNTING)
FROM ( select CONFIG_TYPE, COUNT(*) as COUNTING
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
)
WHERE COUNTING = ( select MAX(COUNTING)
FROM ( select COUNT(*) as COUNTING
FROM NOTIFICATION_CONFIG
GROUP BY CONFIG_TYPE
)
)
GROUP BY CONFIG_TYPE

Related

max(count) from inner query using row_number()

I am trying to pick the src_cd which has the maximum count in my inner query:
select count(*), src_cd
from innertable1
group by src_cd
Result for the above query is:
cnt
src_cd
100
CCC
90
BBB
80
AAA
From the above result I want to do a
select *
from table1
where src_cd having max(cnt of src_cd from innertable1)
I also want to use row_number() to pick the 2nd max, 3rd max and so on
You can use limit 1 with order by to pick largest.
select count(*), src_cd
from innertable1
group by src_cd
order by 1 desc
limit 1
Order by will order in descending order of count.
limit will pick up first row.
You can also use subquery to calculate next max rows using row_number().
select src_cd as second_max
from (
select src_cd, row_number() over( order by cnt desc) as rownum
from (
select count(*) cnt, src_cd
from innertable1
group by src_cd
)rs
) rs2
where rownum=2 -- second MAX

How to find most frequent value in SQL column and return that value?

I was trying to do something like this:
select nume_produs
from incasari
group by id
having count(nume_produs) = max(count(nume_produs));
but it doesn't work
Do a GROUP BY. Order by count descending. Fetch the first row (highest count) only.
select nume_produs, count(*) as cnt
from incasari
group by nume_produs
order by cnt desc
fetch first 1 row with ties
For the most common value in the column:
select num_produs
from (select nume_produs, count(*) as cnt,
row_number() over (order by count(*)) as seqnum
from incasari
group by nume_produs
) i
where seqnum = 1;
If you want multiple values in the event of duplicates, use rank() instead of row_number().
If you want the most common value per id, then add partition by:
select num_produs
from (select nume_produs, count(*) as cnt,
row_number() over (partition by id order by count(*)) as seqnum
from incasari
group by nume_produs
) i
where seqnum = 1;
SELECT `nume_produs`,
COUNT(`nume_produs`) AS `value_occurrence`
FROM `incasari`
GROUP BY `nume_produs`
ORDER BY `value_occurrence` DESC
LIMIT 1;
Increase 1 if you want to see the N most common values of the column.

Need to change LIMIT into something else

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.

Total Row Count in sql query---sql server 2008

My query is as follows
BEGIN
WITH MyCTE
AS (
SELECT T.MusicAlbumTitle
,D.musicTitle
,D.mVideoID
,D.musicFileName
,T.ReleaseDate AS ReleasedDate
,D.MusicLength
,D.musicSinger
,D.MusicVideoID
,D.ExternalLink
,D.CoverImg
,ROW_NUMBER() OVER (
PARTITION BY D.MusicVideoID ORDER BY D.mVideoID
) AS row_num
FROM dbo.Music_Video T
JOIN dbo.Music_Video_Details D ON T.MusicVideoID = D.MusicVideoID
WHERE T.PortalID = #PortalID
AND T.CultureCode = #CultureCode
AND T.ComingSoon <> 1
GROUP BY T.MusicAlbumTitle
,D.musicTitle
,D.mVideoID
,T.ReleaseDate
,D.musicFileName
,D.MusicLength
,D.musicSinger
,D.MusicVideoID
,D.ExternalLink
,D.CoverImg
)
SELECT a.mVideoID
,a.MusicVideoID
,a.musicFileName
,a.MusicAlbumTitle
,a.ReleasedDate
,a.row_num
,a.CoverImg
,a.ExternalLink
,a.musicTitle
,a.MusicLength
FROM MyCTE a
WHERE row_num = 1
ORDER BY MusicVideoID DESC
END
I need to achieve total row count from last select statement.
which mean total row count that is being selected.
or any idea that might be use in this condition
How can i do this ..
Please add COUNT(*) OVER() in your select, which returns total rows selected as a new column.
Ex:
SELECT
*,
COUNT(*) OVER() AS [Total_Rows]
FROM YourTable
Just to be clear, you need to add the count to the CTE, not the outer query. The outer select is returning only one row, so the count would always be one.
The CTE should start:
WITH MyCTE
AS (
SELECT T.MusicAlbumTitle
,D.musicTitle
,D.mVideoID
,D.musicFileName
,T.ReleaseDate AS ReleasedDate
,D.MusicLength
,D.musicSinger
,D.MusicVideoID
,D.ExternalLink
,D.CoverImg
,ROW_NUMBER() OVER (
PARTITION BY D.MusicVideoID ORDER BY D.mVideoID
) AS row_num,
COUNT(*) over () as total_count

SELECT MAX of COUNT

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