having

MySQL GROUP BY…HAVING different values same field

安稳与你 提交于 2019-12-06 06:49:51
问题 I want to use a query similar to the following to retrieve all rows in events that have at least one corresponding event_attendances row for 'male' and 'female' . The below query returns no rows (where there certainly are some events that have event_attendances from both genders). Is there a way to do this without a subquery (due to the way the SQL is being generated in my application, a subquery would be considerably more difficult for me to implement)? SELECT * FROM events e LEFT JOIN event

SQL: HAVING clause

五迷三道 提交于 2019-12-06 02:07:46
See the following SQL statement: SELECT datediff("d", MAX(invoice.date), Now) As Date_Diff , MAX(invoice.date) AS max_invoice_date , customer.number AS customer_number FROM invoice INNER JOIN customer ON invoice.customer_number = customer.number GROUP BY customer.number If the the following was added: HAVING datediff("d", MAX(invoice.date), Now) > 365 would this simply exclude rows with Date_Diff <= 365? What should be the effect of the HAVING clause here? EDIT: I am not experiencing what the answers here are saying. A copy of the mdb is at http://hotfile.com/dl/40641614/2353dfc/test.mdb.html

oracle sql select syntax with GROUP BY and HAVING clause

与世无争的帅哥 提交于 2019-12-05 08:52:45
I been going thru some of the sql syntax to study for the oracle sql exam, I found something rather confusing based on the official references, the select syntax is as follow : SELECT [ hint ] [ { { DISTINCT | UNIQUE } | ALL } ] select_list FROM { table_reference | join_clause | ( join_clause ) } [ , { table_reference | join_clause | (join_clause) } ] ... [ where_clause ] [ hierarchical_query_clause ] [ group_by_clause ] [ HAVING condition ] [ model_clause ] based on this you cannot have the HAVING clause before the GROUP BY clause . However if i were to execute the following sql in the test

sql - find average salary for each department with more than five members

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 07:44:11
问题 Not quite sure how to get this one. I have a staff table and I need to find the average salary. I know I can use use avg() . But the trick is I need to find the average for departments that have more than 5 staff members. I'm not sure if I should use group by or how to use it. Thanks! CREATE TABLE STAFF (STAFF_ID CHAR(3), STAFF_NAME CHAR(20), GENDER CHAR(6), DEPARTMENT CHAR(20), BOSS_ID CHAR(3) SALARY NUMBER(8,2)); 回答1: select DEPARTMENT,count(STAFF_ID) as CountStaff, avg(SALARY) as AVGSalary

PostgreSQL - Aliases column and HAVING

♀尐吖头ヾ 提交于 2019-12-05 04:10:14
SELECT CASE WHEN SUM(X.Count)*3600 is null THEN '0' ELSE SUM(X.Count)*3600 END AS PJZ, X.Mass FROM X WHERE X.Mass > 2000 HAVING ((X.Mass / PJZ * 100) - 100) >= 10; Getting: ERROR: Column »pjz« doesn't exists. How can I do something like this? Wrap it into a derived table: SELECT CASE WHEN PJZ = 0 THEN 100 ELSE PJZ END as PJZ, mass FROM ( SELECT CASE WHEN SUM(X.Count)*3600 is null THEN '0' ELSE SUM(X.Count)*3600 END AS PJZ, X.Mass FROM X WHERE X.Mass > 2000 GROUP BY X.mass ) t WHERE PJZ = 0 OR ((X.Mass / PJZ * 100) - 100) >= 10; (Note that I added the missing group by as otherwise the query

MySQL Update Subset Having

感情迁移 提交于 2019-12-05 03:51:21
I have three tables: contacts, domains, and contacts_domains, which form a many-to-many relationship. I would like to run a query that updates the contacts_domains table, but only for domains that have exactly one contact. I know how to SELECT the rows I'm interested in, but not how to UPDATE them. SELECT domain_id, contact_id, dominant FROM contacts_domains GROUP BY domain_id HAVING COUNT(contact_id) = 1 I want to set contacts_domains.dominant = 1 for all these results. Thanks! santiagobasulto The simplest solution: UPDATE contacts_domains cd SET cd.dominant = 1 WHERE cd.id IN ( SELECT

SQL Query to get common records

不打扰是莪最后的温柔 提交于 2019-12-05 00:55:27
问题 I have a table as below ID Username GroupID 1 venkat 2 2 venkat 3 3 ramu 1 4 ramu 2 Using the sql statement I want to retrieve all username's that are available in both the groupids 2,3 In this case only Venkat is the username that's available in both groupid 2 and 3 Kindly help me 回答1: Try this: SELECT userName FROM tableA WHERE groupId IN (2, 3) GROUP BY userName HAVING COUNT(DISTINCT groupId) = 2; Check the SQL FIDDLE DEMO OUTPUT | USERNAME | |----------| | venkat | 回答2: An alternate

How to reduce SQL queries in only one in this case

佐手、 提交于 2019-12-04 20:47:17
I want to save the hassle of doing many querys for the following: I have a table like this: name, age { Mike, 7 Peter, 2 Mario, 1 Tony, 4 Mary, 2 Tom, 7 Jerry, 3 Nick, 2 Albert, 22 Steven, 7 } And I want the following result: Results(custom_text, num) { 1 Year, 1 2 Year, 3 3 Year, 1 4 Year, 1 5 Year, 0 6 Year, 0 7 Year, 3 8 Year, 0 9 Year, 0 10 Year, 0 More than 10 Year, 1 } I know how to do this but in 11 queries :( But how to simplify it? EDIT: Doing the following, I can obtain the non zero values, but I need the zeroes in the right places. SELECT COUNT(*) AS AgeCount FROM mytable GROUP BY

MySQL GROUP BY…HAVING different values same field

时光总嘲笑我的痴心妄想 提交于 2019-12-04 13:41:50
I want to use a query similar to the following to retrieve all rows in events that have at least one corresponding event_attendances row for 'male' and 'female' . The below query returns no rows (where there certainly are some events that have event_attendances from both genders). Is there a way to do this without a subquery (due to the way the SQL is being generated in my application, a subquery would be considerably more difficult for me to implement)? SELECT * FROM events e LEFT JOIN event_attendances ea ON (e.id = ea.event_id) GROUP BY e.id HAVING ea.gender = 'female' AND ea.gender = 'male

sql - find average salary for each department with more than five members

*爱你&永不变心* 提交于 2019-12-03 21:58:11
Not quite sure how to get this one. I have a staff table and I need to find the average salary. I know I can use use avg() . But the trick is I need to find the average for departments that have more than 5 staff members. I'm not sure if I should use group by or how to use it. Thanks! CREATE TABLE STAFF (STAFF_ID CHAR(3), STAFF_NAME CHAR(20), GENDER CHAR(6), DEPARTMENT CHAR(20), BOSS_ID CHAR(3) SALARY NUMBER(8,2)); Vikram select DEPARTMENT,count(STAFF_ID) as CountStaff, avg(SALARY) as AVGSalary from STAFF group by DEPARTMENT having count(STAFF_ID) > 5 来源: https://stackoverflow.com/questions