How to get an group data from sql? - sql

Can anyone help me create chart like the one below? I'm using CFDB on wordpres. It is a simple form inputs counter.
I've figured out something like this:
SELECT month(FROM_UNIXTIME(`submit_time`)) as miesiac,
year(FROM_UNIXTIME(`submit_time`)) as rok,
`form_name`, `field_name`, `field_value`, `field_order`, `file`
FROM `wp_cf7dbplugin_submits`
WHERE year(FROM_UNIXTIME(`submit_time`)) = 2016
I would like to get final result like in the attachment.
Now I get something like this one:
enter image description here

In order to generate the data needed for a chart like the one in your example, you need to return the number of form submissions for each month and form type.
SELECT COUNT(sub.form_name) as total, sub.form_name, sub.miesiac
FROM (
SELECT DISTINCT `submit_time`, month(FROM_UNIXTIME(`submit_time`)) as miesiac,
`form_name`
FROM `wp_cf7dbplugin_submits`
WHERE year(FROM_UNIXTIME(`submit_time`)) = 2016 ) sub
GROUP BY sub.form_name, sub.miesiac
The sub-query identifies the distinct submissions (since each submission has multiple rows) and the main query counts the number of submissions for each form type per month. There's no need to include the year because it's already included in the WHERE statement.

Related

How to combine rows in BigQuery that share a similar name

i'm having trouble creating a query that'll group together responses from multiple rows that share a similar name and count the specific response record in them.
the datatable i currently have looks like this
test_control
values
test
selected
control
selected
test us
not selected
control us
selected
test mom
not selected
control mom
selected
what i'd like, is an output like the below that only counts the number of "selected" responses and groups together the rows that have either "control" or "test" in the name"
test_control
values
test
3
control
1
The query i have below is wrong as it doesn't give me an output of anything. The group by section is where im lost as i'm not sure how to do this. tried to google but couldn't seem to find anything. appreciate any help in advance!!!
SELECT distinct(test_control), values FROM `total_union`
where test_control="%test%" and values="selected"
group by test_control, values
use below
SELECT
REGEXP_EXTRACT(test_control, r'^(TEST|CONTROL) ') AS test_control,
COUNTIF(values = 'selected') AS values
FROM `total_union`
GROUP BY 1
As mentioned by #Mikhail Berlyant, you can use REGEX_EXTRACT to match the expression and COUNTIF to get the count of the total number of matching expressions according to the given condition. Try below code to get the expected output :
Code
SELECT
REGEXP_EXTRACT(test_control, r'^(test|control)') AS test_control,
COUNTIF(values = "selected") AS values
FROM `project.dataset.testvalues`
group by 1
Output

How to count unique occurences of string in table for separate records in apex 5

I am trying to automatically count the unique occurrences of a string saved in the table. Currently I have a count of a string but only when a user selects the string and it gives every record the same count value.
For example
Below is a image of my current table:
From the image you can see that there is a Requirement column and a count column. I have got it to the point were when the user would select a requirement record (each requirement record has a link) it would insert the requirement text into a requirement item called 'P33_REQUIREMENT' so the count can have a value to compare to.
This is the SQL that I have at current:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT
group by REQUIREMENT
) AS COUNT,
DPD.DIA_SELECTED,
DPD.Q_NUMBER_SELECTED,
DPD.SECTION_SELECTED,
DPD.ASSIGNED_TO_PERSON,
DAQD.REFERENCE,
DAQD.REQUIREMENT,
DAQD.PROGRESS,
DAQD.ACTION_DUE_DATE,
DAQD.COMPLETION_DATE,
DAQD.DIA_REF,
DA.DIA,
DA.ORG_RISK_SCORE
FROM DIA_PROPOSED_DETAIL DPD,
DIA_ASSOCIATED_QMS_DOCUMENTS DAQD,
DIA_ASSESSMENTS DA
WHERE DPD.DIA_SELECTED = DAQD.DIA_REF
AND DPD.DIA_SELECTED = DA.DIA
This is the sql used to make the table in the image.
This issue with this is, it is giving every record the same count when the user selects a requirement value. I can kind of fix this by also adding in AND DIA_SELECTED = :P33_DIA into the where clause of the count. DIA_SELECTED being the first column in the table and :P33_DIA being the item that stores the DIA ref number relating to the record chosen.
The output of this looks like:
As you can see there is only one count. Still doesn't fix the entire issue but a bit better.
So to sum up is there a way to have the count, count the occurrences individually and insert them in the requirements that are the same. So if there are three tests like in the images there would be a '3' in the count column where requirement = 'test', and if there is one record with 'test the system' there would be a '1' in the count column.
Also for more context I wont know what the user will input into the requirement so I can't compare to pre-determined strings.
I'm new to stack overflow I am hoping I have explained enough and its not too confusing.
The following extract:
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = :P33_REQUIREMENT group by REQUIREMENT ) AS COUNT
Could be replaced by
SELECT (SELECT COUNT(*)
FROM DIA_ASSOCIATED_QMS_DOCUMENTS
WHERE REQUIREMENT = DAQD.REQUIREMENT ) AS COUNT
Which would give you - for each line, the number of requirements that are identical.
I'm not completely certain it is what you are after, but if it isn't, it should give you some ideas on how to progress (or allow you to indicate where I failed to understand your request)

