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
Related
I have data with column name Rule1 with values ..Correct, Incorrect and Undefined
I was able to get the count of them in this particular column using Group BY
select Rule1, count(*) from table_name group by Rule1;
I have one more column Rule2 with Values Correct, Incorrect only
When I try a similar select statement as above, I am getting the only count of Correct and Incorrect
I want to get the count of Undefined as zero as no Undefined is present in Rule2 column
How to do that
How I can write a query that needs to show the count of fixed values in multiple columns
You can create a derived table that enumerates the possible values with union all, then bring your table with a left join:
select x.rule1, count(t.rule1) cnt
from (select 'Correct' rule1 union all select 'Incorrect' union all select 'Undefined') x
left join mytable t on t.rule1 = x.rule1
group by x.rule1
This is a cross-database syntax that will work in both MySQL and SQL Server.
In very recent versions of MySQL, you can use the values(row ...) syntax:
select x.rule1, count(t.rule1) cnt
from (values row('Correct'), row('Incorrect'), row('Undefined')) x(rule1)
left join mytable t on t.rule1 = x.rule1
group by x.rule1
In SQL Server, the equivalent query is:
select x.rule1, count(t.rule1) cnt
from (values ('Correct'), ('Incorrect'), ('Undefined')) x(rule1)
left join mytable t on t.rule1 = x.rule1
group by x.rule1
You could also use sum like this:
SELECT
sum(case when Rule1 = 'Correct' then 1 else 0 end) AS Rule1Correct,
sum(case when Rule1 = 'Incorrect' then 1 else 0 end) AS Rule1Incorrect,
sum(case when Rule1 = 'Undefined' then 1 else 0 end) AS Rule1Undefined,
sum(case when Rule2 = 'Correct' then 1 else 0 end) AS Rule2Correct,
sum(case when Rule2 = 'Incorrect' then 1 else 0 end) AS Rule2Incorrect
FROM
table_name
WHERE
1
It enables multiple counts in one query. I added all the counts, thought the first two was enough for the idea.
Futhermore I have no idea what is better for the serverload, this or using unions.
I need an SQL query from a gravity form to search for duplicate entries.
Gravity form save entries in rows and I must change it to columns.
The default query is:
SELECT * FROM `wp_rg_lead_detail` WHERE 1 ORDER BY `value` DESC
look at screenshot
I don't know if this is a good solution but in works for me:
SELECT id, lead_id, form_id,
MAX(case when field_number = 6 then value end) Projektname,
MAX(case when field_number = 7 then value end) Projektnummer,
MAX(case when field_number = 129 then value end) Keyaccounter
FROM `wp_rg_lead_detail` GROUP BY lead_id
HAVING form_id = 1
This is the result
But now I must serach for duplicates in field_numer 7 to list only entries who have duplicates in this field. I have change the last line with this:
HAVING form_id = 1 AND HAVING COUNT(field_number = 7) > 1
This does not work.
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 was looking around but couldn't find an answer
I need a query that return 2 groups of values as field names based on values in a same field
example I have a table
NAME, VALUE
name1, 2
name1, 2
name1, 3
name1, 4
name2, 2
name2, 2
name2, 3
name2, 4
now I want to count and group values 2 and 3 in one group and values 4 in another group so my result would look like this
NAME, GRP1_COUNT, GRP2_COUNT
name1, 3, 1
name2, 3, 1
I tried the JOIN and UNION without much luck
any help appreciated
MySQL does not have a pivot function so you will have to transform the data using an aggregate function with a CASE expression. For this type of calculation you will use either COUNT or SUM:
select name,
sum(case when value in (2,3) then 1 else 0 end) GRP1_COUNT,
sum(case when value = 4 then 1 else 0 end) GRP2_COUNT
from yourtable
group by name
See SQL Fiddle with Demo
COUNT version:
select name,
count(case when value in (2,3) then VALUE end) GRP1_COUNT,
count(case when value = 4 then VALUE end) GRP2_COUNT
from yourtable
group by name
See SQL Fiddle with Demo
Try this
SELECT
name,
sum(case when value=2 or value=3 then 1 else 0 end),
sum(case when value=4 then 1 else 0 end)
FROM YourTable
GROUP BY name
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