select highest value in mysql [duplicate] - mysql

This question already has answers here:
Select max value of each group
(8 answers)
How to select a maximum value row in mysql table
(7 answers)
Closed 4 years ago.
So I have a database that has some values like this:
Level , Indicator
4 1
3 2
4 3
3 4
4 5
3 6
What I want to do is to select the highest level value in every indicator.
Is there any sql query that I can use that will generate a result like this?
Level , Indicator
4 1
4 3
4 5
If not, can you help me out using php and mysqli? Thank you so much.

To get Indicators having just highest level value -
select distinct Indicator, level
from your_table
where level = (select max(level) from your_table)
Also, you can use group by to get highest level for each Indicator value -
select Indicator, max(Level) from your_table group by Indicator

Related

I want to do a query in mySQL from horizontal to vertical [duplicate]

This question already has answers here:
MySQL transpose columns sum into rows with column names as a value [duplicate]
(1 answer)
MySQL converting columns to rows
(1 answer)
Closed 4 months ago.
I have this table:
ColumnA
ColumnB
ColumnC
ColumnD
1
2
1
3
3
4
3
4
I want a to do a select like this:
Column id
sum
ColumnA_
4
ColumnB_
6
ColumnC_
4
ColumnD_
7
The porpuse of this is to get a column with the comumn id and another with the sum of each column
I've tried to get the sum of the columns but I couldn't get it like vertically
SELECT SUM(ColumnA ) as ColumnA_, SUM(ColumnB ) as ColumnB_, SUM(ColumnC ) as ColumnC_, SUM(ColumnD ) as ColumnD_ FROM table_name
and I get this (not what I want):
ColumnA_
ColumnB_
ColumnC_
ColumnD_
4
6
4
7

Retrieve different data from one column in MySQL [duplicate]

This question already has answers here:
Count boolean field values within a single query
(4 answers)
Closed 4 months ago.
Suppose a table have a column with values like:
column
3
1
1
3
4
1
2
I want to retrieve that how many times there is 1 in column and how many times > 1.
Is there anyway to do this within one query?
I want the data to be fetched like this:
one | greater_than_one
3 | 4
Thanks in advance.
Simple:
select sum(col1=1) as one ,
sum(col1>1) as greater_than_one
from test_tbl;
https://dbfiddle.uk/348f5YOK

How do I create a running sum for the count of the number of items associated with a value in a query? [duplicate]

This question already has answers here:
SQL Server: create an incremental counter for records in the same year?
(3 answers)
Closed 1 year ago.
I am trying to use a query to create a Bill of Materials list that gives an item number to a material. I have a column for Material and BOMPart. The Material column gives a list of item numbers that repeat for each item in the Bill of Materials. The BOMPart column lists each part of the item in Material. So Material 1 is created using items a, b, and c. I would like the third column, ItemNo, to start at 1 for each Material and count each BOMPart associated to the material. It should then reset to 1 for the next Material. Any suggestions? I am still pretty new to Access. Here is what I would like the columns to look like, I replaced my actual material numbers for simplicity.
Material
BOMPart
ItemNo
1
a
1
1
b
2
1
c
3
2
a
1
2
f
2
3
g
1
3
h
2
3
i
3
4
k
1
4
m
2
You want to use row_number for this. I am not certain this works in Access.
select Material
, BOMPart
, Row_Number() over (partition by Material order by BOMPart)
from xxxx

How to select median value? [duplicate]

This question already has answers here:
Simple way to calculate median with MySQL
(48 answers)
Closed 4 years ago.
Could anybody give me a hint how to find median value for "tax" during 12.04.18 - 16.04.18:
user_id login_time tax
3 2018-04-15 16625000
5 2018-04-16
6 2018-04-17 296470000
6 2018-04-16 192519750
6 2018-04-15 4455500
6 2018-04-13 17125
6 2018-04-12 120180000
7 2018-04-18 24060000
7 2018-04-17 42959500
The result equals 16625000 (because there is NULL value. We need to use it as 0).
Thank U for attention to my question!
The median is the value
located exactly in the middle of an odd dataset.
Or the average of the two middle values in an even dataset.
So, by considering this two cases, the first you need is the count of datarows. Then, you have to decide (simple case) if you pick the value in the middle, or if you need the average of two values (Don't forget to apply sorting prior to selecting the actual values):
I would use a little "code" to achieve this:
Pseudo-Code:
1.) SELECT count(id) AS val FROM myTable WHERE datetime ... //$val=9
2.) Programming language: $lim = floor($val/2); // $lim=4
if odd($val){
3.) SELECT tax FROM myTable WHERE datetime [...] ORDER BY tax LIMIT $lim,1
}
else if even($val){
3.) Programming language: $lim -=1; // if $val was 10, we want row 4 and 5
4.) SELECT AVG(tax) AS tax FROM
(SELECT * FROM myTable WHERE datetime [...] ORDER BY tax LIMIT $lim,2) AS tmp
}
[...]
echo "Median is: ". $row["tax"];

Does anyone know how to Stuff in groups? [duplicate]

This question already has answers here:
Can I concatenate multiple MySQL rows into one field?
(16 answers)
Closed 9 years ago.
I am trying to use the stuff function in MS SQL to stuff certain info. Here is the example:
Number Value
1 1
2 1
3 1
1 2
2 2
3 2
1 3
2 3
I would like to stuff the column so that only one record will display as following:
Value Number
1 1,2,3
2 1,2,3
3 1,2
Please note that there are a like n-Numbers and n-Values.
You can use GROUP_CONCAT for this. For example:
SELECT `Value`, GROUP_CONCAT(DISTINCT `Number` ORDER BY `Number`)
FROM `yourTable`
GROUP BY `Value`