I would like to use two kinds of ordering.
From the highest earnings
From the highest losses.
My database looks like this
id
saldo
saldo_type
7
75,67
1
10
7
1
3
5,45
1
11
12,3
0
4
6,45
0
saldo_type:
1 = earnings
0 = losses
Expected result/output for sort by highest earnings:
id
saldo
saldo_type
7
75,67
1
10
7
1
3
5,45
1
4
6,45
0
11
12,3
0
and by highest losses:
id
saldo
saldo_type
11
12,3
0
4
6,45
0
3
5,45
1
10
7
1
7
75,67
1
So far I am stuck with such a code
SELECT id, saldo, saldo_type FROM `investitions`
ORDER BY
(CASE WHEN saldo_type = "1" THEN saldo END) DESC,
(CASE WHEN saldo_type = "0" THEN saldo END) ASC
Expected result/output:
sort by highest earnings
sort by highest losses
One way would be to convert the losses to negative values then you will achieve your goal. In your case, try the following:
SELECT id, saldo, saldo_type FROM `investitions`
ORDER BY (CASE WHEN saldo_type = "1" THEN saldo else -saldo END) desc
Cast saldo to decimal
SELECT id, saldo, saldo_type FROM `investitions`
ORDER BY saldo_type desc,cast(replace(saldo,',','.') as decimal(4,2)) desc -- sort by highest earnings
Adjust the precision and scale according to your data.
Thank you very much. I was able to build a working query using a piece of each answer.
Query looks like this:
SELECT id, saldo, saldo_type FROM `investitions` ORDER BY (CASE WHEN saldo_type = "1" THEN cast(replace(saldo,',','.') as decimal(12,2)) else -cast(replace(saldo,',','.') as decimal(12,2)) END) desc
Related
Their are two column umpire1 and umpire 2.
I need to find the name of the umpire who attend maximum matches doesn't matter he is umpire1 or umpire2 in that match his occurrence should count.
for e.g-- S Ravi
37 time as umpire1
57 time as umpire2
94 -- total
Output like
Umpire Total_count
S Ravi 94
Error in SQL statement: AnalysisException: cannot resolve 'level' given input columns:
line 2 pos 14;
'Aggregate [umpire1#2497], [umpire1#2497, 'sum(CASE WHEN ('level = S Ravi) THEN 1 ELSE 0 END) AS u1#2464, 'sum(CASE WHEN ('level = SJ Davis) THEN 1 ELSE 0 END) AS u2#2465, 'sum(('u1 + 'u2)) AS total#2466]
select umpire1,
sum(case when level ='umpire1' then 1 else 0 end) as u1,
sum(case when level ='umpire2' then 1 else 0 end) as u2,
sum(u1+u2) as total
from IPL_MATCHES group by umpire1;
I need to find the name of the umpire who attend maximum matches doesn't matter he is umpire1 or umpire2 in that match his occurrence should count.
I think umpire1 and umpire2 are two columns in your table and you want to unpivot and count:
select umpire, count(*)
from ((select umpire1 as umpire from ipl_matches
) union all
(select umpire2 as umpire from ipl_matches
)
) u
group by umpire;
If you want the umpire with the most matches, then add:
order by count(*) desc
limit 1
I want to have a sum of a column that is based if the value of another column is this/that. Here's my table structure
id leave_hours leave_type overtime_hours employee_id
1 7.6 1 0 3
2 5 2 0 3
3 0 0 2.3 3
I have this query already but I need to have a column that will only sum if leave type is 1 and if the leave type is 2
SELECT employee_id, SUM(overtime_hours) AS ot_hrs, SUM(leave_hours if leave_type == 1) AS
leave_1, SUM(leave_hours if leave_type == 2) AS leave_2
FROM table
GROUP BY employee_id
Result should be:
employee_id ot_hrs leave_1 leave_2
3 2.3 7.6 5
Thanks for your inputs.
You need to use conditional aggregation to sum the different leave_hours separately:
SELECT employee_id,
SUM(overtime_hours) AS ot,
SUM(CASE WHEN leave_type = 1 THEN leave_hours ELSE 0 END) AS leave_1,
SUM(CASE WHEN leave_type = 2 THEN leave_hours ELSE 0 END) AS leave_2
FROM `table`
GROUP BY employee_id
Output:
employee_id ot leave_1 leave_2
3 2.3 7.6 5
Demo on dbfiddle
Note that if you use floating point numbers to store the hours, you may need to ROUND the results.
you can use max() with case when
SELECT employee_id,
max(overtime_hours) AS ot,
max(CASE WHEN leave_type = 1 THEN leave_hours END) AS leave_1,
max(CASE WHEN leave_type = 2 THEN leave_hours END) AS leave_2
FROM table_name
GROUP BY employee_id
output
employee_id ot leave_1 leave_2
3 2.3 7.599999904632568 5
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
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
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