SQL ignore all blank fields in calculation

I am using the following query in SQL within PhpRunner:
SELECT
[Date],
(MondayStrengthEnd1Sets*MondayStrengthEnd1Reps*MondayStrengthEnd1Distance) + (MondayStrengthEnd2Sets*MondayStrengthEnd2Reps*MondayStrengthEnd2Distance) AS Total
FROM Running
When I run the query I get a blank for an answer. Some of the fields will not necessarily be filled in for every record. The example above is just a snippet of all the fields that is in the table and in the complete calculation, there will be almost 90 fields in total. All the fields are from the same table.
What can I add to the query to treat the blank fields as blanks and not as zeros in order to still calculate the total despite some fields not being filled in? If there is anything that will do it automatically for all the fields it would be great.
I am aiming for something like this:
It looks like you need something like:
SELECT
[Date],
isnull(MondayStrengthEnd1Sets*MondayStrengthEnd1Reps*MondayStrengthEnd1Distance, 0)
+ isnull(MondayStrengthEnd2Sets*MondayStrengthEnd2Reps*MondayStrengthEnd2Distance, 0)
AS Total
FROM Running

Countif query in access

I am trying to run a query that calculate with a countif function but I am having trouble with it. I have used the count and the iif functions in the builder but I think something weird is going on. I am trying to count the number of times a certain value occurs in a column so I do not want a specific value to equal to if that's possible?
Thanks!
To count the number of times a value appears you can use something like.
If you want to know how many times each value appears just omit the WHERE clause (without a sample of data I've used a table in the database I'm working on).
SELECT ProcessID,
COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
if you need just the value you can use:
SELECT COUNT(ProcessID)
FROM tbl_PrimaryData_Step1
WHERE ProcessID = 4
GROUP BY ProcessID
Another way is:
SELECT DCOUNT("ProcessID","tbl_PrimaryData_Step1","ProcessID = 4")
Edit:
In reply to your comment on your original post this SQL will give the result you're after:
SELECT Concatenate,
COUNT(Concatenate)
FROM MyTable
GROUP BY Concatenate

JSP result set from MS Access - how to get the location name?

I have a table called "form" in ms access which have following fields:
formno, location, status.
I want to create a report which calculates:
the total no of forms(columns) at each location
the total no of forms(columns) with status= "pending" at each location.
I tried to do it with this query:
select count(formno) as totalforms
from form
group by location;
select count(formno) as pendingforms
from form
group by location
WHERE status = 'pending';
SELECT Location, COUNT(FormNo) AS TotalForms,
SUM(IIF(status = "pending", 1, 0)) AS Pending
FROM FORM GROUP BY Location
Does this help?
The second SQL Statment has the Where Clause in the wrong place. It should look like one of these:
SELECT Count(form.formno) AS CountOfformno, form.location
FROM form
GROUP BY form.location, form.status
HAVING (((form.status)='pending'));
SELECT Count(form.formno) AS CountOfformno, form.location
FROM form
WHERE (((form.status)='pending'))
GROUP BY form.location;
Basically if you wish to group by a column and set criteria on it use the Having Clause. The Where Clause comes before the group by clause.
To learn the SQL syntax you may find it easier to use the Query By Design Grid (in Design View) first then switch to SQL View after you get the results you are after in Datasheet View.
Form is a reserved word and must be enclosed in square brackets: http://support.microsoft.com/kb/286335. I would suggest renaming the table, because it will continue to cause problems.
select location,count(formno) as totalforms
from [form]
group by location
select location, count(formno) as pendingforms
from [form]
WHERE status = 'pending'
group by location
Note that count(*) can also be used if you wish to include all records in the table: In SQL is there a difference between count(*) and count(<fieldname>)