Mysql Sum Conditional - mysql

I'm trying to get the total cost from the table below and if Include_Extra is set, use the value in the corresponding Extra_Seat_Cost column. I'm new to mysql, so any help would be greatly appreciated!
+-----------+-----------------+---------------+
| ID | Cost | Extra_Seat_Cost | Include_Extra |
+-----------+-----------------+---------------+
| 3 | 20 | 15 | 1 |
| 4 | 10 | 5 | 0 |
+----+------+-----------------+---------------+
The result should yield total = $45

You can use SUM with CASE WHEN:
SELECT SUM(Cost + CASE WHEN Include_Extra = 1 --if Include_Extra is bool delete = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
SqlFiddleDemo
I've added COALESCE in case Extra_Seat_Cost can be nullable. number + NULL produces NULL.
If you have grouping column use:
SELECT group_column, SUM(Cost + CASE WHEN Include_Extra = 1
THEN COALESCE(Extra_Seat_Cost,0)
ELSE 0 END) AS total
FROM table_name;
GROUP BY group_column;

Related

SQL query using stored procedure

I have a table in MySQL with three columns that need an average of each row of three columns using the stored procedure:
Id | One | Two | Three
----+-------+-------+-------
1 | 10 | 30 | 20
2 | 50 | 60 | 20
3 | 60 | 0 | 40
The average must be determined using a stored procedure, not a normal query.
I have this SQL query
select
id,
(ifnull(one, 0) + ifnull(two, 0) + ifnull(three, 0)) /
((one is not null) + (two is not null) + (three is not null)) as average
from table
I want that to look like this, with a MySQL query:
Id | Average
---+--------
1 | 20
2 | 43.3
3 | 50
Maybe not the best solution but you could use:
select id,
SUM(coalesce(one,0)+coalesce(two,0)+coalesce(three,0)) /
count(CASE WHEN one != 0 and one is not null then 1 ELSE NULL END)
+ count(CASE WHEN two != 0 and two is not null then 1 ELSE NULL END)
+ count(CASE WHEN three != 0 and three is not null then 1 ELSE NULL END ) as average
from my_table
group by id;
Result:
id average
1 20.0000
2 43.3333
3 50.0000
4 35.0000
Demo
This query excludes Null and 0 values
coalesce
Full Procedure
DELIMITER//
CREATE PROCEDURE average()
BEGIN
select id, SUM(coalesce(one,0)+coalesce(two,0)+coalesce(three,0)) /(count(CASE WHEN one != 0 and one is not null then 1 ELSE NULL END) + count(CASE WHEN two != 0 and two is not null then 1 ELSE NULL END) + count(CASE WHEN three != 0 and three is not null then 1 ELSE NULL END)) as average from my_table group by id ;
END
DELIMITER ;

How to Query Maximum Integer Value from Substring of Varchar Column with Condition

I want to get the total maximum number of column CODE which the maximum is defined by the last five digits from mybarcode column.
mybarcode | code | judge | create_date |
-------------+------+--------+-------------+
M71X400001 | 7 | pass |
M71X400002 | 7 | pass |
M71X400005 | 7 | pass |
M71X400010 | 7 | pass |
M81X400001 | 8 | pass |
M81X400002 | 8 | pass |
M81X400007 | 8 | pass |
M91X400001 | 9 | pass |
M91X400003 | 9 | pass |
```
Example:
>The maximum value of 7 from CODE column is 10 ( from M71X4'00010')
>The maximum value of 8 from CODE column is 7 ( from M81X4'00007')
>The maximum value of 9 from CODE column is 3 ( from M91X4'00003')
The result should be 10+7+3=20.
And want display in the result table below.
```
SELECT DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail
**??? as number**
from MYTABLE
where MONTH(create_date) = '04' and YEAR(create_date) = '2019'
GROUP BY DAY
Result Table
day | pass | fail | number |
--------+------+--------+----------+
1 | 9 | 0 | 20 |
2 | 9 | 0 | ?? |
3 | 9 | 0 | ?? |
I think you need to do group by two times. Please try below code -
For MySQL -
SELECT
DAY,
SUM(pass),
SUM(fail),
SUM(max_barcode)
FROM (
SELECT
DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail,
Code,
CAST(MAX(SUBSTRING(mybarcode, 5)) AS SIGNED) AS max_barcode
FROM MYTABLE
WHERE MONTH(create_date) = '%s' and YEAR(create_date) = '%s'
GROUP BY DAY, Code
) AS CTE
GROUP BY DAY;
FOR MS SQL Server -
;WITH CTE AS (
SELECT
DAY,
SUM(CASE WHEN judge = 'pass' then 1 else 0 end) pass,
SUM(CASE WHEN judge = 'fail' then 1 else 0 end) fail,
Code,
max_barcode = cast(max(right(mybarcode, 5)) as int)
FROM MYTABLE
WHERE MONTH(create_date) = '%s' and YEAR(create_date) = '%s'
GROUP BY DAY, Code
)
SELECT
DAY,
SUM(pass),
SUM(fail),
SUM(max_barcode)
FROM CTE
GROUP BY DAY;

How to manipulate SQL Query output

I would like to know if it's possible for example I have one sql table with the following results
+----+---------+--------+--------+
| ID | Name | Number | Active |
+----+---------+--------+--------+
| 1 | Jessica | 12 | 0 |
| 2 | Andrew | 23 | 1 |
| 3 | Jason | 53 | 0 |
+----+---------+--------+--------+
And I would like to change the active field to 0 = No | 1 = Yes but only in the results, I don't want to change the value of the row, is it possible to make one query that can do it?
Well with the answers bellow I managed to get it changed but now how can I echo the value in php?
SELECT *, case when Active =0 then 'No' when Active =1 then 'Yes' end as Expr1,
FROM table
Should it be like: $isActive = $rows['Expr1'];
NVM the line above is working.
Just use a case statement for translating 1 = yes and 0 = No like this
select ID
,Name
,Number
,case when Active=0 then 'No'
when Active=1 then 'Yes'
end as active_y_n
from table
use case when
select Id,name,number,
case Active when 0 then 'No'
when 1 then 'Yes' end as active_status
from t
A particularly simple way would use elt():
select Id, name, number,
elt(Active + 1, 'No', 'Yes') as as active_status
from t

Count considering sum of 3 columns

I have 3 column (prod1 , prod2 , prod3 ) with TYPE : DOUBLE
id | prod1 | prod2 | prod3 |
1 | 1.3 | 2.6 | 2.8 |
2 | 0.8 | 3.4 | 0 |
3 | 0 | 0 | 1.3 |
4 | 0 | 0 | 0 |
What I want is COUNT() of 3 columns
SELECT count(prod1,prod2,prod3) AS allc
FROM `testprd`
WHERE id =3
I know above code is wrong
SHOULD GIVE RESULT
allc
-------
1
As prod1 and prod2 have 0 values
Similarly when id = 4 count should be 0 as all column for resp id have zero value ,but when id = 1 then count should be 3
Hence I taught count for each id columns and then sum of all , will result me solution but am not able to reach it.
BELOW IS WHAT I HAVE TRIED
SELECT count(prod1) AS a,
count(prod2) AS b,
count(prod3) AS c
FROM `testprd`
WHERE id =3
Result:
a | b | c
-------------
1 1 1
But should be:
a | b | c
-------------
0 0 1
So sum(a+b+c) = 1
Hence count for id = 3 is 1
What am I doing wrong?
You could get the result you want to have with
SELECT
(prod1 !=0 ) + (prod2 != 0) + (prod3 != 0) AS allc
FROM `testprd`
WHERE id = 3
The aggregate function COUNT counts rows in a table or not null rows in a certain column, but not the values that are not equal zero in a set of columns.
COUNT(expr)
Returns a count of the number of non-NULL values of expr in the rows
retrieved by a SELECT statement. The result is a BIGINT value.
COUNT() returns 0 if there were no matching rows.
Use
SELECT id, if(prod1+prod2+prod3>0,1,0) from testprd;
For all columns separated it should be:
SELECT id, if(prod1>0,1,0), if(prod2>0,1,0), if(prod3>0,1,0) from testprd;
Use a simple query like this
SELECT
IF(prod1 > 0,1,0)+IF(prod2 > 0,1,0)+IF(prod3 > 0,1,0) as Total
FROM test
WHERE id = 3;
SQL Fiddle Demo
OUTPUT
| TOTAL |
|-------|
| 1 |
Just check if that product is different from zero then sum all counts like:
SELECT if(prod1!=0,1,0) +
if(prod1!=0,1,0) +,
if(prod1!=0,1,0) AS ct
FROM `testprd`
WHERE id =3
How about:
SELECT
count(*) AS allc FROM `testprd`
WHERE
id =3 AND
0 < ANY (prod1, prod2, prod3);
COUNT counts the rows slected by your SELECT statement, it does not sum up the column values.
SELECT prod1 + prod2 + prod3 AS mySum
FROM `testprd`
WHERE id =3;
See the MySQL doc concerning Arithmetic Operators and COUNT

Mysql query for counting winnings and losings

I have a table with some columns. One of the column registers if the row is true or false. E.g if you lost the game, this is set to 0 else 1.
What I want to count is the amount of 1 in row. The table looks like this:
+-----------+
|game_status|
|-----------|
|00000000 |
|-----------|
|00000000 |
|-----------|
|00000001 |
|-----------|
|00000001 |
|-----------|
|00000001 |
|-----------|
| ... |
+-----------+
So if you count it by hand the result would be:
starting from 0:
lost(0) -> 0 - 1 =
lost(0) -> -1 - 1 =
won(1) -> -2 + 1 =
won(1) -> -1 + 1 =
won(1) -> 0 + 1 =
Result = 1
So how do I get this result using mysql queries? I have tried using count but it counts all ones or zeros.
Thanks in advance.
I think I got a little bit confused when I accepted the answer and I do apologize for that. What I think I forgot to say is the result of the query should look like this:
+--------+------+
| 1 | -1 |
|--------|------|
| 2 | -2 |
|--------|------|
| 3 | -1 |
+---------------+
There the incremental column is the amount of games played...
You could count the wins and subtract the count of the loses.
select (select count(status) from table1 where status = 1) -
(select count(status) from table1 where status = 0)
try:
SELECT COUNT(DISTINCT CASE WHEN Column = 0 THEN 1 END) AS TotalZeros,
COUNT(DISTINCT CASE WHEN Column = 1 THEN 1 END) AS TotalOnes
FROM YourTable
Or much simpler way is if your column only holds either 0 or 1 just select count of either one of them and subtract it from the total rows which will give you the count of other.
Use SUM instead of COUNT. COUNT finds the number of items regardless of their value, SUM actually totals up the integer values.
Look here for reference
http://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
SELECT sum(CASE game_status WHEN 1 THEN 1 ELSE -1 end) FROM table_name;