Multiple Count() in Database - mysql

I'm making a survey-applican with winforms and VB. This is the SQL I got so far to show statistics:
SELECT
tblAlt.altText,
Count(tblAnswers.answerID)
FROM tblAlt
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questionID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;
This returns each alternatives for a question, and how many answered them. Is there a way I can count how many answered in total with the same SQL-query?
Tables involved are:
_______________ _______________ ___________ _______________
|_tblUsers____| |_tblAnswers___| |_tblAlt__| |_tblQuestion_|
| userID | | answerAltID | | altID | | questID |
| username | | userID | | altText | | questText |
|_____________| |______________| |_questID_| |_____________|
Any help would be appreciated!
This is what I used in the end:
SELECT
tblAlt.altText,
Count(tblAnswers.answerID),
(SELECT COUNT(answerID) FROM tblAnswers, tblAlt
WHERE tblAnswers.answerAltID = tblAlt.altID
AND tblAlt.questID = " & CInt(questionID) & ") as total_count
FROM tblAlt
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;

Use ROLLUP:
SELECT
tblAlt.altText,
Count(tblAnswers.answerID)
FROM tblAlt
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questionID = " & CInt(questionID) & "
GROUP BY tblAlt.altText WITH ROLLUP;
If you want one-time computation(to make query efficient), use CROSS JOIN. Don't worry, this CROSS JOIN won't yield cartesian product, it's only one row. This might be faster than subquery approach:
SELECT
tblAlt.altText,
Count(tblAnswers.answerID), x.total_count
FROM tblAlt
cross join (SELECT COUNT(answerID) as total_count FROM tblAnswers) as x
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questionID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;
Or use MySqlism, might be faster:
SELECT
tblAlt.altText,
Count(tblAnswers.answerID), #total_count as total_count
FROM tblAlt
cross join (SELECT #total_count := COUNT(answerID) FROM tblAnswers) as x
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questionID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;
Or you can use multi-statements. Note, SET statements doesn't appear on ADO.NET's DataReader or DataTable. ADO.NET can still get the results from your actual query. This has a certainty to be faster among all approaches:
SET #total_count := (SELECT COUNT(answerID) FROM tblAnswers);
SELECT
tblAlt.altText,
Count(tblAnswers.answerID), #total_count as total_count
FROM tblAlt
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questionID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;

you can use an subquery to do this, something like:
SELECT
tblAlt.altText,
Count(tblAnswers.answerID),
(SELECT COUNT(answerID) FROM tblAnswers) as total_count
FROM tblAlt
LEFT JOIN tblAnswers ON (tblAlt.altId = tblAnswers.altID)
WHERE tblAlt.questionID = " & CInt(questionID) & "
GROUP BY tblAlt.altText;
Some Resources:
http://beginner-sql-tutorial.com/sql-subquery.htm
http://www.1keydata.com/sql/sql-subquery.html

Related

MySQL Query too large, is there a way to opimize?

Is there any way to optimize this large MySQL Query? I have tried many things, but they all give me results that are not correct, such as duplicates and empty columns.
SELECT quotations.*,
GROUP_CONCAT(DISTINCT CONCAT(groups.group_id, " | ", groups.group_name) SEPARATOR " , ") AS `quotation_groups`,
GROUP_CONCAT(DISTINCT CONCAT(departments.department_id, " | ", departments.department_name, " | ", departments.department_code) SEPARATOR " , ") AS `quotation_departments`,
GROUP_CONCAT(DISTINCT CONCAT(projects.project_id) SEPARATOR " , ") AS `quotation_projects`,
GROUP_CONCAT(DISTINCT CONCAT(machines.machine_id, " | ", machines.machine_name) SEPARATOR " , ") AS `quotation_machines`,
GROUP_CONCAT(DISTINCT CONCAT(parts.part_id, " | ", parts.part_name) SEPARATOR " , ") AS `quotation_parts`,
GROUP_CONCAT(DISTINCT CONCAT(options.option_id, " | ", options.option_name) SEPARATOR " , ") AS `quotation_options`,
GROUP_CONCAT(DISTINCT CONCAT(applications.application_id, " | ", applications.application_nl_title) SEPARATOR " , ") AS `quotation_applications`,
GROUP_CONCAT(DISTINCT CONCAT(actions.action_id, " | ", actions.action_name) SEPARATOR " , ") AS `quotation_actions`,
GROUP_CONCAT(DISTINCT CONCAT(tests.test_id, " | ", tests.test_name) SEPARATOR " , ") AS `quotation_tests`,
GROUP_CONCAT(DISTINCT CONCAT(notes.note_id, " | ", notes.note_name) SEPARATOR " , ") AS `quotation_notes`,
GROUP_CONCAT(DISTINCT CONCAT(companies.company_id, " | ", companies.company_name) SEPARATOR " , ") AS `quotation_companies`,
GROUP_CONCAT(DISTINCT CONCAT(persons.person_id, " | ", persons.person_gender, " | ", persons.person_firstname, " | ", persons.person_middlename, " | ", persons.person_lastname, " | ", persons.person_function) SEPARATOR " , ") AS `quotation_persons`,
GROUP_CONCAT(DISTINCT CONCAT(addresses.address_id, " | ", addresses.address_types, " | ", addresses.address_name, " | ", addresses.address_streetname, " | ", addresses.address_housenumber, " | ", addresses.address_zipcode, " | ", addresses.address_city) SEPARATOR " , ") AS `quotation_addresses`,
GROUP_CONCAT(DISTINCT CONCAT(attachments.attachment_id, " | ", attachments.attachment_name) SEPARATOR " , ") AS `quotation_attachments`
FROM quotations quotations
LEFT JOIN quotations_relations quotation_departments ON (quotation_departments.quotations_relations_child_name="departments" AND quotation_departments.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_groups ON (quotation_groups.quotations_relations_child_name="groups" AND quotation_groups.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_projects ON (quotation_projects.quotations_relations_child_name="projects" AND quotation_projects.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_machines ON (quotation_machines.quotations_relations_child_name="machines" AND quotation_machines.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_parts ON (quotation_parts.quotations_relations_child_name="parts" AND quotation_parts.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_options ON (quotation_options.quotations_relations_child_name="options" AND quotation_options.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_applications ON (quotation_applications.quotations_relations_child_name="applications" AND quotation_applications.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_actions ON (quotation_actions.quotations_relations_child_name="actions" AND quotation_actions.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_tests ON (quotation_tests.quotations_relations_child_name="tests" AND quotation_tests.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_notes ON (quotation_notes.quotations_relations_child_name="notes" AND quotation_notes.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_companies ON (quotation_companies.quotations_relations_child_name="companies" AND quotation_companies.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_persons ON (quotation_persons.quotations_relations_child_name="persons" AND quotation_persons.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_addresses ON (quotation_addresses.quotations_relations_child_name="addresses" AND quotation_addresses.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN quotations_relations quotation_attachments ON (quotation_attachments.quotations_relations_child_name="attachments" AND quotation_attachments.quotations_relations_parent=quotations.quotation_id)
LEFT JOIN groups_relations groups_relations ON (groups_relations.groups_relations_child_name="quotations" AND groups_relations.groups_relations_child_id=quotations.quotation_id)
LEFT JOIN groups groups ON (groups.group_id=groups_relations.groups_relations_parent OR groups.group_id=quotation_groups.quotations_relations_child_id)
LEFT JOIN departments_relations departments_relations ON (departments_relations.departments_relations_child_name="quotations" AND departments_relations.departments_relations_child_id=quotations.quotation_id)
LEFT JOIN departments departments ON (departments.department_id=departments_relations.departments_relations_parent OR departments.department_id=quotation_departments.quotations_relations_child_id)
LEFT JOIN projects_relations projects_relations ON (projects_relations.projects_relations_child_name="quotations" AND projects_relations.projects_relations_child_id=quotations.quotation_id)
LEFT JOIN projects projects ON (projects.project_id=projects_relations.projects_relations_parent OR projects.project_id=quotation_projects.quotations_relations_child_id)
LEFT JOIN machines_relations machines_relations ON (machines_relations.machines_relations_child_name="quotations" AND machines_relations.machines_relations_child_id=quotations.quotation_id)
LEFT JOIN machines machines ON (machines.machine_id=machines_relations.machines_relations_parent OR machines.machine_id=quotation_machines.quotations_relations_child_id)
LEFT JOIN parts_relations parts_relations ON (parts_relations.parts_relations_child_name="quotations" AND parts_relations.parts_relations_child_id=quotations.quotation_id)
LEFT JOIN parts parts ON (parts.part_id=parts_relations.parts_relations_parent OR parts.part_id=quotation_parts.quotations_relations_child_id)
LEFT JOIN options_relations options_relations ON (options_relations.options_relations_child_name="quotations" AND options_relations.options_relations_child_id=quotations.quotation_id)
LEFT JOIN options options ON (options.option_id=options_relations.options_relations_parent OR options.option_id=quotation_options.quotations_relations_child_id)
LEFT JOIN applications_relations applications_relations ON (applications_relations.applications_relations_child_name="quotations" AND applications_relations.applications_relations_child_id=quotations.quotation_id)
LEFT JOIN applications applications ON (applications.application_id=applications_relations.applications_relations_parent OR applications.application_id=quotation_applications.quotations_relations_child_id)
LEFT JOIN actions_relations actions_relations ON (actions_relations.actions_relations_child_name="quotations" AND actions_relations.actions_relations_child_id=quotations.quotation_id)
LEFT JOIN actions actions ON (actions.action_id=actions_relations.actions_relations_parent OR actions.action_id=quotation_actions.quotations_relations_child_id)
LEFT JOIN tests_relations tests_relations ON (tests_relations.tests_relations_child_name="quotations" AND tests_relations.tests_relations_child_id=quotations.quotation_id)
LEFT JOIN tests tests ON (tests.test_id=tests_relations.tests_relations_parent OR tests.test_id=quotation_tests.quotations_relations_child_id)
LEFT JOIN notes_relations notes_relations ON (notes_relations.notes_relations_child_name="quotations" AND notes_relations.notes_relations_child_id=quotations.quotation_id)
LEFT JOIN notes notes ON (notes.note_id=notes_relations.notes_relations_parent OR notes.note_id=quotation_notes.quotations_relations_child_id)
LEFT JOIN companies_relations companies_relations ON (companies_relations.companies_relations_child_name="quotations" AND companies_relations.companies_relations_child_id=quotations.quotation_id)
LEFT JOIN companies companies ON (companies.company_id=companies_relations.companies_relations_parent OR companies.company_id=quotation_companies.quotations_relations_child_id)
LEFT JOIN persons_relations persons_relations ON (persons_relations.persons_relations_child_name="quotations" AND persons_relations.persons_relations_child_id=quotations.quotation_id)
LEFT JOIN persons persons ON (persons.person_id=persons_relations.persons_relations_parent OR persons.person_id=quotation_persons.quotations_relations_child_id)
LEFT JOIN addresses_relations addresses_relations ON (addresses_relations.addresses_relations_child_name="quotations" AND addresses_relations.addresses_relations_child_id=quotations.quotation_id)
LEFT JOIN addresses addresses ON (addresses.address_id=addresses_relations.addresses_relations_parent OR addresses.address_id=quotation_addresses.quotations_relations_child_id)
LEFT JOIN attachments_relations attachments_relations ON (attachments_relations.attachments_relations_child_name="quotations" AND attachments_relations.attachments_relations_child_id=quotations.quotation_id)
LEFT JOIN attachments attachments ON (attachments.attachment_id=attachments_relations.attachments_relations_parent OR attachments.attachment_id=quotation_attachments.quotations_relations_child_id)
GROUP BY quotations.quotation_id ORDER BY quotations.quotation_updated DESC
As you can see, i need to get all details from every table that have a relation with eachother.
I noticed, that, after a while, the more relations there are, the slower the query is, for example, i have this quotation with 103 relations, it takes up to 34seconds to gather all data.
My first suggestion would be to do a single join with conditional aggregation. You will probably not need the DISTINCT in GROUP_CONCAT() any more. Here is an example for departments:
SELECT q.*,
GROUP_CONCAT(CASE WHEN qr.quotations_relations_child_name = 'departments' THEN qr.department_id, ' | ', qr.department_name END)
FROM quotations q LEFT JOIN
quotations_relations qr
ON qr.quotations_relations_parent = q.quotation_id
GROUP BY q.quotation_id
ORDER BY q.quotation_updated DESC ;
For a start, you could use only one join for the quotations:
LEFT JOIN quotations_relations ON quotations_relations_parent=quotations.quotation_id
and do the relation check on the individual rows:
e.g.
LEFT JOIN groups groups ON
(
groups.group_id=groups_relations.groups_relations_parent OR
(groups.group_id=quotations_relations.quotations_relations_child_id AND quotations_relations_child_name="groups")
)

Combining 3 querys on SQL

I have a table with both, Products and Sevices. And I store the products brands, product names and services names on separate tables.
I get the list of Services with this Query:
SELECT maeinvs.idInv, CONCAT(services.Service, " ", maeinvs.Detail) AS Name
FROM maeinvs
INNER JOIN services
ON services.idService = maeinvs.idService
And the list of Products with this one:
SELECT maeinvs.idInv, CONCAT(brands.Brand, " ", products.Product, " ", maeinvs.Detail) AS Name
FROM maeinvs
INNER JOIN products
ON products.idProduct = maeinvs.idProduct
INNER JOIN brands
ON brands.idBrand = maeinvs.idBrand
I need to get a Query like this, but instead of the idInv field I need the actual Name of the Product or Service. Any ideas?
SELECT idInv, Desc, Qnty, Price
FROM invoicedetails
WHERE idInvoice = $id
Thanks.
SELECT maeinvs.idInv, CONCAT(services.Service, " ", maeinvs.Detail) AS Name,
"" AS Desc, 0 AS Qnty, 0 AS Price
FROM maeinvs
INNER JOIN services
ON services.idService = maeinvs.idService
UNION
SELECT maeinvs.idInv, CONCAT(brands.Brand, " ", products.Product, " ", maeinvs.Detail) AS Name,
"" AS Desc, 0 AS Qnty, 0 AS Price
FROM maeinvs
INNER JOIN products
ON products.idProduct = maeinvs.idProduct
INNER JOIN brands
ON brands.idBrand = maeinvs.idBrand
UNION
SELECT idInv,
"" AS Name,
Desc, Qnty, Price
FROM invoicedetails
WHERE idInvoice = $id
I've written what I've changed on next line
As the columns match each other, UNION will work.
I still don't know how to do it on a single query (I just know the basics; an Alias?) but solved it doing a VIEW and then a JOIN with my invoicedetails table.
VIEW:
SELECT maeinvs.idInv, CONCAT(services.Service, " ", maeinvs.Detail) AS Name
FROM maeinvs
INNER JOIN services
ON services.idService = maeinvs.idService
UNION
SELECT maeinvs.idInv, CONCAT(brands.Brand, " ", products.Product, " ", maeinvs.Detail) AS Name
FROM maeinvs
INNER JOIN products
ON products.idProduct = maeinvs.idProduct
INNER JOIN brands
ON brands.idBrand = maeinvs.idBrand
QUERY:
SELECT viewnames.Name, invoicedetails.Desc, invoicedetails.Qnty, invoicedetails.Price
FROM invoicedetails
INNER JOIN viewnames ON viewnames.idInv = invoicedetails.idInv
WHERE invoicedetails = $id

mysql query group by in group_concat

i have the following query:
SELECT files.file_name, files.locked, projects.project_name,
group_concat( versions.version, versions.language SEPARATOR ' & ')
FROM files
JOIN `projects` ON (files.project_id = projects.project_id)
JOIN `versions` ON (files.file_id = versions.file_id)
WHERE files.file_id = '1'
ORDER BY projects.project_name ASC
this gives me this table:
filename - 1 - projectname - 0.1EN & 0.2FR & 0.3DE & 0.1IT
what i want, is the query to output something like this:
filename - 1 - projectname - 0.1-EN,IT & 0.2-FR & 0.3-DE
so i tried this:
group_concat( versions.version, versions.language GROUP BY versions.version SEPARATOR ' & ')
but mysql did not like that.
How can i get the result i want? Thanks.
Edit: sample tables!
Give this a try,
SELECT a.file_name,
a.locked,
b.project_name,
GROUP_CONCAT(c.version, c.language SEPARATOR ' & ')
FROM files a
INNER JOIN projects b
ON a.project_id = b.project_ID
INNER JOIN
(
SELECT file_ID, `version`, GROUP_CONCAT(language) language
FROM versions
GROUP BY file_ID, `version`
) c ON a.file_ID = c.file_ID
WHERE a.file_ID = 1
GROUP BY a.file_name,
a.locked,
b.project_name

Query Change from MySQL to MS Access

i have a view in MySQL but for some reason i want it to write in MS Access..i have already imported neccessary tables. when i copy the view definaation from MySQL and write it in Access Query Design it gives me error on "Join operation".What's wrong here ?
View -> qryOccupation
select `a`.`Employee_ID` AS `Employee_ID`,`a`.`Employee_Name` AS `Employee_Name`,`a`.`Flat_No` AS `Flat_No`,`a`.`Area` AS `Area`,`a`.`Building_Name` AS `Building_Name`
from (`tblallotment` `a` join `tblflat` `f`)
where ((`a`.`Flat_No` = `f`.`Flat_No`)
and (`f`.`Status` = 'A')
and (not(`a`.`Employee_ID` in
(select `c`.`Employee_ID`
from (`tblallotment` `a` join `tblcancel_allotment` `c`)
where ((`a`.`Employee_ID` = `c`.`Employee_ID`)
and (`c`.`Date_Cancellation` = 0))))))
select a.Employee_ID AS Employee_ID,
a.Employee_Name AS Employee_Name,
a.Flat_No AS Flat_No,
a.Area AS Area,
a.Building_Name AS Building_Name
from tblallotment a
inner join tblflat f
on a.Flat_No = f.Flat_No
where f.Status = 'A'
and (not (a.Employee_ID in
(select c.Employee_ID
from tblallotment a
inner join tblcancel_allotment c
on a.Employee_ID = c.Employee_ID
where c.Date_Cancellation = 0)))
("SELECT [Department Name], " _
& "FirstName & Chr(32) & LastName AS Name " _
& "FROM Departments LEFT JOIN Employees " _
& "ON Departments.[Department ID] = " _
& "Employees.[Department ID] " _
& "ORDER BY [Department Name];")

Filtering rows by date in a full outer join query -> missing some results

Background
I've got two tables with different types of feedback items in MySQL. I've built a query to combine these tables by FULL OUTER JOIN (which is actually written as two joins and an union in MySQL) and to count some average grades. This query seems to work perfectly:
(SELECT name, AVG(l.overallQuality) AS avgLingQual,
AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic AS l
LEFT JOIN feedback_service AS s USING(name)
GROUP BY name)
UNION ALL
(SELECT name, AVG(l.overallQuality) AS avgLingQual,
AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic AS l
RIGHT JOIN feedback_service AS s USING(name)
WHERE l.id IS NULL
GROUP BY name)
ORDER BY name;
(This is somewhat simplified for readability but it doesn't make a difference here)
Problem
Next I tried adding filtering by date (i.e. only feedback items created after a certain date are taken in account). With my SQL skills and the research I did, I was able to come up with this:
(SELECT name, AVG(l.overallQuality) AS avgLingQual,
AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic AS l
LEFT JOIN feedback_service AS s USING(name)
WHERE (s.createdTime >= '" & date & "' OR s.createdTime IS NULL)
AND (l.createdTime >= '" & date & "' OR l.createdTime IS NULL)
GROUP BY name)
UNION ALL
(SELECT name, AVG(l.overallQuality) AS avgLingQual,
AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic AS l
RIGHT JOIN feedback_service AS s USING(name)
WHERE l.id IS NULL
AND (s.createdTime >= '" & date & "' OR s.createdTime IS NULL)
GROUP BY name)
ORDER BY name;
This almost works: the results I get look about right. However, a couple of feedback items are missing. For example, setting the date one month ago, I counted feedback for 21 different people in the database, but this query only returns 19 people. The worst thing is that I can't seem to find any similarities between the missing items.
Am I doing something wrong in this query? I think that the WHERE clause does the date filtering after the JOIN and ideally I would probably be doing it before. Then again, I don't know if this causes my problem and I also have no idea how to write this query differently.
I accepted Johans answer as he did a good job explaining this stuff to me and the answer is useful even in a more generic sense. However, I thought I'd also post the first solution I arrived to. It was using subqueries:
(SELECT name, AVG(l.overallQuality) AS avgLingQual,
AVG(s.overallSatisfaction) AS avgSvcQual
FROM (
SELECT * FROM feedback_linguistic WHERE createdTime >= '" & date & "'
) AS l
LEFT JOIN (
SELECT * FROM feedback_service WHERE createdTime >= '" & date & "'
) AS s USING(name)
GROUP BY name)
UNION ALL
(SELECT name, AVG(l.overallQuality) AS avgLingQual,
AVG(s.overallSatisfaction) AS avgSvcQual
FROM (
SELECT * FROM feedback_linguistic WHERE createdTime >= '" & date & "'
) AS l
RIGHT JOIN (
SELECT * FROM feedback_service WHERE createdTime >= '" & date & "'
) AS s USING(name)
WHERE l.id IS NULL
GROUP BY name)
ORDER BY name;
The results are correct with this query. However, the solution doesn't really look optimal, as subqueries are sometimes slow in my experience. Then again, I haven't done any performance analysis, so maybe using subqueries here is not a bottleneck. In any case it worked fast enough in my application.
A full outer join is a combination of 3 joins:
1- inner join between A and B
2- left exclusion join between A and B
3- right exclusion join between A and B
Note that the combination of an inner and a left exclusion join is a left outer join, so you normally rewrite the query as a left outer join + right exclusion join.
However for debugging purposes it can be useful to union all 3 joins and to add some marker as to which join does what:
/*inner join*/
(SELECT
'inner' as join_type
, COALESCE(s.name, l.name) as listname
, AVG(l.overallQuality) AS avgLingQual
, AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic l
INNER JOIN feedback_service s ON (l.name = s.name)
WHERE (s.createdTime >= '" & date & "' OR s.createdTime IS NULL)
AND (l.createdTime >= '" & date & "' OR l.createdTime IS NULL)
GROUP BY l.name)
UNION ALL
(SELECT
'left exclusion' as join_type
, COALESCE(s.name, l.name) as listname
, AVG(l.overallQuality) AS avgLingQual
, AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic l
LEFT JOIN feedback_service s ON (l.name = s.name)
WHERE s.id IS NULL
/*AND (s.createdTime >= '" & date & "' OR s.createdTime IS NULL) */
AND (l.createdTime >= '" & date & "' OR l.createdTime IS NULL)
GROUP BY l.name)
UNION ALL
(SELECT
'right exclusion' as join_type
, COALESCE(s.name, l.name) as listname
, AVG(l.overallQuality) AS avgLingQual
, AVG(s.overallSatisfaction) AS avgSvcQual
FROM feedback_linguistic l
RIGHT JOIN feedback_service s ON (s.name = l.name)
WHERE l.id IS NULL
AND (s.createdTime >= '" & date & "' OR s.createdTime IS NULL)
/*AND (l.createdTime >= '" & date & "' OR l.createdTime IS NULL) */
GROUP BY s.name)
ORDER BY listname;
I think that the WHERE clause does the date filtering after the JOIN and ideally I would probably be doing it before.
If you want to do the filtering before, then put it in the join clause.