Min Max Value in coloumn but with a certain condition - mysql

I have a MySql table with 3 coluomn : Nip, Bidang and Total.
I want to count min and max value of total with the same bidang. but i don't want to count min and max value for all coloumns.
Sample data:
NIP Bidang Total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
And the result column:
MIN
1
1
1
4
4
8
8

Simply do this:
SELECT * FROM
(
(SELECT Bidang FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(Total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
BIDANG MINVAL MAXVAL
A 1 10
A 1 10
A 1 10
B 4 7
B 4 7
C 8 9
C 8 9
See result in SQL Fiddle.
EDIT
To see the Total column also, add Total to first query.
SELECT * FROM
(
(SELECT NIP,Bidang,Total FROM TableName) T1 LEFT OUTER JOIN
(SELECT bidang, MIN(total) MinVal, MAX(Total) MaxVal
FROM TableName
GROUP BY Bidang) T2 ON T1.Bidang=T2.Bidang
)
Result:
NIP BIDANG TOTAL MINVAL MAXVAL
1 A 10 1 10
2 A 5 1 10
3 A 1 1 10
4 B 4 4 7
5 B 7 4 7
6 C 8 8 9
7 C 9 8 9
See result in SQL Fiddle.

Select bidang, min(total) MyMin, Max(total) myMax
From tableName
group by bidang
NIP bidang total
1 A 10
2 A 5
3 A 1
4 B 4
5 B 7
6 C 8
7 C 9
should return
A 1 10
B 4 7
C 8 9

Related

Self-referential table ordering in SQL

Given a table like this:
taskId
nextTaskId
1
2
2
3
3
6
4
5
5
NULL
6
7
7
8
8
4
I need the following order for output:
taskId
nextTaskId
1
2
2
3
3
6
6
7
7
8
8
4
4
5
5
NULL
Is there any way to do this via MySQL query?
Using recursive CTE -
WITH RECURSIVE
self_ref_cte ( taskid, nexttaskid )
AS
( SELECT taskid, nexttaskid
FROM self_ref WHERE taskid = 1
UNION ALL
SELECT s.taskid, s.nexttaskid
FROM self_ref_cte c JOIN self_ref s
ON s.taskid = c.nexttaskid
)
SELECT * FROM self_ref_cte;
DB fiddle here.

How to calulate SUM previous and current row value in MySQL table?

My table is "Activity_table", which have 4 columns. How can I create a function, which SUM each Peroson 2 previous and current activities?
My Activity_table
ID PID AID Act
1 1 1 12
2 1 2 32
3 2 1 5
4 1 3 21
5 2 2 12
6 2 3 19
7 1 4 11
8 2 4 6
PID-PersonID; AID-ActivitieID; Act-Activitie Value
My target:
ID PID AID Act SUM
1 1 1 12 12
2 1 2 32 44
3 2 1 5 5
4 1 3 21 65
5 2 2 12 17
6 2 3 19 36
7 1 4 11 64
8 2 4 6 37
Sum1=12; Sum2=32+12; Sum3=5; Sum4=21+32+12; Sum5=12+5; Sum6=19+12+5; Sum7=11+21+32; Sum8=6+19+12; Thank you,
To use the two previous and the current row,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND ID >= a.ID - 2
AND PID = a.PID)
FROM Activity_table a;
You are taking the SUM according to the PID right? Then the last two rows of your target should be modified as,
7 1 4 11 **76**
8 2 4 6 **42**
The most flexible query is for your requirement is,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND PID = a.PID)
FROM Activity_table a;
Then if you need only the flow of SUM of a particular PID, you can change it like this,
SELECT ID,PID,AID,Act,
(SELECT SUM(Act)
FROM Activity_table
WHERE ID <= a.ID
AND PID = a.PID)
FROM Activity_table a
WHERE PID = 2;
Result:
ID PID AID Act SUM
3 2 1 5 5
5 2 2 12 17
6 2 3 19 36
8 2 4 6 42
Sorry for the previous wrong Answers (deleted).
This produces the correct answer based on your example:
SET #LastPID = 0, #Act1 = 0,#Act2 = 0,#Act3 = 0;
SELECT ID,PID,AID,Act,`SUM` FROM
(
SELECT ID,PID,AID,Act,#Act3 := IF(#LastPID != PID, 0,#Act2),#Act2 := IF(#LastPID != PID, 0,#Act1), #Act1 := Act, #Act1 + #Act2 + #Act3 `SUM`, #LastPID := PID
FROM Activity_table
ORDER BY PID,ID
) sm
ORDER BY ID
;

update statement with aggregate function

Good day everyone..!:-)
I have this table tab where totalUsed is equal to the sum of all used values referenced to name
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 0
7 a 5 0
i have this code but it only copy totalUsed's adjacent used value
UPDATE tab
SET totalUsed=
(
SELECT SUM(used)
)
cid name used total
1 a 1 1
2 a 3 3
3 a 6 6
4 b 3 3
5 b 7 7
6 b 10 10
7 a 5 5
if used is set as 10 for cid 6, totalUsed should be 20
and for cid 7 it should be 15.
how to do it in mysql?
it should look like this.
cid name used total
1 a 1 1
2 a 3 4
3 a 6 10
4 b 3 3
5 b 7 10
6 b 10 20
7 a 5 15
thanks for help
:-)
In most dialects of SQL, you can do this using an update with a correlated subquery:
UPDATE tab
SET totalUsed = (SELECT SUM(used)
from tab tab2
where tab2.name = tab.name and
tab2.cid <= tab.cid
);
EDIT:
The above will not work in MySQL. You can do this instead:
UPDATE tab join
(select tab2.cid,
(SELECT SUM(used)
from tab tab3
where tab3.name = tab2.name and
tab3.cid <= tab2.cid
) as cum_used
from tab tab2
) tab2
on tab.cid = tab2.cid
SET tab.totalUsed = tab2.cum_used;

