Find row wise max from two different columns with update function - mysql

Can anyone help me with UPDATE with max?
TABLE-A
EMP SALARY BONUS
A 100 110
B 50 80
C 30 20
D 80 50
E 30 40
I want Answer like
TABLE-A
EMP SALARY BONUS MAX
A 100 110 110
B 50 80 80
C 30 20 30
D 80 50 80
E 30 40 40

You could use GREATEST(MySQL):
UPDATE tableA
SET `max` = GREATEST(Salary, Bonus);
I would suggest adding computed column to avoid updating in the future:
CREATE TABLE tableA(Emp INT, Salary INT, BONUS INT,
`max` INT AS (GREATEST(Salary, Bonus)));
Rextester Demo

SQL Server you could use a CASE STATEMENT
select
EMP,
SALARY,
BONUS,
[Max] = case when Salary > Bonus then Salary else Bonus end
from
TABLE-A
Or on the update...
UPDATE TABLE-A
SET [Max] = case when Salary > Bonus then Salary else Bonus end

For SQL Server 2012+:
UPDATE TABLE_A
SET [MAX] = IIF (SALARY > BONUS, SALARY, BONUS);

Related

Running total with condition and always looking at the previous value

I want to do a sequential sum in a table by taking into consideration a few extra conditions.
We need to make sure that when the sum is taken sequentially so if a id has +40 then the next sum would be 130, if the next one is +1, the sum is still 130, now if the next one is -1 then the sum has to be 129.
100 needs to be added to the sum for the first time and from there on just the count should be added depending on condition.
We need to even cap the min value of sum so it can't be less than 70
I have tried the query below but it does not seem to look at the prior value.
Example that I tried:
create table tableA (id int not null, count int not null);
insert into tableA(id, count) values(1,11), (2,21),(3, -3); -- case 1
insert into tableA(id, count) values(1,35), (2,-3); -- case 2
insert into tableA(id, count) values(1,-45),(2,67); -- case3
Query tried:
select t.id, t.count,
case when (100+(select ifnull(sum(count),0) from tableA x where x.id <= t.id)) >= 130 then 130
when (100+(select ifnull(sum(count),0) from tableA x where x.id <= t.id)) <= 70 then 70
else (100+(select ifnull(sum(count),0) from tableA x where x.id <= t.id))
end as xxxx
from tableA t;
I expect my output to look like:
Case1 Result:
id count Sum
1 11 111
2 21 130
3 -4 126
Case2 Result:
id count Sum
1 35 130
2 -3 127
Case3 Result:
id count Sum
1 -45 70
2 67 137
THIS ANSWERS THE ORIGINAL VERSION OF THE QUESTION.
I think this does what you want:
select a.*, (#sum := least(#sum + count, 130)) as "sum"
from (select a.*
from tablea a
order by a.id
) a cross join
(select #sum := 0) params;
I don't understand where the 100 is coming from. It is not part of your explanation.
Here is a db<>fiddle that illustrates how this works using 30 as the limit (which seems to be your intention).

How to get second highest salary department wise?

Suppose we have some employees in each department.we have total 3 departments . Below is the sample source table named 'employee'
emp dept_id salary
A 10 1000
B 10 2000
C 10 3000
D 20 7000
E 20 9000
F 20 8000
G 30 17000
H 30 15000
I 30 30000
j 30 30000
k 30 17000
Here may same salary is exist in same department.
I use Wamp-server which has mysql-5.7.23
And I want to like:
B 10 2000
F 20 8000
G 30 17000
I think there are several way to solve the problem. Following solution from my side and works fine.
SELECT *
From employee e2
WHERE e2.salary = (SELECT distinct salary FROM employee where dept_id=e2.dept_id order by salary desc limit 1,1);
I need only second highest salary value with department wise which is the input array of next operation in my project. Finally I use
SELECT e2.dept_id, max(e2.salary)
From employee e2
WHERE e2.salary = (SELECT distinct salary FROM employee where dept_id=e2.dept_id order by salary desc limit 1,1)
group by e2.dept_id

Need help solving this SQL query to understand

write an sql to generate the report for employee dataset with give condition if average age >35 then states value is ok else notok dataset
id name age dept salary
1 tt 51 it 4000
2 kk 56 it 6000
3 mm 45 sales 7000
4 kk 25 sales 9000
5 op 24 hr 4000
6 op 24 hr 8000
output
dept avgage states
it 53.5 ok
sales 35 ok
hr 24 notok
Use this query.
SELECT a.dept,
a.avgage,
CASE
WHEN a.avgage >= 35 THEN 'ok'
ELSE 'notok'
END states
FROM (SELECT dept,
Avg (age) avgage
FROM employee
GROUP BY dept) a
ORDER BY avgage DESC;
Note: Please show some effort to understand and write a query on your own.

How to update the value of the least value row with the value of the maximum value row using MySQL?

I have a table which has a combination of item_id and payment_id columns as a key.
Every two rows have the same item_id value.
This is how the table looks.
item_id payment_id amount
1 140 1000
1 141 3000
2 141 500
2 145 600
3 4 4000
3 735 9000
How to subtract the amount value of the least payment_id row from the amount value of the maximum payment_id row (of the two rows with the same item_id) using MySQL?
To clarify, this is how the table I want.
item_id payment_id amount
1 140 1000
1 141 2000 : 3000 - 1000
2 141 500
2 145 100 : 600 - 500
3 4 4000
3 735 5000 : 9000 - 4000
Cheer!
You can get the new amount with this query:
select p1.item_id, p1.payment_id, p1.amount - (
select p0.amount
from payments p0
where p0.item_id = p1.item_id
and p0.payment_id < p1.payment_id
order by p0.payment_id
limit 1
) as new_amount
from payments p1
having new_amount is not null;
It will subtract the amount of the "last" row with the same item_id (if present).
You can then use that query in the UPDATE statement as derived table joined to your original table:
update payments p
join (
select p1.item_id, p1.payment_id, p1.amount - (
select p0.amount
from payments p0
where p0.item_id = p1.item_id
and p0.payment_id < p1.payment_id
order by p0.payment_id
limit 1
) as new_amount
from payments p1
having new_amount is not null
) p1 using (item_id, payment_id)
set p.amount = p1.new_amount;
Demo: http://rextester.com/DJD86481
UPDATE tt JOIN (SELECT item_id, MAX(payment_id) mp , (SUBSTRING_INDEX(GROUP_CONCAT(amount ORDER BY payment_id DESC),',',1) - SUBSTRING_INDEX(GROUP_CONCAT(amount ORDER BY payment_id ),',',1)) maxdif FROM tt GROUP BY item_id) s
ON tt.item_id=s.item_id
SET tt.amount =s.maxdif
WHERE tt.payment_id =s.mp AND tt.item_id=s.item_id;
SELECT * FROM tt;
See it working

Removing duplicate values from a dataset

I am developing an SSRS report with the following dataset (Table-1). I am grouping by Account and Period. My goal is to get the Total Expense and the Budget within a group. Because the Budget data is duplicated per group, I cannot use SUM() function for Budget. How do I remove the duplicates so the new dataset looks like this? (Table-2) Please advice. Thank you for your time.
Table-1
ID Account Period Expense Budget
1 100 201301 20 100
2 100 201301 30 100
3 100 201302 10 150
4 100 201302 40 150
5 200 ...................
Table-2
ID Account Period Expense Budget
1 100 201301 20 100
2 100 201301 30 NULL
3 100 201302 10 150
4 100 201302 40 NULL
5 200 ...................
If you really want to make duplicate budgets null try this update command
please check sqlfiddle http://sqlfiddle.com/#!3/1e619/11
Update table1
set budget = null
where id in
(
select aa.id from
(
select id,row_number()
over(partition by Budget order by Period) as rno
from table1
) aa
where rno > 1
);
select * from table1;
good luck.
I would use a windowed function if you have to do that grouping in SQL. If you can do it in SSRS just add a 'Row Grouping Parent' it would be better.
For SQL I would do this:
declare #Temp table ( ID int identity, Account int, period int, expense int, budget int);
insert into #Temp values (100, 201301, 20, 100),(100, 201301, 30, 100),(100, 201302, 10, 150),(100, 201302, 40, 150)
select *
from #Temp
select
ID
, Account
, Period
, Expense
, case when (row_number() over(partition by Budget order by Period) = 1) then Budget end as Budget-- only shows first occurrence of an order amount ordering by person
from #Temp