How to select column and it's max - mysql

I have a mySQL table like:
ID Value
1 123
2 321
3 31
4 234
5 123
6 32
7 77
What I need to get is a table with additional column:
ID Value Maximum
1 123 321
2 321 321
3 31 321
4 234 321
5 123 321
6 32 321
7 77 321
But I have no idea how I should build a query.

Join your table with a subquery that gets the maximum.
SELECT t1.id, t1.value, t2.maximum
FROM yourTable AS t1
CROSS JOIN (SELECT MAX(value) AS maximum FROM yourTable) AS t2

Related

Daily Sales from Total Sales

I have a database that looks like this:
ID
Sale_Date(YYYY-MM-DD)
Total_Volume
123
2022-01-01
0
123
2022-01-02
2
123
2022-01-03
5
456
2022-04-06
38
456
2022-04-07
40
456
2022-04-08
45
I want to get a daily sale column from Total Volume. which is just by subtracting the total volume on date x with total volume on date x-1 for each id.
ID
Sale_Date(YYYY-MM-DD)
Total_Volume
Daily_Sale
123
2022-01-01
0
0
123
2022-01-02
2
2
123
2022-01-03
5
3
456
2022-04-06
38
38
456
2022-04-07
40
2
456
2022-04-08
45
5
My initial attempt was using a rank function and self join but that didnt turn out correct.
with x as (
select
distinct t1.ID,
t1.Sale_Date,
t1.Total_volume,
rank() over (partition by ID order by Sale_Date) as ranker
from t t1 order by t1.Sale_Date)
select t2.ID, t2.ranker, t2.Sale_date, t1.Total_volume, t1.Total_volume - t2.Total_volume as Daily_sale
from x t1, x t2 where t1.ID = t2.ID and t2.ranker = t1.ranker-1 order by t1.ID;
You should use:
the LAG window function to retrieve last "Sale_Date" value
the COALESCE function to replace NULL with "Total Volume" for each first rows
Then subtract Total_Volume from the previous value of Total_Volume and coalesce if the value of the LAG is NULL.
SELECT *,
COALESCE(`Total_Volume`
-LAG(`Total_Volume`) OVER(PARTITION BY `ID`
ORDER BY `Sale_Date(YYYY-MM-DD)`), `Total_Volume`) AS `Daily_Sale`
FROM tab
Check the demo here.

how to select the row where sum reach 1000?

id | amount
1 | 96
2 | 0.63
3 | 351.03
4 | 736
5 | 53
6 | 39
7 | 105
8 | 91
I want to get the row where sum(amount) reach 1000
please note only the row that trigger 1000
This query should do what (I think) you want:
select id, (select sum(amount)
from table1 t1
where t1.id <= table1.id) as total
from table1
having total >= 1000
limit 1
For your sample table, it gives
id total
4 1183.66

create new column and set increment by step of 3 rows

I have table1, i want get table2 ? increment by step of 3 rows.
I want create same |B value for first 3 rows, then increment +1 for second's 3 rows
table1
ID |A
1 125
2 412
3 567
4 567
5 485
6 458
7 656
8 856
9 456
table2
ID |A |B
1 125 101
2 412 101
3 567 101
4 567 102
5 485 102
6 458 102
7 656 103
8 856 103
9 456 103
In MySQL you can use something like this:
SET #c := -1;
SELECT id, A, (#c := #c+1) DIV 3 + 101 AS B FROM table1
You can use either create table from select or insert from select.
You can achieve it using MySql's mathematical functions, e.g.:
select t.id, t.a, 101 + floor((#rn:=#rn+1)/3) as B
from temp t, (SELECT #rn:=-1) t2;
Here is the SQL Fiddle.

Update value in a column based on another column in the same table in MYSQL

I have a table that is having 3 columns
vid - auto increment column
video_id - containing numbers
a_id - containing junk numbers
The table looks like below.
Vid Video_id a_id
101 1 3
102 1 3
103 5 3
104 5 3
105 5 3
106 11 3
107 11 3
108 11 3
109 11 3
110 11 3
I want to update a_id column values based on video_id values. Values in a_id should be updated as below.ex: If there are five 11 digit in video_id then the value in a_id should be updated 1 through 5.
Vid Video_id a_id
101 1 1
102 1 2
103 5 1
104 5 2
105 5 3
106 11 1
107 11 2
108 11 3
109 11 4
110 11 5
You can use user defined variables to give rank for each video group and then join with your real table by your auto increment column and update a_id accordingly
update t
join (
SELECT
Vid,
#r:= CASE WHEN Video_id = #g THEN #r+1 ELSE #r:=1 END a_id
,#g:=Video_id
FROM t,(SELECT #r:=0,#g:=0) t1
ORDER BY Video_id
) t1
on(t.Vid =t1.Vid)
set t.a_id = t1.a_id
Demo

Mysql left join or much simpler way?

I need to order data according to order index from other table. and order the data with the same 'id' according to entry date.
i cant figure it out how to join data and order using mysql command.
Table1
id name order
1 Ali 1
2 Cenk 3
3 Tan 2
Table 2
id tid m date
1 232 msj1 3
2 434 msj2 2
1 453 msj4 1
3 455 msj5 2
2 541 msj6 4
1 234 msj7 2
3 132 msj8 6
Needed query result
id tid m date
1 453 msj4 1
1 234 msj7 2
1 232 msj1 3
3 455 msj5 2
3 132 msj8 6
2 434 msj2 2
2 541 msj6 4
This should work:
select t2.id, t2.tid, t2.m, t2.date
from t2
left join t1 on t2.id=t1.id
order by t1.order
This orders by the ordering field from table 1.