i need help to create a sql query that can find the smallest value in 1 row , and display it in the last column, like this table.
id
out
mid
in
Smallest
1
200
100
50
50
2
100
150
50
50
3
200
100
250
100
4
50
100
150
50
5
50
100
100
50
6
20
200
100
20
7
-
-
100
100
8
150
-
100
100
this is my query :
On MySQL you may use the scalar LEAST() function:
SELECT id, `out`, mid, `in`, LEAST(`out`, mid, `in`) AS Smallest
FROM yourTable;
If your database doesn't have a LEAST function, we can use a CASE expression as an alternative:
SELECT id, `out`, mid, `in`,
CASE WHEN `out` < mid AND `out` < `in` THEN `out`
WHEN mid < `in` THEN mid
ELSE `in` END AS Smallest
FROM yourTable;
Side note: Both IN and OUT are reserved MySQL keywords, and you should avoid naming your columns with them.
Related
Given I have following table
Id
FileSizeMB
1
100
2
100
3
100
4
100
5
100
6
100
I want to select oldest records exceeding a cumulative value, in this case say 500.
So something like this
Id
Cumulative_FileSizeMB
6
100
5
200
4
300
3
400
2
500
1
600
I want to select only records with id 2 and 1 as they are >= 500.
Goal is to delete them.
Thanks
For anyone with same problem.
I have reached this solution using Mysql window functions,
and also there is no need to declare a variable for cumulative total
SELECT * from (
SELECT
id,
FileSizeMB,
SUM(FileSizeMB) OVER (ORDER BY id DESC) AS TotalFileSizeMB
FROM table
) as t1
WHERE TotalFileSizeMB > 500
I have table like this
Order D.O Cost Maintenance Total
ORD-0005 1 100 50 150
ORD-0005 2 50 120 170
ORD-0006 3 200 100 300
ORD-0006 4 150 50 200
Now I want to have total SUM based on Column 'ORDER'
So the result will look like this
Order D.O Cost Maintenance Total
ORD-0005 1 100 50 320
ORD-0005 2 50 120 320
ORD-0006 3 200 100 500
ORD-0006 4 150 50 500
The total value is sum from all cost+maintenance refer to Order Column
Thank you..really appreciated it
You could use a sub-query that calculates the SUM and then join this to the output:
SELECT a.`Order`,
a.`D.O`,
a.`Cost`,
a.`Maintenance`,
b.`Total`
FROM `myTable` a
INNER JOIN (
SELECT `Order`,
SUM(`Total`) AS `Total`
FROM `myTable`
GROUP BY `Order`
) b ON a.`Order` = b.`Order`
ORDER BY a.`Order`
This will calculate the total value for each order in the sub-query, and then include that total value in the output for each Order.
Output:
Order
D.O
Cost
Maintenance
Total
ORD-0005
1
100
50
320
ORD-0005
2
50
120
320
ORD-0006
3
200
100
500
ORD-0006
4
150
50
500
Seen here in this working fiddle.
Please help me with writing a query for the following condition. I have a table which I have listed below
ID Wt1 Wt1_Type Wt2 Wt2_Type Wt3 Wt3_Type Wt4 Wt4_Type
--------------------------------------------------------------
1 200 1 220 1 300 2 400 3
2 100 4 150 3 100 5 120 1
3 100 3 110 1 200 5 100 4
I want a query to sum all the the weights (wt1, wt2, wt3, wt4) grouped on the weight type (wt1_type, wt2_type, wt3_type, wt4_type).
The output should look like
Wt_type Total
1 650
2 300
3 650
4 200
5 300
Can someone please help me draft a mysql query to get this result ?
Thanks
You can try below - using union all and subquery
select Wt_Type,sum(Wt) as total from
(
select Wt1_Type as Wt_Type,Wt1 as Wt from tablename
union all
select Wt2_Type ,Wt2 from tablename
union all
select Wt3_Type ,Wt3 from tablename
union all
select Wt4_Type ,Wt4 from tablename
)A group by Wt_Type
Rather than giving the answer by #fa06, which should work for you, I am going to suggest using a better table design. Here is how you should be storing your data:
ID Type Wt
-------------
1 1 200
2 4 100
3 3 100
4 1 220
5 3 150
6 1 110
7 2 300
8 5 100
9 5 200
10 3 400
11 1 120
12 4 100
Note that there is a single column which stores the type and a single column for that type's weight. Now your expected output just requires a very simple query:
SELECT Type, SUM(Wt) AS Total
FROM yourTableUpdated
GROUP BY Type;
Databases are really good at performing operations across rows, much less so across columns.
Use this it should be work
select Wt_Type,sum(Wt) as total from ( select Wt1_Type as Wt_Type,Wt1 as Wt from tablename union all select Wt2_Type ,Wt2 from tablename union all select Wt3_Type ,Wt3 from tablename union all select Wt4_Type ,Wt4 from tablename )A group by Wt_Type
I am trying to do a sum calculation with an if condition. However I do not seem to get it right. I would like to do the following with the following table:
id amt1 amt2
1 100 50
2 80 -50
3 40 30
4 45 50
5 85 20
I would like to Sum(amt1)+(amt2) only if amt2<0
So the totals should be:
100
30
40
45
85
Is there any way of doing this in a query?
Kind regards,
Michel
In MySQL, false == 0 and true == 1, so:
select amt1 + (amt2 < 0) * amt2
MySQL Query :
SELECT IF(amt<0,SUM(amt1+amt2),amt1) as totals from tb;
Hi there you can do the following:
SELECT id, IF(amt2 < 0,sum(amt1 + amt2),amt1) as result FROM tb GROUP BY id;
I want to find number of repeated value in group using single select query.
I can't use the column in group by for which I want to find number of occurrence in some group.
e.g.
obsid Name Value
1 Ronak 120
2 Ronak 125
3 Ronak 120
4 Pankti 130
5 Pankti 130
6 Sanket 140
7 Aakash 140
8 Unnat 120
Now I want to develop select query in mysql that give me below result
obsid Name Value Value_occurrence
1 Ronak 120 2
2 Ronak 125 1
4 Pankti 130 2
6 Sanket 140 1
7 Aakash 140 1
8 Unnat 120 1
SELECT min(obsid) AS obsid, Name, Value, COUNT(*) AS Value_occurence
FROM yourTable
GROUP BY Name, Value
Group by the desired columns and use COUNT to count the rows in each group:
SELECT MIN(`obsid`) AS `obsid`, `Name`, `Value`, COUNT(*) AS Value_occurrence
FROM yourtable
GROUP BY `Name`, `Value`
Try this online demo which shows your example data and the results for the above query.