I have a MySQL table like below.
ID X Y Z
1 1 1 1
2 7 4 2
2 9 4 3
3 2 3 1
4 2 2 1
4 2 2 2
5 3 3 1
I want to select X where ( ID=2 and Y=4 and Z Maximum).
I have do this code but cant understand where to add the MAX function.
$check_x=mysql_query("SELECT X from Hamdun_Soft where ID='2' AND Y='4'");
Please help me with MySQL.
There already was right variant if you need only one X with maximum Z
SELECT
X
FROM
Hamdun_Soft
WHERE ID = '2'
AND Y= '4'
ORDER BY Z DESC
LIMIT 1
And variant with subquery if there may be not only one X with maximum Z.
SELECT
X
FROM
Hamdun_Soft
WHERE ID = '2'
AND Y = '4'
AND Z = (SELECT MAX(Z) FROM Hamdun_Soft WHERE ID = '2' AND Y = '4')
P. S.
And don't forget what mysql_* family of function is deprecated now. You can use mysqli_* or PDO.
SELECT
X
FROM
Hamdun_Soft
WHERE ID = '2'
AND Y= '4'
ORDER BY Z DESC
LIMIT 1
Related
I want to generate a counter with a condition. By id i want to generate a counter that start from 1 and whenever column changer change from x to y or y to x add 1 to the counter created.
id changer
1 x
1 x
1 y
1 x
1 y
2 y
2 x
2 y
3 x
3 y
3 x
3 y
3 y
Expected result is :
id changer counter
1 x 1
1 x 1
1 y 2
1 x 3
1 y 4
2 y 1
2 x 2
2 y 3
3 x 1
3 y 2
3 x 3
3 y 4
3 y 4
Interestingly, if you only need the maximum value, this makes things much simpler as you only need to create a counter for the rows where a change occured, and then get the max value:
WITH tCountAtChange as
(SELECT *, ROW_NUMBER() OVER (PARTITION BY id ORDER BY date_time) + 1 as aCounter
FROM
(SELECT UniqueNbr, date_time, id
,CASE WHEN LAG(changer) OVER (PARTITION BY id ORDER BY date_time) <> changer THEN 'YES' ELSE 'NO' END as ChangeFlag
FROM MyTable) a
WHERE ChangeFlag = 'YES')
SELECT MyTable.id, COALESCE(MAX(aCounter), 1) as MaxCounter
FROM MyTable
LEFT JOIN tCountAtChange
ON MyTable.UniqueNbr = tCountAtChange.UniqueNbr
GROUP BY MyTable.id
Result :
id MaxCounter
1 4
2 3
3 4
You will need a date_time field or some sort field for the ORDER BY clauses, and a UniqueNbr (Unique ID) that you can use for the join, or you could always create one in an other cte with a ROW_NUMBER function.
I got following table(tablea):
x y z
--------------------
3 0 2
0 0 3
0 3 2
0 0 1
0 0 4
i want to select all values in a specific row. The row has to fulfill specific requirements in that order (sorted by priority)
z has to be in a specific range (between 2 and 4)
x as low as possible
y as low as possible
z as low as possible
I got following code which fulfills requirement 1, 2 and 4:
Select x, MIN(z) AS z
FROM tablea
WHERE x = (SELECT MIN(x) FROM tablea where z between 2 AND 4)
and z between 2 AND 4;
I would also need requirement 3, what do i need to add in my code for that (or alternatively, completely different code)?
The result in my example should be x=0 y=0 z=3.
Edit: Second example
x y z
--------------------
3 0 2
1 0 3
0 3 2
0 0 1
0 2 4
Here x=0 y=2 z=4 should be selected.
Use order by and limit:
select t.*
from t
where z between 2 and 4
order by x, y, z
limit 1;
This chooses the lowest value of x, y, z in order (lowest x, then lowest y if there are ties, then lowest z if there are ties). If you have some other definition, you can include that. For instance, for the lowest sum:
select t.*
from t
where z between 2 and 4
order by x + y + z
limit 1;
You can use derived tables to get the min values of x and y. Then join the results to the original table and select the min value of z.
SQL Fiddle
select t.x, t.y, min(t.z) as z
from tablename t
join (select min(x) as minx from tablename where z between 2 and 4) mx
on t.x = mx.minx
join (select min(y) as miny from tablename where z between 2 and 4) my
on t.y = my.miny
where t.z between 2 and 4
group by t.x,t.y
my table 'product':
id:
1
2
3
4
my another table 'productSet'
id / specification
1 / 1
1 / n
1 / n
1 / n
1 / 1
2 / 1
2 / n
2 / n
3 / 1
3 / n
3 / n
3 / 1
3 / n
3 / n
3 / n
4 / 1
4 / n
4 / n
4 / 1
product.id = productSet.id
count(productSet WHERE id LIKE '1') = 5
count(productSet WHERE id LIKE '1' AND specification LIKE 'n') = 3
percent = (100-((3*100)/5)) = 40%
count(productSet WHERE id LIKE '2') = 3
count(productSet WHERE id LIKE '2' AND specification LIKE 'n') = 2
percent = (100-((2*100)/3)) = 33,33%
count(productSet WHERE id LIKE '3') = 7
count(productSet WHERE id LIKE '3' AND specification LIKE 'n') = 5
percent = (100-((5*100)/7)) = 28,57%
count(productSet WHERE id LIKE '4') = 4
count(productSet WHERE id LIKE '4' AND specification LIKE 'n') = 2
percent = (100-((2*100)/4)) = 50%
I NEED RESULT DESC
4 (50%)
1 (40%)
2 (33,33%)
3 (28,57)
Please help me with this command. I don't know where to start.
This is an easy aggregate query to make. Good use of GROUP BY with SUM() and COUNT() gives you a precise answer, and very efficiently. http://sqlfiddle.com/#!2/95546c/8/0
SELECT id,
COUNT(*) AS total_count,
SUM(specification <> 'n') AS non_n_count,
100.0 * SUM(specification <> 'n') / COUNT(*) AS percent
FROM productSet
GROUP BY id
ORDER BY 4 DESC
The trick here is to realize that the expression specification <> 'n' yields a value of either zero or one for each row in the table where there's a non-NULL value of specification. So summing up those values gives you a count of rows meeting the criterion.
ORDER BY 4 DESC orders the result set, descending, by its fourth column.
run_software
runID Release
1 X
2 X
3 Y
4 Z
5 Y
6 X
7 Y
8 Z
9 X
10 Z
testcase
testID runID Result
T_1 1 PASS
T_2 1 FAIL
T_3 1 PASS
T_4 2 PASS
T_5 2 FAIL
T_6 3 PASS
T_7 4 FAIL
T_8 3 PASS
T_9 3 FAIL
T_10 5 PASS
T_11 5 FAIL
T_12 3 PASS
1) From run_software table we can understand X software run on runID's 1, 2,6,9
2) Take runID - 1 and come to testcase table.
Here we have 7 testID's with runID 1.
From these 7 testID's we need to measure the TC count and percentage of PASS/FAIL using group by Result and runID.
AIM: Ultimate aim is to find the latest 3 Release's and its runID's with PASS percentage by considering max testcase count.
Eg. if 'X' release executed on runID's 1, 2, 3, 4 with each 10, 12, 9, 21 testcases respectively, we should consider runID 4 for release 'X' to measure the 'PASS %'
Desired OutPut:
considering PASS% is > 60
Release runID Result PASS %
X 1 PASS 66.66
Y 3 PASS 75
Z 4 FAIL 0
To be understanding
Release 'X' has runID's - 1, 2 , 6, 9 with 3, 2, 0 , 0 TestID's respecively
Hence, X finalized runID '1' with 66.66 as PASS% (2 PASS & 1 FAIL)
Your question is not very clear actually, but it should be something like that I guess:
SELECT r.Release, r.runID, (CASE WHEN subq.PassPerc >= 60 THEN 'PASS' ELSE 'FAIL' END) AS Result, subq.PASSPerc
FROM run_software AS r
INNER JOIN (SELECT p.runID, (100 * p.passed / t.total) AS PASSPerc
FROM (SELECT runID, COUNT(*) AS passed FROM testcase WHERE Result = 'PASS' GROUP BY runID) AS p
INNER JOIN (SELECT runID, COUNT(*) AS total FROM testcase GROUP BY runID) AS t ON t.runID = p.runID
GROUP BY p.runID, p.passed, t.total) AS subq
ON subq.runID = r.runID
GROUP BY r.Release, r.runID, subq.PASSPerc
I'm coding a video game and using MySQL to display an icon showing the player where treasure is located. The database stores the locations by their X and Y on a square map. I would like to also count the number of items on each square.
So given a table such as this
Id x y
== == ==
1 2 3
2 3 2
3 3 2
4 4 4
5 4 4
6 4 4
I would like to return something to the effect of
x y count
= = =====
4 4 3
3 2 2
2 3 1
Use GROUP BY clause for results.
Select x, y, count(*) as 'count' from mytable group by x, y
What about concatenate your 2 columns and use DISTINCT ?
Select x, y, count(DISTINCT CONCAT(x, '_', y)) as 'count' from mytable group by x, y
SELECT x,y,SUM(1) FROM table GROUP BY x,y;