Generate a counter with a condition is met - mysql

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.

Related

MySQL -- Select the minimum value of a column after selecting the minimum value of another column, and be in a specific range

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

getting data in single query

Say I have the following:
id bill vote
1 x 1
2 y 1
3 y 0
4 z 1
5 x 1
What I want the query to return is:
bill vote(1) vote(0)
x 2 0
y 1 1
z 1 0
vote(1) is count of data (1), same applies to vote(0)
select bill, sum(vote) as vote_1, sum(1-vote) as vote_0
from tablename
group by bill
The first sum is used to sum all 1's. The second one to sum all 0's (1-1 = 0, 1 - 0 = 1!)

Selecting data where a column is maximum

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

How to fetch specific number of row on basis of score

Have to show on specific count . Means i have to show max score value 4 times then less than that score to 3 times and so on
I have table like:-
ID Score
1 1
2 1
3 1
4 1
5 2
6 2
7 2
8 3
9 3
10 4
11 4
12 4
and I am expecting output like:-
Score
1
1
1
2
2
3
4
Try this:
SELECT Score
FROM
(SELECT a.Score, a.ID, count(*) as RN FROM TableName a
JOIN TableName b ON a.Score = b.Score AND a.ID >= b.ID
GROUP BY a.Score, a.ID) T
WHERE RN>1
Result:
SCORE
1
1
1
2
2
3
4
4
An example in SQL Fiddle.
Explanation:
In the inner query, we are selecting a row number partitioned by ID and order by score. And then we select Score from the inner query whose Row number is greater than 1 (inorder to omit 1 record).
SELECT x.*
FROM my_table x
JOIN my_table y
ON y.marks = x.marks
AND y.id <= x.id
GROUP
BY x.marks
, x.id
HAVING COUNT(0) <= MAX(4-x.marks)
ORDER
BY marks DESC,id;

MySQL Need return a count of unique combinations of cols

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;