SQL query to get the value from score - sql

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

Related

Fill in the most recent value highest value in the row field over the date column using tableau or sql

I have a data having 0,1,2 values in the row field and dates in increasing order in the column field and I would like the last '2' value keep constant moving forward in the dates. Please do let know a way to workaround. Example may 2027 has 2 and then 0 but I would like to have 2 in june 2027 and the rest of the dates.I would like to keep previousor beginning date values same but has the maximum value 2 in this case carry forward to the later dates.
Thank you.
Question
MS SQL Syntax:
SELECT TOP 1 value FROM YourTableName WHERE value <> 0 ORDER BY date_action DESC
MySQL Syntax:
SELECT value FROM YourTableName WHERE value <> 0 ORDER BY date_action DESC LIMIT 1

SQL, Select Max 5 From Field

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;

How do I write this SQL query in MS Access to

I have a list of records like so
ID---EffectiveDate---Rate
1----1/1/2011--------1.2
2----1/1/2012--------1.3
3----1/1/2013--------1.5
4----1/1/2014--------1.2
Given a date parameter, d1, I want to get the record with the latest effective date prior to d1. So, if d1 = 6/1/2012, I want to get the second record. How can I write a query like this in MS Access SQL?
I think the SQL query would looks something like this, based on your data:
SELECT TOP 1 EffectiveDate
FROM MyTableOfDates
WHERE EffectiveDate <= #MyInputDate#
ORDER BY EffectiveDate DESC
Try this,
select top 1 * from tablename where EffectiveDate<=d1 order by EffectiveDate desc

get previous from max value

I have folowing sql query an di want to get previous of max value from table.
select max(card_no),vehicle_number
FROM WBG.WBG_01_01
group by vehicle_number
Through this query i got each maximum card number of each vehicle.But i want to get previouse of that max.For example
if vehicle number has card number 21,19,17,10,5,6,1 and i want to get 19 from max function
Please anyone tell me how can i do this in sql.
Another idea would be to use analytics, something like this:
select
vehicle_number,
prev_card_no
from (
select
card_no,
vehicle_number,
lag(card_no) over
(partition by vehicle_number order by card_no) as prev_card_no,
max(card_no) over
(partition by vehicle_number) as max_card_no
FROM WBG.WBG_01_01
)
where max_card_no = card_no;
Of course, this doesn't take into account your seemingly arbitrary ordering from your question, nor would it work with duplicate maximum numbers.
try this one:
select max(card_no),vehicle_number
FROM WBG.WBG_01_01
where card_no < (Select max(card_no) from WBG.WBG_01_01 group by vehicle_number)
group by vehicle_number

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)