How to fetch MAX rank using case statement mysql - mysql

Already checked solutions related to my question but didn't get what I want to achieve
Here is my table structure (for ex. where table name is tbl_rank)
name rank
apple 10
banana 2
grapes 5
orange 1
chiku 0
pineapple 0
Now What I want as result is that the elements with 0 rank should be given the max rank and all the elements should appear in descending order of their rank (actually creating a new column as new_rank),
This is the desired result
name rank new_rank
apple 10 10
chiku 0 10
pineapple 0 10
grapes 5 5
banana 2 2
orange 1 1
To get the above result I am using this query
SELECT *, (CASE WHEN rank=0 THEN MAX(rank) ELSE rank END) as new_rank
FROM `tbl_rank` ORDER BY new_rank DESC
but all I am getting is only one row as result of the above query
No idea where I am doing wrong or why its returning only one row

Try this:
SELECT A.*, (CASE WHEN A.rank = 0 THEN B.rank ELSE A.rank END) AS new_rank
FROM tbl_rank A, (SELECT MAX(rank) AS rank FROM tbl_rank) AS B
ORDER BY new_rank DESC;

Related

mysql Table Sum of Positive and Negative Numbers

I have a column which has positive & negative numbers. Is it possible to have sum of all the positive numbers in Col1, negative numbers in Col2 and (Col1 - Col2) in Col3. Then sort by the last Col.
Current table New Table
ID Score ID Pos Neg Diff
1 3 3 5 0 5
1 1 1 4 1 3
1 -1 2 2 1 1
2 1
2 -1
2 1
3 3
3 1
3 1
This gives me the total but i would like to list the Pos and Neg numbers as well.
SELECT ID, SUM(Score) as total FROM results
GROUP BY ID ORDER BY total DESC
Just use a standard pivot query with separate conditional aggregations for the positive and negative numbers.
SELECT
ID,
SUM(CASE WHEN Score >= 0 THEN Score ELSE 0 END) AS Pos,
SUM(CASE WHEN Score < 0 THEN -1*Score ELSE 0 END) AS Neg,
SUM(Score) AS Diff
FROM results
GROUP BY ID
ORDER BY ID
Demo

How to achieve MySQL Query combining multiple COUNT() and GROUP BY

I have this data:
id date userid result
1 2015-05-01 1 a
2 2015-05-02 1 b
3 2015-05-03 1 b
4 2015-05-03 1 a
5 2015-05-04 1 a
I need to get users sorted by result:
id a b
1 1 1
2 1 1
You want conditional aggregation:
SELECT user_id, sum(result = 'win') AS wins, sum(result = 'loss') as losses
FROM table
GROUP BY user_id
ORDER BY wins DESC
LIMIT 4;
I would do it like this:
SELECT user_id,
SUM(CASE WHEN result='win' THEN 1 ELSE 0 END) AS wins,
SUM(CASE WHEN result='loss' THEN 1 ELSE 0 END) AS losses
FROM table1
GROUP BY user_id
ORDER BY wins DESC
LIMIT 4
Fiddle is here: http://sqlfiddle.com/#!9/00ac7/8

Selecting a count on two separate conditions

Let's say I have the following data.
id name_id completed
1 10 1
2 10 0
3 15 1
4 10 0
5 20 1
6 15 0
7 20 1
8 15 0
I'm trying to find a count by the name id, which is pretty simple
SELECT name_id, COUNT(*) FROM db
GROUP BY name_id
Now, I have a second component which I want to include in the query.
For name_id 10, I want to count just those values where completed is 1. For the other name_id's, I want to select them regardless of whether they are 0 or 1.
So I should end up with:
name_id count(*)
10 1
15 3
20 2
Name_id 10 only has a count of 1 because it's just the 1 which is completed, while the other counts include both 0 and 1.
Can anyone help with this task.
Thanks!
You can use a CASE expression inside of your aggregate function.
SELECT name_id,
sum(case
when name_id = 10
then case when completed = 1 then 1 else 0 end
else 1 end) Total
FROM db
GROUP BY name_id;
See SQL Fiddle with Demo.
Exclude the rows where name_id = 10 and completed = 0:
SELECT name_id, COUNT(*) FROM db
WHERE NOT (completed = 0 AND name_id = 10)
GROUP BY name_id
SELECT name_id, COUNT(*) FROM db
WHERE name_id != 10 or completed = 1
GROUP BY name_id
Count when name_id is not 10. If it is 10, count when completed = 1:
SELECT
name_id,
COUNT(CASE WHEN name_id <> 10 or completed = 1 THEN 1 END)
FROM db
GROUP BY name_id

