Select Minimum value of column A where Column B=1 - sql

I have this table AVERAGE
I want to
select * from table where VhrNum=MIN(VhrNum) and EmptyOrNot=1
I've Tried this query but it's not working
select *
from Average
where Itmnum='1'
and VhrNum = (
select MIN(VhrNum)
from (
select *
from Average
where EmptyOrNot = '1'
)
)
In my case it's should select the third row

Why not just take the top 1 row and order by by the column?
SELECT TOP 1 *
FROM [table]
WHERE EmptyOrNot=1
ORDER BY VhrNum

Use TOP 1 with ORDER BY
select Top 1 * from table where EmptyOrNot=1
Order by VhrNum ASC
If you more than one record with min VhrNum value and you want all the tie records then use TOP 1 WITH TIES
select Top 1 With Ties * from table where EmptyOrNot=1
Order by VhrNum ASC

You can try like this
SELECT MIN(VhrNum) FROM table_name where EmptyOrNot=1;

Aggregate functions need a GROUP BY - or you could ORDER BY and select the first.
Aggregate won't let you do a SELECT *, which isn't really good practice anyway, and aggregate makes it clearer that you're actually trying to get the MIN of this. TOP 1/ORDER BY would let you do a SELECT *, but may be less immediately clear that all you're really trying to get is the MIN(VhrNum).
Aggregate:
SELECT MIN(VhrNum)
FROM Average
WHERE EmptyOrNot = 1
GROUP BY EmptyOrNot
Top:
SELECT TOP(1) VhrNum, *
FROM Average
WHERE EmptyOrNot = 1
ORDER BY VhrNum ASC

You can try this:
select top 1 * from table where VhrNum= (select min(VhrNum) from table);

Related

select max value and another column

id value
2 20
1 30
3 15
5 25
I have this table and want to get max value and id. When i use select id,max(value) i've got 2,30 but the right answer is 1,30. I really need to get your attention.
Thank you very much
select id, value from `table` order by value desc limit 1
You can try using subquery
select * from tablename
where value in (select max(value) from tablename)
SELECT *
FROM `tablename`
WHERE value=(SELECT
MAX(value) as value
FROM tablename)
You can check this query. Giving result according your requirement.
SELECT top 1 id,max(value) FROM table
GROUP BY id
ORDER BY max(value) desc
Use subquery
select *
from t
where value = (select max(value) from table)
From the #Thorsten comments i noted that 1st query will return all ties of max value.
Or you can use order by with limit if it is mysql
select *
from table
order by value desc
limit 1
And 2nd query will return only the single row of highest value

Issue with SQL "Order by"

I would like to compose a SQL query that for example selects the top 2 values of a sorted table using ORDER BY with a query below, but I'm getting unexpected results
select top 2 * from (select top 100 percent * from events order by dates desc) a ;
let's say for example, if the values of the table were 1,3,4
using
select top 100 percent * from events order by dates desc;
would give me 4,3,1
but with
select top 2 * from (select top 100 percent * from events order by dates desc) a;
I get 1,3 instead of 4,3
I was wondering why is that the case and am I missing something?
use order by in outer query as well
select top 2 * from (select top 100 percent * from events order by dates desc) a order by dates desc

SQL. Is there any efficient way to find second lowest value?

