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
Related
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;
Scenario: I am trying to create an output matrix where I have all the columns names (fields) of a source table in the first column, followed by the sum of all Null values of that original field column.
Ex:
Original Table:
Id1 Code Range
aa 33 null
ab 12 001
ac 53 001
ad null null
null 36 002
Wanted output:
Fields #ofnull #ofnonnull
Id1 1 4
Code 1 4
Range 2 3
For this I have a code that retrieves the names and positions of all the columns in the original matrix, and a snippet which counts my nulls/nonnulls.
Issue: I have no idea how to string those together and get this output with a single query. I tried searching around, but most answers were regarding just counting the nulls, not on the process on inputting a list of columns to the query.
Question: Is it possible to do this? or do I have to feed the query each column name manually?
Code so far:
select
`ordinal_position`,
`column_name`,
from `dev1`.`info`
where `table_schema` = 'dev1'
and `table_name` = 'data1'
order by `ordinal_position`;
select
count(1)
from `dev1`.`data1`
where Id1 is null;
-- where Id1 is not null;
One approach uses a series of unions:
SELECT
'Id1' AS Fields,
COUNT(CASE WHEN Id1 IS NULL THEN 1 END) AS NoNull,
COUNT(Id1) AS NoNonNull
FROM yourTable
UNION ALL
SELECT 'Code', COUNT(CASE WHEN Code IS NULL THEN 1 END), COUNT(Code)
FROM yourTable
UNION ALL
SELECT 'Range', COUNT(CASE WHEN `Range` IS NULL THEN 1 END), COUNT(`Range`)
FROM yourTable;
Demo
You can try using UNION ALL
SELECT
field,
COUNT(CASE WHEN val IS NULL THEN 1 END) AS `#ofnull`,
COUNT(CASE WHEN val IS NOT NULL THEN 1 END) AS `#ofnotnull`
FROM
(
SELECT 'Id1' AS field, Id1 AS val FROM yourTable
UNION ALL
SELECT 'Code', Code FROM yourTable
UNION ALL
SELECT 'Range', `Range` FROM yourTable
) a
GROUP BY field;
I have a table for user input to questions. They were given the option to give up to five answers to the same question so you have 5 fields with up to five different answers for the same question.
So field1,filed2,field3,field4,field5 could all have the answer "foo"
I want create a query that will return a number count of the total answers in any of the five columns for all users for each possible answer.
So the output would be:
'foo' 22 'I like cake' 32 'who cares' 2
I am on the right track with the following but I suspect there is a more elegant solution. Here is what I have so far:
SELECT field1,filed2,field3,field4,field5,
(sum(CASE WHEN field1='foo' THEN 1 ELSE 0 END) +
sum(CASE WHEN field2='foo' THEN 1 ELSE 0 END)) +
sum(CASE WHEN field3='foo' THEN 1 ELSE 0 END)) +
sum(CASE WHEN field4='foo' THEN 1 ELSE 0 END)) +
sum(CASE WHEN field5='foo' THEN 1 ELSE 0 END)) AS count
FROM items
GROUP BY field1,filed2,field3,field4,field5
;
Any suggestions would be appreciated.
Perhaps this is what you need. Have a derived table that returns all columns UNION ALL-ed into one. Then do the GROUP BY:
select field, count(*)
from
(
select field1 as field from items
union all
select field2 from items
union all
...
select fieldn from items
) dt
group by field
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
I have the following query:
SELECT cashtransactionstatus_id, SUM(cashtransaction_amount) AS cashtransaction_amount
FROM cash_transactions
WHERE cashpaymenttype_id = 1
GROUP BY (cashtransactionstatus_id);
This query returns:
How i can return only one digit number endtotal subtracting and sum depending by the column cashtransactionstatus_id? Ex:
SUM Status(1,3): 1195.00 as col1
SUM Status(2,4,5): 430.00 as col2
then show (col1 - col2) AS endtotal = 765.00
Thank you
You can use conditional aggregates:
SELECT SUM(CASE WHEN cashtransactionstatus_id IN (1,3) THEN cashtransaction_amount END) -
SUM(CASE WHEN cashtransactionstatus_id IN (2,4,5) THEN cashtransaction_amount END) AS endtotal
FROM cash_transactions
WHERE cashpaymenttype_id = 1
SQL Fiddle Demo