SQL, Select Max 5 From Field - sql

I'm using Visual Basic 2010 for a project of mine. It's a database project and I'm trying to do this:
I want to get 5 rows based on the 5 largest values in the field "percentage". Is It possible to do that with an SQL command?

Yes.
Select the percentage column, and order by it (desc). Then limit by 5.
SELECT percentage FROM x ORDER BY percentage DESC LIMIT 5

For SQL Server
SELECT TOP 5 percentage_col1 FROM Table1
ORDER By percentage_col1 DESC;

Related

Get count of records of the top 3 rows and compare the counts

In SQL Server 2016, I have a query as such:
SELECT [Report_date], count(distinct indv_id)
FROM
[dbo].[STG_TABLE] group by report_date order by report_date desc
I get the results as below:
Report_date (No column name)
2020-08-21 47918
2020-08-12 968065
2020-07-31 977804
Now I want to compare the difference between the counts in each row. If the difference is more than 10%, then I need to send an email out in the SSIS package.
How can I go through each row and calculate the difference? I want to look at the first row and compare it with the second row.
You question seems to be about calculating the ratios between rows. For that, use lag(). To get the ratio:
SELECT [Report_date], COUNT(DISTINCT indv_id),
(COUNT(DISTINCT indv_id) * 1.0 / LAG(COUNT(DISTINCT indv_id)) OVER (ORDER BY report_date))
FROM [dbo].[STG_TABLE]
GROUP BY report_date
ORDER BYreport_date DESC;
I'm not sure what results you want, but this is the basic information.

SQL query to get the value from score

I have a table like this:
I need a SQL query to get the value if I have a 2.3 score. So it means the result 2.3 should be between 2.0/2.5/2.3 and value should be 90. How can I do this?
select value
from your_table
where 2.3 >= score
order by score
limit 1
Depending on your DB engine you need to change the limit 1 part. To get only the first record DB engines have different methods.
Try this
select value
from tbl_in_between
where 2.3 between score and (score+0.5)
In SQL SERVER you can do it like:
DECLARE #Score NUMERIC(2,1)=2.3
SELECT TOP 1
FROM [YourTable]
WHERE Score<=#Score
ORDER BY Score
You can try...
SELECT TOP 1 value
FROM tbl_in_between
WHERE Score <= 2.3
ORDER BY Score

How to generate a custom sequential number with SQL Server 2012

Is there any way to generate a custom sequential number like the following?
I want the Number to be incremented with grouping by the Code and Year.
Code Year Number
A 2016 1
A 2016 2
A 2016 3
B 2016 1
B 2016 2
C 2016 1
A 2017 1
A 2017 2
Any suggestion would be appreciated.
EDIT
Sorry, I was too ambiguous what I want. I want to generate the unique number when I query, so if I ask a new number in the above data context with Code:A and Year:2017, I want the Number to be 3. I guess to get the Number properly in a future I need to save the Code and Year with the Number.
Use ROW_NUMBER to assign Number per Code,Year grouping.
SELECT *,
Number = ROW_NUMBER() OVER(PARTITION BY Code, [Year] ORDER BY (SELECT NULL))
FROM tbl
Replace SELECT NULL with the column you want the order to be based from.

Sql Statement that returns only the top 5 highest values in desc order, but the 5th en 6th value are the same?

Im struggling to retrieve only 5 values from my sql Statement. I want to retrieve the 5 highest numbers in a single column. The trouble is that some of the numbers repeat. When the 5th and the 6th highest numbers are the same my sql statement retrieves 6 values and not 5. If the 5th and the 6th number are the same i dont need the 6th number from the sql. Can anyone please help?
Here is the column:
Number
10
9
5
5
3
3
2
1
My results are:
Number
10
9
5
5
3
3
I would like to retrieve only first 5 numbers excluding the 6th number.
Here is my sql:
10
9
5
5
3
3
SELECT TOP 5 Number
FROM tbl
Order by Number Desc
You can group them and display like this:
SELECT TOP 5 Number
FROM tbl
GROUP BY Number
Order by Number Desc
or you can use DISTINCT:
SELECT DISTINCT TOP 5 Number
FROM tbl
Order by Number Desc
Just order by number desc, and limit it to 5
select Number from table
order by Number desc
limit 5;
Note, that is MySQL syntax, i have no idea if it is "standard" for other DBs. You don't specify what DB you are using.

Query return rows whose sum of column value match given sum

I have tables with:
id desc total
1 baskets 25
2 baskets 15
3 baskets 75
4 noodles 10
I would like to ask the query with output which the sum of total is 40.
The output would be like:
id desc total
1 baskets 25
2 baskets 15
I believe this will get you a list of the results you're looking for, but not with your example dataset because nothing in your example dataset can provide a total sum of 40.
SELECT id, desc, total
FROM mytable
WHERE desc IN (
SELECT desc
FROM mytable
GROUP BY desc
HAVING SUM(total) = 40
)
Select Desc,SUM(Total) as SumTotal
from Table
group by desc
having SUM(Total) > = 40
Not quite sure what you want, but this may get you started
SELECT `desc`, SUM(Total) Total
FROM TableName
GROUP BY `desc`
HAVING SUM(Total) = 40
From reading your question, it sounds like you want a query that returns any subset of of sums that represent a certain target value and have the same description.
There is no simple way to do this. This migrates into algorithmic territory.
Assuming I am correct in what you are after, group bys and aggregate functions will not solve your problem. SQL cannot indicate that a query should be performed on subsets of data until it exhaust all possible permutations and finds the Sums that match your requirements.
You will have to intermix an algorithm into your sql ... i.e a stored procedure.
Or simply get all the data from the database that fits the desc then perform your algorithm on it in code.
I recall there was a CS algorithmic class I took where this was a known Problem:
I believe you could just adapt working versions of this algorithm to solve your problem
http://en.wikipedia.org/wiki/Subset_sum_problem
select desc
from (select desc, sum(total) as ct group by desc)