select max value and another column - sql

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

Related

Get unique value as per sorting key up to certain count (SQL)

I would like to get 5 unique value as per sorted key.
Below is the table example:-
Expected result is 5,10,3,9,1. First unique 5 value from table.
Thanks.
You can do the following for Oracle:
select * from (
select value
from table
group by value
order by min(serial)
) where rownum <=5
Try this(For MySQL):
SELECT DISTINCT value FROM <table-name> ORDER BY serial LIMIT 5;
For SQL Server
select TOP 5 value from table1
group by value
order by min(Serial)

Select Minimum value of column A where Column B=1

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);

How to get the value of max length in sql server

i would like to ask you if there is a way to get the value of maxlength of column in ms sql server.
For example in the table A we have:
id | value
----+--------
1 | 0123
2 | 00034567
3 | 547
The desired result for this data set is 00034567.
Wherever i have searched for this problem i get the answer select max(len(value)) which is not what i need, because it gives the max number of characters of the value and not the value itself.
Any ideas?
SELECT TOP 1 t.value
FROM table AS t
ORDER BY LEN(t.value) DESC
Sort by length and take the first row:
select top 1 value
from mytable
order by len(value) desc
If you want ties:
select value
from mytable
where len(value) = (select max(len(value)) from mytable)
Or more efficient but SQL Server specific:
select value from (
select value,
rank() over (order by len(value) desc) rank
from mytable) x
where rank = 1
See SQLFiddle for last one.
the sql you use is correct, u just have to cast it to an nvarchar

Row with the highest ID

You have three fields ID, Date and Total. Your table contains multiple rows for the same day which is valid data however for reporting purpose you need to show only one row per day. The row with the highest ID per day should be returned the rest should be hidden from users (not returned).
To better picture the question below is sample data and sample output:
ID, Date, Total
1, 2011-12-22, 50
2, 2011-12-22, 150
The correct result is:
2, 2012-12-22, 150
The correct output is single row for 2011-12-22 date and this row was chosen because it has the highest ID (2>1)
Assuming that you have a database that supports window functions, and that the date column is indeed just date (and not datetime), then something like:
SELECT
* --TODO - Pick columns
FROM
(
SELECT ID,[Date],Total,ROW_NUMBER() OVER (PARTITION BY [Date] ORDER BY ID desc) rn
FROM [Table]
) t
WHERE
rn = 1
Should produce one row per day - and the selected row for any given day is that with the highest ID value.
SELECT *
FROM table
WHERE ID IN ( SELECT MAX(ID)
FROM table
GROUP BY Date )
This will work.
SELECT *
FROM tableName a
INNER JOIN
(
SELECT `DATE`, MAX(ID) maxID
FROM tableName
GROUP BY `DATE`
) b ON a.id = b.MaxID AND
a.`date` = b.`date`
SQLFiddle Demo
Probably
SELECT * FROM your_table ORDER BY ID DESC LIMIT 1
Select MAX(ID),Data,Total from foo
for MySQL
Another simple way is
SELECT TOP 1 * FROM YourTable ORDER BY ID DESC
And, I think this is the most simple way!
SELECT * FROM TABLE_SUM S WHERE S.ID =
(
SELECT MAX(ID) FROM TABLE_SUM
WHERE CDATE = GG.CDATE
GROUP BY CDATE
)

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 :)