Mysql select to table 5 per line

how i can Select from mysql_db to table limit by 5. 5 per line.
I have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
i need:
1 6 11
2 7 12
3 8 13
4 9 14
5 10
or :
1 2 3 4 5
6 7 8 9 10
11 12 13 14
thank you.
Try to adapte this function according to your case :
<?php
$array=array('a','b','c','d','e','f','g');
function display($array,$limite){
$arrays= array_chunk($array, $limite);
foreach($arrays as $array){
echo implode(' ',$array).'<br/>';
}
}
display($array,2);
?>
Output of display($array,2);
a b
c d
e f
g
Output of display($array,3);
a b c
d e f
g
This query will solve your problem. Table named test is joined to himself two times on the interesting condition:
SELECT
t.num as num,
t1.num as num1,
t2.num as num2
FROM
test t
LEFT JOIN test t1 ON(t.num+5 = t1.num)
LEFT JOIN test t2 ON(t1.num+5 = t2.num)
ORDER BY num ASC
LIMIT 5

Unioning and Joining in 1 Query?

I have the following tables:
TABLE1:
A B C
1 2 3
2 4 6
3 6 9
TABLE2:
A B C
4 8 12
5 10 15
6 12 18
TABLE3:
A D
2 X
4 Y
6 Z
I need one query that gives:
A B C D
1 2 3
2 4 6 X
3 6 9
4 8 12 Y
5 10 15
6 12 18 Z
Is that possible?
I can do it in 2 queries, but the person I'm doing it for wants it in 1.
Thanks!
Try this (example on sqlfiddle):
SELECT x.a, x.b, x.c, d
FROM (
SELECT a, b, c FROM table1
UNION ALL
SELECT a, b, c FROM table2
) x
LEFT JOIN table3 ON ( table3.a = x.a )
Sure:
select v1.*, table3.d
from
(select table1.a, table1.b, table1.c
from table1
union all
select table2.a, table2.b, table2.c
from table2
) v1
left join table3 on v1.a = table3.a