having-clause

What is the correct way to do a HAVING in a MongoDB GROUP BY?

≡放荡痞女 提交于 2019-12-17 10:53:46
问题 For what would be this query in SQL (to find duplicates): SELECT userId, name FROM col GROUP BY userId, name HAVING COUNT(*)>1 I performed this simple query in MongoDB: res = db.col.group({key:{userId:true,name:true}, reduce: function(obj,prev) {prev.count++;}, initial: {count:0}}) I've added a simple Javascript loop to go over the result set, and performed a filter to find all the fields with a count > 1 there, like so: for (i in res) {if (res[i].count>1) printjson(res[i])}; Is there a

Skip whole row if aggregated value is null

偶尔善良 提交于 2019-12-13 17:40:56
问题 This is my approach: select distinct (invoice_no) as no,sum(total), sum(case when department_id=2 then total end) as a2, sum(case when department_id=3 then total end) as a3, sum(case when department_id=4 then total end) as a4, sum(case when department_id=5 then total end) as a5, sum(case when department_id=6 then total end) as a6 from article_sale where invoice_date = '2018-10-01' group by no order by no ASC The query returns output like this: no sum a2 a3 a4 a5 a6 68630 690 NULL 75 404 NULL

How to get total number of rows within subquery for multiple conditions

对着背影说爱祢 提交于 2019-12-13 07:11:50
问题 This query return number of rows where Sum of Product's Quantity is equal to 0 and Product Name is 'XYZ'. But I want same result for each products. For example if I remove product name from where clause so it will bring SUM of every product where quantity is 0. I want it to return separately for each products. Products may increase in future. I have to first check how many distinct products are there in CustomerProduct table and then need to query same things for each product. How can I do

Confusion between HAVING and WHERE, SQL Server

放肆的年华 提交于 2019-12-12 03:53:05
问题 I am trying to fetch all vendors whose product's quantity ( SUM ) is equal to zero. This is what I have done so far but it doesn't return any row nor it gives any error. I am making sure there are products whose quantity is zero and script is running on proper database. select VI.Name, Cp.ProductName from VendorInfo VI inner join VendorTrading VT on VI.Id = VT.VendorId inner join CustomerProducts CP on VT.Id = CP.VendorTradingId group by VI.Name, CP.ProductName having sum(CP.ProductQuantity)

WHERE clause in sub-query

╄→гoц情女王★ 提交于 2019-12-11 20:18:38
问题 Following SQL query: select * from er_101 where cd_relnaam IN ( select cd_relnaam from er_101 group by cd_relnaam having count(*) > 1) AND ld_relopdrachtgever = '1' Though I need to have that sub query also limits on ld_relopdrachtgever = '1' How is that possible with an HAVING statement? 回答1: You can also use WHERE in sub-query. SELECT * FROM er_101 WHERE cd_relnaam IN ( SELECT cd_relnaam FROM er_101 WHERE ld_relopdrachtgever = '1' <--You can add WHERE clause before GROUP BY --^^^^^----

SQL group by returns just first row

蓝咒 提交于 2019-12-11 10:15:48
问题 I have a table which contains two columns 'id' and 'layout plan' I need to see all those rows which have the same layout plan I use this query. select * from project_layout group by layout_plan having count(layout_plan) > 1 But this query is only returning the first row. I want to see all the groups with the same layout plan. 回答1: Databases other than MySQL would give an error if you use a column that's not grouped without an aggregate. But MySQL will return an indeterminate value from among

difference between where and having with respect to aliases

家住魔仙堡 提交于 2019-12-10 04:35:47
问题 If I create an alias in the select clause then I cannot use it in the where clause because according to the order of execution of sql queries where comes before select . But I can create an alias in the select clause and use it in a having clause though having comes before select . Why is it so? Ex: select type, (case when number>25 then 1 else 0 end) inc from animals where inc='1'; this wont work. But, select type, (case when number>25 then 1 else 0 end) inc from animals having inc='1'; This

Why do you have 'where' when there is 'having' [duplicate]

一世执手 提交于 2019-12-08 17:26:39
问题 This question already has answers here : SQL - having VS where (7 answers) Closed 6 years ago . I know this is much discussed, but none of my research could convince me the difference between ' where ' and ' having ' clauses in MySQL. From what I understand we can achieve everything that can be done with 'where' clause using ' having ' . For eg. select * from users having username='admin' . Then why do you need ' where ' clause? Does using where make any performance differences? 回答1: The

how to use SQL group to filter rows with maximum date value

你。 提交于 2019-12-05 23:11:19
问题 I have the following table CREATE TABLE Test (`Id` int, `value` varchar(20), `adate` varchar(20)) ; INSERT INTO Test (`Id`, `value`, `adate`) VALUES (1, 100, '2014-01-01'), (1, 200, '2014-01-02'), (1, 300, '2014-01-03'), (2, 200, '2014-01-01'), (2, 400, '2014-01-02'), (2, 30 , '2014-01-04'), (3, 800, '2014-01-01'), (3, 300, '2014-01-02'), (3, 60 , '2014-01-04') ; I want to achieve the result which selects only Id having max value of date. ie Id ,value ,adate 1, 300,'2014-01-03' 2, 30 ,'2014

SELECT id HAVING maximum count of id

狂风中的少年 提交于 2019-11-30 11:42:55
Have a products table with item_id and color_id. I'm trying to get the color_id with the most non-null instances. This fails: SELECT color_id FROM products WHERE item_id=1234 GROUP BY item_id HAVING MAX(COUNT(color_id)) with Invalid use of group function This SELECT color_id, COUNT(color_id) FROM products WHERE item_id=1234 GROUP BY item_id Returns color_id count 1, 323 2, 122 3, 554 I am looking for color_id 3, which has the most instances. Is there a quick and easy way of getting what I want without 2 queries? SELECT color_id AS id, COUNT(color_id) AS count FROM products WHERE item_id = 1234