I have a table like this:
`id|value
1|2
2|8
3|5
4|6
5|10
6|7`
I need a query to pull AND sum the 3 highest values. So the correct query would pull the following:
3 highest:
5|10
2|8
6|7
Sum of 3 highest values = 25
I feel like this should be pretty simple but i'm having a tough time! Thanks for your help
SELECT SUM(Value) AS SumOfTop3Values
FROM (
SELECT Value
FROM Table
ORDER BY Value DESC
LIMIT 3
) AS sub
I think you need to wrap this in a subquery:
SELECT SUM(value) AS total FROM (
SELECT value FROM table
ORDER BY value DESC
LIMIT 3
);
To have MySQL return highest 3 values and their Sum in a 4th row, you can use (aasuming that id is the Primary Key of the table):
SELECT id, SUM(value)
FROM
( SELECT id, value
FROM TableX
ORDER BY value DESC
LIMIT 3
) AS tmp
GROUP BY id
WITH ROLLUP ;
Related
I am doing a sort based on names, for that, I am using rownum, since the sorting is according to names I am trying to fetch next and prev id according to rownum column.
I am trying to subtract subquery result near WHERE clause but its not showing any result. If I place a numeric value directly i.e 3 it will subtract properly.
I have tried typecasting the return value but it still gives me empty result set.
SELECT inner_id, first_name, rank as sort_id
FROM
(
SELECT id as inner_id, first_name, (#rownum:=#rownum+1) rank
FROM ympa_applications, (SELECT #rownum:=0) t
ORDER BY first_name ASC
) t WHERE rank = ( (SELECT DISTINCT CAST(rank AS UNSIGNED) FROM ympa_applications WHERE inner_id=132) - 1 )
How can I find the most frequent value in a given column in an SQL table?
For example, for this table it should return two since it is the most frequent value:
one
two
two
three
SELECT
<column_name>,
COUNT(<column_name>) AS `value_occurrence`
FROM
<my_table>
GROUP BY
<column_name>
ORDER BY
`value_occurrence` DESC
LIMIT 1;
Replace <column_name> and <my_table>. Increase 1 if you want to see the N most common values of the column.
Try something like:
SELECT `column`
FROM `your_table`
GROUP BY `column`
ORDER BY COUNT(*) DESC
LIMIT 1;
Let us consider table name as tblperson and column name as city. I want to retrieve the most repeated city from the city column:
select city,count(*) as nor from tblperson
group by city
having count(*) =(select max(nor) from
(select city,count(*) as nor from tblperson group by city) tblperson)
Here nor is an alias name.
Below query seems to work good for me in SQL Server database:
select column, COUNT(column) AS MOST_FREQUENT
from TABLE_NAME
GROUP BY column
ORDER BY COUNT(column) DESC
Result:
column MOST_FREQUENT
item1 highest count
item2 second highest
item3 third higest
..
..
For use with SQL Server.
As there is no limit command support in that.
Yo can use the top 1 command to find the maximum occurring value in the particular column in this case (value)
SELECT top1
`value`,
COUNT(`value`) AS `value_occurrence`
FROM
`my_table`
GROUP BY
`value`
ORDER BY
`value_occurrence` DESC;
Assuming Table is 'SalesLT.Customer' and the Column you are trying to figure out is 'CompanyName' and AggCompanyName is an Alias.
Select CompanyName, Count(CompanyName) as AggCompanyName from SalesLT.Customer
group by CompanyName
Order By Count(CompanyName) Desc;
If you can't use LIMIT or LIMIT is not an option for your query tool. You can use "ROWNUM" instead, but you will need a sub query:
SELECT FIELD_1, ALIAS1
FROM(SELECT FIELD_1, COUNT(FIELD_1) ALIAS1
FROM TABLENAME
GROUP BY FIELD_1
ORDER BY COUNT(FIELD_1) DESC)
WHERE ROWNUM = 1
If you have an ID column and you want to find most repetitive category from another column for each ID then you can use below query,
Table:
Query:
SELECT ID, CATEGORY, COUNT(*) AS FREQ
FROM TABLE
GROUP BY 1,2
QUALIFY ROW_NUMBER() OVER(PARTITION BY ID ORDER BY FREQ DESC) = 1;
Result:
Return all most frequent rows in case of tie
Find the most frequent value in mysql,display all in case of a tie gives two possible approaches:
Scalar subquery:
SELECT
"country",
COUNT(country) AS "cnt"
FROM "Sales"
GROUP BY "country"
HAVING
COUNT("country") = (
SELECT COUNT("country") AS "cnt"
FROM "Sales"
GROUP BY "country"
ORDER BY "cnt" DESC,
LIMIT 1
)
ORDER BY "country" ASC
With the RANK window function, available since MySQL 8+:
SELECT "country", "cnt"
FROM (
SELECT
"country",
COUNT("country") AS "cnt",
RANK() OVER (ORDER BY COUNT(*) DESC) "rnk"
FROM "Sales"
GROUP BY "country"
) AS "sub"
WHERE "rnk" = 1
ORDER BY "country" ASC
This method might save a second recount compared to the first one.
RANK works by ranking all rows, such that if two rows are at the top, both get rank 1. So it basically directly solves this type of use case.
RANK is also available on SQLite and PostgreSQL, I think it might be SQL standard, not sure.
In the above queries I also sorted by country to have more deterministic results.
Tested on SQLite 3.34.0, PostgreSQL 14.3, GitHub upstream.
Most frequent for each GROUP BY group
MySQL: MySQL SELECT most frequent by group
PostgreSQL:
Get most common value for each value of another column in SQL
https://dba.stackexchange.com/questions/193307/find-most-frequent-values-for-a-given-column
SQLite: SQL query for finding the most frequent value of a grouped by value
SELECT TOP 20 WITH TIES COUNT(Counted_Column) AS Count, OtherColumn1,
OtherColumn2, OtherColumn3, OtherColumn4
FROM Table_or_View_Name
WHERE
(Date_Column >= '01/01/2023') AND
(Date_Column <= '03/01/2023') AND
(Counted_Column = 'Desired_Text')
GROUP BY OtherColumn1, OtherColumn2, OtherColumn3, OtherColumn4
ORDER BY COUNT(Counted_Column) DESC
20 can be changed to any desired number
WITH TIES allows all ties in the count to be displayed
Date range used if date/time column exists and can be modified to search a date range as desired
Counted_Column 'Desired_Text' can be modified to only count certain entries in that column
Works in INSQL for my instance
One way I like to use is:
select *<given_column>*,COUNT(*<given_column>*)as VAR1 from Table_Name
group by *<given_column>*
order by VAR1 desc
limit 1
i have a problem my database looks like this DATABASE STRUCTURE
i want to sort the lboard and ignore the repeated values
like this REQUIRED OUTPUT
can anyone help me?
You could use GROUP BY to get single result for one user and MAX function to get its maximum score
the query will be like this
SELECT id, exam_id, user_id, MAX(lboard) FROM `leaderb` GROUP BY user_id ORDER BY lboard DESC
SELECT id, exam_id, user_id, MAX( lboard )
FROM wp_watu_takings WHERE exam_id = 3
GROUP BY user_id
ORDER BY MAX( lboard ) DESC
I have a question about how to select the second, third, fourth, and fifth largest number in a table. To select the biggest row I use:
$max = SELECT max(money) FROM table
Right now I want to specify $second_max, $third_max, $fourth_max and $fifth_max.
Does someone know how to change my previous SQL select max() easy to specify second max, third max etc...?
I do not want to use:
select money from table order by money desc limit 5;
Because I want them all in different variables.
select money from table order by money desc LIMIT 5
Probably the easiest way is to get them on separate rows:
select t.money
from table t
group by t.money
order by money desc
limit 5;
The next easiest thing is to put them in a comma-separated list:
select group_concat(money order by money desc) as monies
from (select t.money
from table t
group by t.money
order by money desc
limit 5
) T
Just this:
SELECT money
FROM yourtable
ORDER BY money DESC
LIMIT 5
You'll get a 5-record result set, ordered by the top money values - assuming you actually have 5+ records in the table.
USE SQL
select money from table order by money desc limit 5;
The five rows are there as max, secondary,... value of money.
In ORACLE you could do the following :
SELECT *
FROM (
SELECT ADRESSID,
ROW_NUMBER() OVER (ORDER BY ADRESSID DESC) AS ROW_NUM
FROM ADRESSTABLE
) t
WHERE ROW_NUM = 1
OR ROW_NUM = 3
OR ROW_NUM = 5;
I have a table with the following structure - steamid, itemid, eventid, value , all just ints or big ints that log hourly data I insert. So a user most likely has multiple entries with the same steamids, itemids, and even values.
I'm trying to get the 100 top values - but the same item must not repeat.
What I have so far is
SELECT itemid,value,steamid
FROM $table
GROUP BY itemid
ORDER BY value DESC
LIMIT 0,100
Which gives me this data set (sample only here):
itemid value steamid
=================================================
599291414 66397 76561198032389066
779150329 62882 76561198001229760
773965297 51895 76561198014617403
332883551 43201 76561197992659494
333434836 40880 7656119799359013
However, this for some reason ignores the true largest value listed in the table. I'm not sure how else I can format this so that I can ignore duplicate entries with the same itemid and steamid. I don't think I can group by steamid because then it would ignore other items that could be associated with a steamid.
Here's the first few if I select without grouping.
itemid value steamid
=====================================
1011809265 753665 76561198010314894
376615188 101684 76561197989760193
478937438 83448 76561198010314894
478937438 83448 76561198010314894
376662587 72693 76561197989760193
376662587 72693 76561197989760193
599291414 66454 76561198032389066
599291414 66454 76561198032389066
599291414 66454 76561198032389066
Insight appreciated and I'll gladly answer any questions that may help in figuring this out.
Try
SELECT * FROM (
SELECT itemid,value,steamid FROM $table GROUP BY itemid ORDER BY value DESC
) as tbl1
GROUP BY itemid, value
LIMIT 0,100
This should unique itemid, but then give you only the single values.
Try Adding Distinct.
SELECT DISTINCT itemid, value, steamid
FROM $table
ORDER BY value DESC
LIMIT 0,100