having

GROUP BY WITH HAVING ( DISTINCT ) : PHP , MYSQL

家住魔仙堡 提交于 2019-12-13 00:50:07
问题 id | mid | pid | owgh | nwgh | 1 3 12 1.5 0.6 2 3 12 1.5 0.3 3 3 14 0.6 0.4 4 3 15 1.2 1.1 5 4 16 1.5 1.0 6 4 17 2.4 1.2 7 3 19 3.0 1.4 From Above i want total count of mid AND SUM of nwgh with its resp. id ex: mid=3 or mid=4 but with DISTINCT pid but please note sum of nwgh should not be DISTINCT Hence my result will be as below : mid | countmid | totalnwgh 3 4 (DISTINCT value) 3.8 (no DISTINCT it take both value of pid =12) 4 2 2.2 in above result mid = 3 have count 4 beause pid = 12 is

Mysql - find conversation only being held by two users

寵の児 提交于 2019-12-12 10:08:56
问题 I have a table called participants with the following fields: id user_id conversation_id The background is that there are conversations with at least two or more participants. I want to find a conversation that has only been held by two specified users, with no other users in the conversation. I have come up with the following: SELECT * FROM participants WHERE user_id = 1 OR user_id = 2 GROUP BY conversation_id HAVING COUNT(*) = 1 Given this content you can see that the users 1 and 2 share a

Android SQLiteDatabase.query() GROUP BY / HAVING with more than one column not working

岁酱吖の 提交于 2019-12-12 06:08:43
问题 In Android I am having a table which stores latitude and longitude values, along with a corresponding id that is associated with every row. when i am using the query SELECT latitude, longitude, COUNT(*) count FROM tasks GROUP BY latitude, longitude HAVING count > 1 It is giving me group by latitude only and not longitude. how can I achieve this? I want to get the count of tasks with unique lat long values only. is there any way to achieve this using Android SQLite? For example, with this data

Using a Result Set From a Sub SELECT When Duplicate Rows are Encountered

南笙酒味 提交于 2019-12-12 02:57:50
问题 First of all, the table I'm struggling with: DispatchLocations ================= DispatchID int StopNumber int Arrived bool Departed bool The idea is that on a trucking route, there are many stops in a dispatch. This is a list of each location on each dispatch (essentially, it's a route table). In this view, there should only be one dispatch for each row of output, which points to the "current stop" that the dispatch is at. SELECT TOP 4 DispatchID, Min(StopNumber) AS NextStop, Arrived,

Using alias in subquery

末鹿安然 提交于 2019-12-11 17:58:36
问题 I'm running the following query to get the open positions on a portfolio: SELECT SUM(trades.quantity) as total_quantity, SUM(trades.price) as total_cost, SUM(trades.price)/SUM(trades.quantity) as cost_per_share, trades.ticker, tickers.code FROM (trades) LEFT JOIN tickers ON trades.ticker = tickers.id GROUP BY tickers.code HAVING total_quantity > 0 ORDER BY tickers.code I'd like to add an extra column to show the weightening of a position, i.e.: total_cost/SUM(total_cost) -- Dividing any given

Why `having` only worked on fields that are `group by` on laravel eloquent?

二次信任 提交于 2019-12-11 17:11:43
问题 My script laravel eloquent like this : $query = $this->item->select('a.*','b.attribute_code') ->from('items as a') ->join('attr_maps as b','b.number','=','a.number') ->groupBy('a.number'); foreach($param as $key => $value) { $query = $query->having($key, '=', $value); } $query = $query->paginate(10); My $param is dynamic. It can change If $param is array('number'=>'1234') , it works. No error If $param is array('description'=>'test') , there exist error : Unknown column 'description' in

SQL HAVING BETWEEN a date range

萝らか妹 提交于 2019-12-11 13:37:35
问题 I am trying to retrieve the count of records between a certain date range that is specific to the users min date and max date in another table. This is what I have so far, however it is excluding at least 13 records that I know of. Can you tell if there is an error in my logic? Thanks in advance for any input you have! SELECT rtam.dbo.wfm_process_instance.user_id AS user_id, MIN(rtam.dbo.wfm_process_instance.LOCAL_USER_START_TIME) AS Min_Date, MAX(rtam.dbo.wfm_process_instance.LOCAL_USER

how can I get a field value in 3 association tables with the max of date

回眸只為那壹抹淺笑 提交于 2019-12-11 10:35:21
问题 So, I have a table and I want to get the value from one field in the record with the greatest DateTime() value in another field and where still another field is equal to a certain value. -- -- Structure de la table `site` -- CREATE TABLE IF NOT EXISTS `site` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `site_name` varchar(15) NOT NULL, `city` varchar(50) DEFAULT NULL, `address` varchar(80) DEFAULT NULL, `entity` varchar(50) DEFAULT NULL, `status_activity` tinyint(1) NOT NULL DEFAULT '1',

How to select grouped records only if every record in a group meets a certain criteria in MySQL?

。_饼干妹妹 提交于 2019-12-11 09:49:14
问题 I'm using MySQL 5.0.88/Coldfusion8 and I have a table that contains products based on barcodes/EANs. So a product 123 in size S,M,L,XL will have four records in the table product size ean qty disregard_inventory 123 S 1111111111111 5 0 123 M 1111111111112 7 0 123 L 1111111111113 1 0 123 XL 1111111111114 2 0 Right now I'm searching this table like so: SELECT count(a.id) AS total_records, a.disregard_inventory, a.qty FROM artikelstammdaten a ... GROUP BY a.style HAVING sum(a.qty) != 0 OR (a

Expand a GROUP BY and HAVING resultset

倾然丶 夕夏残阳落幕 提交于 2019-12-11 07:39:48
问题 Is there a way to expand/get all the records of a GROUP BY and HAVING query? For example, SELECT Column1, Column2, Column3, Count(*) as Count FROM table_name GROUP BY Column1, Column2, Column3 HAVING Count > '2' Is there an easier way to get all the records rather than going through the result set and doing SELECT * FROM table_name WHERE Column1 = 'this' AND Column2 = 'that' AND Column3 = 'more' for each record. If it cannot be done due to mysql or some other restrictions is there any other