having

SQL Query to get common records

爷,独闯天下 提交于 2019-12-03 16:15:34
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 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 | An alternate approach using a plain JOIN ; SELECT DISTINCT t1.username FROM MyTable t1 JOIN MyTable t2 ON t1.username = t2.username

mysql group by having min

痞子三分冷 提交于 2019-12-03 09:08:23
Below is the table data (a small piece) basically I'm looking to query just the rows with the minimum original_date_ctr when grouped by the accoutn number. I've tried using HAVING(MIN()), and where = Min() and other ways with no luck. The correct result here would give me id_ctr 688, 1204 and 1209 id_ctr account_number_cus original_date_ctr mrc_ctr ------ ------------------ ----------------- ---------- 688 20062 2008-05-17 138.97 1204 151604 2006-08-10 42000.00 1209 151609 2006-06-29 968.68 1367 20062 2011-10-27 207.88 1434 151609 2009-09-10 1469.62 1524 151604 2009-09-01 36999.99 1585 151609

It's possible to have a WHERE clause after a HAVING clause?

时光毁灭记忆、已成空白 提交于 2019-12-03 05:54:44
问题 Is it possible to use a WHERE clause after a HAVING clause? The first thing that comes to my mind is sub queries, but I'm not sure. P.S. If the answer is affirmative, could you give some examples? 回答1: No, not in the same query. The where clause goes before the having and the group by . If you want to filter out records before the grouping the condition goes in the where clause, and if you want to filter out grouped records the condition goes in the having clause: select ... from ... where ..

MongoDB group by Functionalities

落花浮王杯 提交于 2019-12-03 03:21:39
问题 In MySQL select a,b,count(1) as cnt from list group by a, b having cnt > 2; I have to execute the group by function using having condition in mongodb. But i am getting following error. Please share your input. In MongoDB > res = db.list.group({key:{a:true,b:true}, ... reduce: function(obj,prev) {prev.count++;}, ... initial: {count:0}}).limit(10); Sat Jan 7 16:36:30 uncaught exception: group command failed: { "errmsg" : "exception: group() can't handle more than 20000 unique keys", "code" :

It's possible to have a WHERE clause after a HAVING clause?

早过忘川 提交于 2019-12-02 19:18:04
Is it possible to use a WHERE clause after a HAVING clause? The first thing that comes to my mind is sub queries, but I'm not sure. P.S. If the answer is affirmative, could you give some examples? No, not in the same query. The where clause goes before the having and the group by . If you want to filter out records before the grouping the condition goes in the where clause, and if you want to filter out grouped records the condition goes in the having clause: select ... from ... where ... group by ... having ... If neither of those are possible to use for some odd reason, you have to make the

MongoDB group by Functionalities

五迷三道 提交于 2019-12-02 17:48:25
In MySQL select a,b,count(1) as cnt from list group by a, b having cnt > 2; I have to execute the group by function using having condition in mongodb. But i am getting following error. Please share your input. In MongoDB > res = db.list.group({key:{a:true,b:true}, ... reduce: function(obj,prev) {prev.count++;}, ... initial: {count:0}}).limit(10); Sat Jan 7 16:36:30 uncaught exception: group command failed: { "errmsg" : "exception: group() can't handle more than 20000 unique keys", "code" : 10043, "ok" : 0 Once it will be executed, we need to run the following file on next. for (i in res) {if

midterm solution around query in SQL

自古美人都是妖i 提交于 2019-12-02 11:38:16
This is the database: EMPLOYEE (fmane, minit, lname, ssn, birthdate, address, sex, salary, superssn, dno) KEY: ssn DEPARTMENT (dname, dnumber, mgrssn, mgrstartdate) KEY: dnumber. PROJECT (pname, pnumber, plocation, dnum) KEY: pnumber. WORKS_ON (essn, pno, hours) KEY: (essn, pno) DEPENDENT (essn, dependent-name, sex, bdate, relationship) KEY: (essn, dependent-name) The question asked is... Give the last name and SSN of the unmarried employees who work on two or more projects. SELECT e.Lname, e.ssn FROM Employee AS e WHERE e.ssn IN ( SELECT w.essn FROM works_on w GROUP BY w.essn HAVING count(*)

What is the semantic difference between WHERE and HAVING?

梦想与她 提交于 2019-12-02 03:25:09
Let's put GROUP BY aside for a second. In normal queries (without GROUP BY ), what is the semantic difference? Why does this answer work? (put an alias in a HAVING clause instead of WHERE ) HAVING operates on the summarized row - WHERE is operating on the entire table before the GROUP BY is applied. (You can't put GROUP BY aside, HAVING is a clause reserved for use with GROUP BY - leaving out the GROUP BY doesn't change the implicit action that is occurring behind the scenes). It's also important to note that because of this, WHERE can use an index while HAVING cannot. (In super trivial un

SELECT From MySQL View With HAVING Clause Returns Empty Result Set

喜你入骨 提交于 2019-12-01 18:56:40
My business partner and I are having issues selecting from a MySQL view that has a HAVING clause. The query simply selects a few fields from the view, determines a distance dynamically with a few calculations, and aliases it as 'distance' - then limits the results to those rows with a distance less than a supplied variable. The distance is calculated using the Haversine formula, referenced by Google Maps: https://developers.google.com/maps/articles/phpsqlsearch Here is what I know: 1) When the HAVING clause is removed from the query, it returns all the results in the view successfully,

… where count(col) > 1

亡梦爱人 提交于 2019-12-01 14:58:56
I have a table like this: +-----+-----+-------+ | id | fk | value | +-----+-----+-------+ | 0 | 1 | peter | | 1 | 1 | josh | | 3 | 2 | marc | | ... | ... | ... | I'd like now to get all entries which have more than one value. The expected result would be: +-----+-------+ | fk | count | +-----+-------+ | 1 | 2 | | ... | ... | I tried to achieve that like this: select fk, count(value) from table where count(value) > 1; But Oracle didn't like it. So I tried this... select * from ( select fk, count(value) as cnt from table ) where cnt > 1; ...with no success. Any ideas? Donnie Use the having