Count considering sum of 3 columns - mysql

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

Related

Mysql Sum Conditional

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;

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;

Select the same column multiple times with MySQL

Assuming I have something like this :
MySQL Table
Date | Name | Val
22/11 | a | 1
22/11 | b | 2
22/11 | a | 3
22/11 | a | 4
23/11 | b | 1
23/11 | a | 2
23/11 | a | 3
23/11 | a | 5
I need a query to have on one column the sum of the values for each day when Name = 'a' and an other column for the sum of all the values (for each day too).
With my example, the result would be something like this :
Date | a.Total | Total
22/11 | 8 | 10
23/11 | 10 | 11
I tried something like this :
SELECT date, SUM(Val) AS a.Total, SUM(Val) AS Total FROM tbl1 Where Name = 'a'
The point is that I need to specify a WHERE clause to get the "a.total" values (WHERE Name = 'a') but I don't want it to be apply to get the total.
I also tried queries with Left Join but it didn't work.
Any help is much appreciated.
You should use GROUP BY and CASE inside of the first SUM()
SELECT date,
SUM( CASE WHEN Name='a'
THEN Val
ELSE 0
END) AS a_Total,
SUM(Val) AS Total
FROM tbl1
GROUP BY `Date`
SQLFiddle demo
This is a type of problem called cross-tabbing (see https://www.simple-talk.com/sql/t-sql-programming/creating-cross-tab-queries-and-pivot-tables-in-sql/)
What you're after is the use of a CASE statement to allow you to sum values only when a condition is met.
SELECT date, SUM(CASE WHEN Name='a' then Val end) AS a.Total, SUM(Val) AS Total FROM tbl1 GROUP BY date

MySQL count per item

I'm currently trying to make a mysql query that will count the number of zeros and ones per item, in the following way:
Table:
ID | PollID | Value
------------------------------------
1 | 1 | 1
2 | 1 | 1
3 | 2 | 0
4 | 2 | 1
5 | 1 | 0
And the result I want is:
Poll | one | zero
----------------------------------
1 | 2 | 1
2 | 1 | 1
Thanks for the help!
This is the shortest possible answer in MySQL because it supports boolean arithmetic.
SELECT PollID,
SUM(value = 1) AS `One`,
SUM(value = 0) AS `Zero`
FROM tableName
GROUP BY PollID
SQLFiddle Demo
select z.pollid,z.ones,s.zeros
from (select a.pollid,count(a.value) as ones from test a
where a.value=1
group by a.pollid) z
left join
(select b.pollid,count(b.value) as zeros from test b
where b.value=0 group by b.pollid) s
on z.pollid=s.pollid;
try this
select table.pollid,
Switch(table.value Like 1, 1)AS one,
Switch(table.value Like 0, 1)AS zero
from table
group by pollid

MySQL: How to return only one row based on criteria within a resultset

I have the following table:
id | group | value
1 | 1 | 10
2 | 1 | 20
3 | 1 | 30
4 | 0 | 20
5 | 0 | 20
6 | 0 | 10
I want to return the highest value where the group is 1 (=30) and all of the values where the group is 0, into one resultset.
I have to do this in one statement, and I guess I should use an IF statement within a SELECT statement, but I can't work out how. Can anyone help to point me in the right direction?
(select max(value) from the_table where group = 1)
union
(select value from the_table where group = 0)
If (group +value) is unique, you can also do it without union (as proposed by Ray Toal)
SELECT a.value
FROM table1 a
WHERE a.`group`=0 or (a.`group`=1 AND a.value =
(SELECT MAX(value) FROM table1 b WHERE b.`group`=1))