I have the following table:
ItemID Price
1 10
2 20
3 12
4 10
5 11
I need to find the second lowest price. So far, I have a query that works, but i am not sure it is the most efficient query:
select min(price)
from table
where itemid not in
(select itemid
from table
where price=
(select min(price)
from table));
What if I have to find third OR fourth minimum price? I am not even mentioning other attributes and conditions... Is there any more efficient way to do this?
PS: note that minimum is not a unique value. For example, items 1 and 4 are both minimums. Simple ordering won't do.
SELECT MIN( price )
FROM table
WHERE price > ( SELECT MIN( price )
FROM table )
select price from table where price in (
select
distinct price
from
(select t.price,rownumber() over () as rownum from table t) as x
where x.rownum = 2 --or 3, 4, 5, etc
)
Not sure if this would be the fastest, but it would make it easier to select the second, third, etc... Just change the TOP value.
UPDATED
SELECT MIN(price)
FROM table
WHERE price NOT IN (SELECT DISTINCT TOP 1 price FROM table ORDER BY price)
To find out second minimum salary of an employee, you can use following:
select min(salary)
from table
where salary > (select min(salary) from table);
This is a good answer:
SELECT MIN( price )
FROM table
WHERE price > ( SELECT MIN( price )
FROM table )
Make sure when you do this that there is only 1 row in the subquery! (the part in brackets at the end).
For example if you want to use GROUP BY you will have to define even further using:
SELECT MIN( price )
FROM table te1
WHERE price > ( SELECT MIN( price )
FROM table te2 WHERE te1.brand = te2.brand)
GROUP BY brand
Because GROUP BY will give you multiple rows, otherwise you will get the error:
SQL Error [21000]: ERROR: more than one row returned by a subquery used as an expression
I guess a simplest way to do is using offset-fetch filter from standard sql, distinct is not necessary if you don't have repeat values in your column.
select distinct(price) from table
order by price
offset 1 row fetch first 1 row only;
no need to write complex subqueries....
In amazon redshift use limit-fetch instead for ex...
Select distinct(price) from table
order by price
limit 1
offset 1;
You can either use one of the following:-
select min(your_field) from your_table where your_field NOT IN (select distinct TOP 1 your_field from your_table ORDER BY your_field DESC)
OR
select top 1 ColumnName from TableName where ColumnName not in (select top 1 ColumnName from TableName order by ColumnName asc)
I think you can find the second minimum using LIMIT and ORDER BY
select max(price) as minimum from (select distinct(price) from tableName order by price asc limit 2 ) --or 3, 4, 5, etc
if you want to find third or fourth minimum and so on... you can find out by changing minimum number in limit. you can find using this statement.
You can use RANK functions,
it may seem complex query but similar results like other answers can be achieved with the same,
WITH Temp_table AS (SELECT ITEM_ID,PRICE,RANK() OVER (ORDER BY PRICE) AS
Rnk
FROM YOUR_TABLE_NAME)
SELECT ITEM_ID FROM Temp_table
WHERE Rnk=2;
Maybe u can check the min value first and then place a not or greater than the operator. This will eliminate the usage of a subquery but will require a two-step process
select min(price)
from table
where min(price) <> -- "the min price you previously got"

Select entire row based on a max value of a field - without nesting

select *
from webOrders
where lastModifiedDate in (select max (lastModifiedDate) from webOrders)
Is there a simpler way without nesting the selects?
Doing something like this also results in an error:
select id, amount, quantity, max(lastModifiedDate) from webOrders.
There are a couple of approaches, depending on your needs.
If you only need one row returned, you can sort on the column of interest and return the top row. For example:
select top 1 *
from `order`
order by last_modified_date desc
if you use SQL Server or
select *
from `order`
order by last_modified_date desc
limit 1
if you use MySQL.
If you need to get one row per group, then you do typically have to use a subquery or a join.
select TOP 1 * from [order] order by lastModifiedDate desc
You can do select
SELECT TOP 1 * FROM XXX
http://www.w3schools.com/sql/sql_top.asp

sql query to find fifth record

How can i find the fifth record in a table using sql query?
If you are feeling argumentative, consider using "SELECT * FROM table LIMIT 1" and arguing that since SQL does not promise to return results in any particular order, the row returned is spiritually equivalent to the fifth, then show your tattoo: "The nth element of an unordered set is meaningless!"
SELECT TOP 1 * FROM (SELECT TOP 5 * FROM Table T ORDER BY Column ASC) ORDER BY Column Desc
If you are using SqlServer you could use the TOP keyword to achieve this.
select top 1 * from(
select top 5 * from myTable order by orderingColumn) as A
order by orderingColumn desc
If you are using Oracle this should work (however i am not able to test this now)
select *
from (
select *, row_number() over (order by orderingColumn) r
from items
)
where r = 5;
SELECT * FROM table LIMIT 1 OFFSET 4;
Fifth record only in MySQL
SELECT * FROM anytable LIMIT ORDER BY id LIMIT 4,1
For SQL Server (recent-ish incarnations, anyway) something like this should work:
SELECT
*
FROM
(
SELECT
*,
ROW_NUMBER() OVER (ORDER BY the_table.the_column) 'row_num'
FROM
the_table
) numbered_rows
WHERE
row_num = 5
However, I'd actually put my vote with Thomas L Holaday's answer :)