distinct

How to get a SQL Alchemy object of distinct items with filter on related objects

别等时光非礼了梦想. 提交于 2019-12-25 07:46:45
问题 I am trying to get a SQL Alchemy query of distinct items below filtering on related objects, the equivalent of the below query: SELECT distinct items.item_id, items.item_name FROM items INNER JOIN categories as cat on items.category_id = cat.category_id INNER JOIN stores on cat.store_id = stores.store_id WHERE store.store_id = 123 I have created the models as below with foreign keys included but when I run the query below it does not filter correctly. items_query = (db.session.query(Store,

Django distinct() not returning distinct values

瘦欲@ 提交于 2019-12-25 03:06:24
问题 I have a Session model like this: class Session(models.Model): user = models.ForeignKey(User, null=True, blank=True, on_delete=models.CASCADE, related_name="sessions") flavor = models.ForeignKey(Flavor, null=True, blank=True, on_delete=models.CASCADE, related_name="sessions") .... And I'm trying to run a query: sessions = Session.objects.all().values('flavor__pk', 'user__pk').distinct() But when I then print the sessions object I get this: <QuerySet [{'user__pk': 14544, 'flavor__pk': 1}, {

Select distinct + select top to merge multiple rows

天大地大妈咪最大 提交于 2019-12-24 22:14:19
问题 I'm trying to select rows from a table, one row per email address, and return one firstname from the top row in the email list. The query, though, returns multiple email addresses. What am I doing wrong? SELECT DISTINCT email, (SELECT TOP 1 firstname FROM onsite_clients_archive oc WHERE oc.client_id=oca.client_id ORDER BY client_id) FROM onsite_clients_archive oca WHERE users_user_id IS NULL 回答1: Your bug is WHERE oc.client_id = oca.client_id should be WHERE oc.email = oca.email . You didn't

$elemMatch with distinct

时光总嘲笑我的痴心妄想 提交于 2019-12-24 17:13:43
问题 I have some problems with distinct queries. db.sessions.distinct("tests.device_serial") [ "", "5b34f4bf9854a", "5b34f4bf98664", "5b34f4bf98712", "5b34f4bf9876b", "5b34f4bf987c6" ] I don't want to get the result with empty strings. I tried to run query: db.sessions.distinct("tests.device_serial", {"tests.device_serial" : {$ne: ""}}) [ ] Why I got empty array? Where is my mistake? 回答1: Guessing tests.device_serial is an array, here's your mistake : db.sessions.distinct("tests.device_serial", {

Apply Distinct Function on TreeMap

我与影子孤独终老i 提交于 2019-12-24 17:05:05
问题 Code: Map<Integer, HashSet<String>> test = new TreeMap<>(); test.put(1, new HashSet<>()); test.put(2, new HashSet<>()); test.put(3, new HashSet<>()); test.put(4, new HashSet<>()); test.get(1).add("1"); test.get(2).add("2"); test.get(3).add("2"); test.get(4).add("3, 33"); //get value of treemap and get rid of the duplicate by using distinct and printout //at the end test.values().stream().distinct().forEach(i -> System.out.println(i)); output: [1] [2] [3, 33] My question is how I can printout

Select Distinct from DataTable using Linq and C#

和自甴很熟 提交于 2019-12-24 13:20:46
问题 I need to select distinct records from a data table using linq and C# and I can't seem to get the syntax correct. I have the following code, which returns all the rows in a data table, how do I just return DISTINCT rows? DataTable dt = ds.Tables[0]; var q = from dr in dt.AsEnumerable() select dr; 回答1: You'll need to use DataRowComparer IEnumerable<DataRow> distinctRows = dt.AsEnumerable().Distinct(DataRowComparer.Default); More info on comparing data rows using linq to dataset can be found

Show only last duplicates in access query

◇◆丶佛笑我妖孽 提交于 2019-12-24 12:34:55
问题 I have a database in access where I log maps which I loan out. So I have a table called MapsOut, with fields, MapNum(The map ID), MapName (Name of the Map), CheckOut (Date the Maps were loaned out), CheckIn (Date the Maps were returned). MapNum MapName CheckOut CheckIn 1 London 01/02/13 07/05/13 1 London 08/05/13 16/06/13 1 London 19/07/13 2 Hull 30/01/13 05/03/13 2 Hull 06/04/13 01/05/13 3 Derby 11/01/13 17/02/13 3 Derby 05/09/13 06/10/13 4 Hove 01/02/13 01/03/13 I want to write a query that

is distinct an expensive query in django?

谁说我不能喝 提交于 2019-12-24 09:58:15
问题 I have three models: Product, Category and Place. Product has ManyToMany relation with Category and Place. I need to get a list of categories with at least on product matching a specific place. For example I might need to get all the categories that has at least one product from Boston. I have 100 categories, 500 places and 100,000 products. In sqlite with 10K products the query takes ~ a second. In production I'll use postgresql. I'm using: categories = Category.objects.distinct().filter

MySql SELECT only newest message from distinct threads order by timestamp -Private Messages Inbox Similar to Facebooks(2013)

馋奶兔 提交于 2019-12-24 05:59:34
问题 Trying to recreate a private message system similar to what Facebooks setup these days. The part im having a problem with is SELECT only newest message from distinct threads order by timestamp. Here is the closest query I could come up with so far. The problem is the inbox should only return one row for each thread and currently my query returns multiple rows from the same thread: SELECT m.created_on, m.thread_id, m.message_id, m.created_by, m.body, u.first_name, u.last_name, u.thumb_img FROM

Hive高性能优化

非 Y 不嫁゛ 提交于 2019-12-24 04:37:25
1 sql书写规范 尽量尽早地过滤数据,减少每个阶段的数据量,对于分区表要加分区。 案例: SELECT a.key,col1,col2,col3,col4 FROM A a LEFT OUTER JOIN B b ON a.key = b.key and a.dt=‘20150101’ and b.dt=‘20150101'; 优化方法1: SELECT ... FROM A LEFT OUTER JOIN B ON A.key = B.key WHERE A.dt='20150101' AND B.dt='20150101'; 优化方法2: SELECT a.key,col1,col2,col3,col4 FROM (SELECT key,col1,col2 FROM A WHERE dt='20150101') a LEFT OUTER JOIN (SELECT key,col3,clo4 FROM B WHERE dt='20150101' ) b ON a.key = b.key; 2 设置合理的map、reduce数 2.1 splitSize的计算方式 map数的主要决定因素有: input的文件总个数,input的文件大小,集群设置的文件块大小(blockSize默认为64M),以及maxSize(mapred.max.split.size)决定。