Display results in a particular format MYSQL

I have a table like
ID Name Points
1 A 10
1 A 11
1 B 11
1 B 12
1 C 12
1 C 13
2 A 8
2 A 9
2 B 9
2 B 10
2 C 10
2 C 11
I want my output to look like the following
ID Average(A) Average(B) Average(C)
1 10.5 11.5 12.5
2 8.5 9.5 10.5
The following group by query displays the output but not in above format
Select Avg(Points),ID,name from table group by Name,ID
Thanks
Wrapping your existing query in a subquery will allow you to build out a pivot table around it. The `MAX()
aggregate's purpose is only to eliminate the NULLs produced by the CASE statement, and therefore collapse multiple rows per ID down to one row per ID with a non-NULL in each column.
SELECT
ID,
MAX(CASE WHEN Name = 'A' THEN Points ELSE NULL END) AS `Average (A)`,
MAX(CASE WHEN Name = 'B' THEN Points ELSE NULL END) AS `Average (B)`,
MAX(CASE WHEN Name = 'C' THEN Points ELSE NULL END) AS `Average (C)`
FROM (
SELECT ID, AVG(Points) AS Points, Name FROM yourtable GROUP BY Name, ID
) avg_subq
GROUP BY ID
Here is a live demonstration on SQLFiddle

MySQL query to count non-null values in a single row

I'm trying to put together a MYSQL query that will count the number of Non-Null (or better yet, non-zero) values in select fields in a single row and then sort from lowest to highest (based on the count). For example, I have a table with 5 fields... ID, Name, Score_1, Score_2, Score_3. I want to count how many times the value "0" exists in Score_1, Score_2 and Score_3 for each record, then sort from most non zero values to least.
ID Name Score_1 Score_2 Score_3
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
4 Mike 4 5 5
I assume the query has to look something like this...
Select ID, Name, Score_1, Score_2, Score_3 where (???) ORDER BY (???)
Output should look like this (ID 4 is displayed first since it has the least amount of non-zero entries)...
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
I'm somewhat new to mysql query's, so any help would be greatly appreciated. I thought the COUNT function would help, but that function appears to count columns from all rows. Perhaps there is a way to use the COUNT function and limit it to a singel row so it can be sorted by that row count?
This should do what you want:
SELECT ID, Name, Score_1, Score_2, Score_3
FROM Table1
ORDER BY (Score_1 = 0) + (Score_2 = 0) + (Score_3 = 0)
Result:
ID Name Score_1 Score_2 Score_3
4 Mike 4 5 5
1 Dan 8 7 0
2 Joe 0 0 3
3 Chris 0 0 0
try This:
Select id, Count1, Count2, Count3, Count4
From
(Select
Sum(Case When IsNull(Score_1,0) = 0 Then 1 Else 0 End) Count1,
Sum(Case When IsNull(Score_2,0) = 0 Then 1 Else 0 End) Count2,
Sum(Case When IsNull(Score_3,0) = 0 Then 1 Else 0 End) Count3,
Sum(Case When IsNull(Score_4,0) = 0 Then 1 Else 0 End) Count4
From Table
Group By Id) Z -- This column (Id) better not be the PK for this table!!!
Order By Count1 + Count2 + Count3 + Count4