i want to make somthing like this :
SELECT COUNT(is_available = 1) / COUNT(*) FROM users;
but the condition of is_available = 1 doesn't reflect
it always gives result of "1"
Use CASE. For example:
select
sum(case when is_available = 1 then 1 else 0 end)
/
count(*)
from users;
Assuming is_available just takes two values, 0 and 1, you can use:
SELECT AVG(is_available)
FROM users;
If it can take other non-NULL values:
SELECT AVG(is_available = 1)
FROM users;
And, if you really need to counts NULLs, then you can use COALESCE() or:
SELECT SUM(is_available = 1) / COUNT(*)
FROM users;
Related
I have a query:
select AVG(tickets_number)
from TABLE_ONE
where ofd_id = :ofdId and launch_id in
(select id from TABLE_TWO where is_actual = 1 and is_matching_completed = 1)
But I need to calculate average value only if there are at least 10 rows from this query, otherwise I have to return NULL.
How can I do this?
Try this case expression.
select CASE WHEN COUNT(tickets_number) >= 10
THEN AVG(tickets_number)
ELSE NULL END average
from TABLE_ONE ...
Background
I want to rename my case statement in sql select statement dynamically.
Eg:
SELECT (case when id= x.id then x.sums end) x.id
as (select id,count(*) sums from table
group by id) x
what i want the output is list of columns created ,with Labels as distinct id's from "id" column.
However,this variable x.id is not dynamically outputing values,rather i get output a single column x.id.
Eg:
Columns in table...
id---c1----c2
1----x1---x2
2----x2----x3
3----x4----x5
columns expected after running query...
1-----2----3
but actual o/p column is::
x.id
Query
Any ideas,how to generate columns dynamically using select query,please correct me ,if i am wrong.
Below is for BigQuery!
Please note: your expectations about output column names are not correct!
Column name cannot start with digit - so in below example - i will be using id_1, id_2 and id_3 instead of 1, 2 and 3
SELECT
SUM(CASE WHEN id = 1 THEN 1 END) AS id_1,
SUM(CASE WHEN id = 2 THEN 1 END) AS id_2,
SUM(CASE WHEN id = 3 THEN 1 END) AS id_3
FROM YourTable
Above example assumes you know in advance your IDs and there are very few of them so it is not a big deal to write manually few numbers of lines with SUM(...) for each id
If this is not a case - you can first generate above query programmatically by running below query
SELECT 'SELECT ' +
GROUP_CONCAT_UNQUOTED(
'SUM(CASE WHEN id = ' + STRING(id) + ' THEN 1 END) AS id_' + STRING(id)
)
+ ' FROM YourTable'
FROM (
SELECT id FROM (
SELECT * FROM YourTable GROUP BY id ORDER BY id
)
as a result - you will get string like below
SELECT SUM(CASE WHEN id = 1 THEN 1 END) AS id_1,SUM(CASE WHEN id = 2 THEN 1 END) AS id_2,SUM(CASE WHEN id = 3 THEN 1 END) AS id_3 FROM YourTable
So, now just copy it and paste into Query Editor and run it
you can see similar example here - https://stackoverflow.com/a/36623258/5221944
The aggregate query groups by attribute A3 and then performs a COUNT(A4) but it doesn't consider the NULL values in the attribute A4.
For a regular count, don't include the column name:
count(*)
For count distinct, just add the extra value back in:
count(distinct a4) + (case when count(a4) <> count(*) then 1 else 0 end)
This can be simplified in MySQL to:
count(distinct a4) + (count(a4) <> count(*))
Or, if you know there is value that won't exist in the column:
count(distinct coalesce(a4, ' <NULL>'))
I have a MySQL table with 20 fields.
out of 20, 15 have 3 possible value like 0,NA,1+. i need to write a query to fetch each field count which has value > 0. is it possible to get it in single query.?
Thanks Guys.
Unless I'm missing something...
SELECT
COUNT(CASE WHEN c1<>'0' THEN 1 END) AS c1_count,
COUNT(CASE WHEN c2<>'0' THEN 1 END) AS c2_count,
-- ...
COUNT(CASE WHEN c15<>'0' THEN 1 END) AS c15_count
FROM t
select "column1", count(*) from your_table where value>0
union all
select "column2", count(*) from your_table where value>0
...
... // repeat until column20
interesting documentation :- http://dev.mysql.com/doc/refman/5.0/en/union.html
I got a table with username, usergroup and usertype.
A 0 0
B 0 1
C 1 2
D 2 2
Now i want something like
get all the users where the group is 0 or 1 and type is 2
I use:
SELECT * FROM table WHERE usergroup= 0 OR 1 AND usertype=2 GROUP BY usergroup
I expect it will return the username C, but strangely it returns A,C,D
What am I doing wrong?
I'm going to ignore that GROUP BY.
SELECT * FROM table WHERE usergroup= 0 OR 1 AND usertype=2
What am I doing wrong?
First, you have precedence issues; your SQL query is equivalent to:
SELECT * FROM table WHERE usergroup = 0 OR (1 AND usertype=2)
Use parentheses to be clear:
SELECT * FROM table WHERE (usergroup = 0 OR 1) AND usertype=2
There's still a problem, in that boolean operators simply don't work like this: 0 OR 1 is an expression that evaluates to 1.
You have to repeat the thing you're comparing against:
SELECT * FROM table WHERE (usergroup = 0 OR usergroup = 1) AND usertype=2
However, this can be shortened thus:
SELECT * FROM table WHERE usergroup IN (0, 1) AND usertype=2
Your SQL should be :
SELECT *
FROM table
WHERE (usergroup = 0 OR usergroup = 1)
AND (usertype = 2)
Why do you need a 'GROUP BY' is unclear, though.
SELECT * FROM table WHERE usergroup IN (0,1) AND usertype = 2