MySQL - search for duplicates from gravity forms database - mysql

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.

Related

Counting individual items in MY SQL or MS SQL column

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.

Mysql create table with sums of nulls and not nulls

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;

Divide the columns in mysql into multiple columns

I am completely new to mysql. Here I am trying to make a query in mysql which divides a column col1 into 4 different columns based on its category(col2) in sorted order as shown below. I have written a query like this till now:
select if(category = 'first',name ,NULL) as first,
if(category = 'second',name,NULL) as second,
if(category = 'third',name,NULL) as third,
if(category = 'fourth',name,NULL) as fourth
from 'table';
this code gives me the four columns but I am stuck now as I am not able to further filter this.
Given table:
name category
John first
Sunil third
Jenny third
Ashley fourth
Meera second
Abhay first
Required answer:
col1 col2 col3 col4
Abhay Meera Jenny Ashley
John NULL Sunil NULL
Note that all the columns in the answer are sorted.
Edit: I guess the question is not clear about the format of final answer. Thanks #philipxy for pointing out. Final answer should be adjusted in least number of rows (which is 2 in my case). All the columns should have equal number of rows and if some column has lesser values then that row will have NULL value in respective columns e.g col2 and col 4 above. Finally all the columns should be in sorted order where NULL will be in the last(if any)e.g Suppose there was an entry named Olly with category fourth then it should appear before NULL in col4 and after Ashley.
This is tricky. You are trying to stack lists vertically, rather than horizontally, which is not a "normal" SQL operation.
You can do what you want by using conditional aggregation. The problem is there is nothing to aggregate by. The solution is to introduce a variable column to calculate a sequence number for aggregation:
select max(case when category = 'first' then name end) as first,
max(case when category = 'second' then name end) as second,
max(case when category = 'third' then name end) as third,
max(case when category = 'fourth' then name end) as fourth
from (select t.*,
(#rn := if(#c = category, #rn + 1,
if(#c := category, 1, 1)
)
) as rn
from `table` t cross join
(select #c := '', #rn := 0) params
order by category
) t
group by rn;
If you want the values in a particular order in each column, then add a second sort key after category.
EDIT:
I should note that if you don't need multiple rows, but just the values, you can concatenate them together:
select group_concat(case when category = 'first' then name end) as firsts,
group_concat(case when category = 'second' then name end) as seconds,
group_concat(case when category = 'third' then name end) as thirds,
group_concat(case when category = 'fourth' then name end) as fourths
from`table` t
group by rn;

Select count of similar values in multiple columns

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

Dynamically creating columns from row data using Select in Bigquery

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