distinct

PostgreSQL - Select distinct(column1, column2) where a condition holds

∥☆過路亽.° 提交于 2019-12-24 03:40:06
问题 I have the following table and some sample records in it: id | attr1_id | attr2_id | user_id | rating_id | ------+----------+----------+-------------------+-----------+ 1 | 188 | 201 | user_1@domain.com | 3 | 2 | 193 | 201 | user_2@domain.com | 2 | 3 | 193 | 201 | user_2@domain.com | 1 | 4 | 194 | 201 | user_2@domain.com | 1 | 5 | 194 | 201 | user_1@domain.com | 1 | 6 | 192 | 201 | user_2@domain.com | 1 | The combination of ( attr1_id , attr2_id , user_id ) is UNIQUE , meaning each user can

How to return distinct values and their count?

北城以北 提交于 2019-12-24 03:17:07
问题 What I'm trying to do is (hopefully) simple, but I just don't quite have the right syntax. I'd like to return all distinct values in a table with a count of how many records for each value. So, in PHP, I've got: $result = mysql_query("SELECT DISTINCT tagName FROM tagTable"); while($row = mysql_fetch_array($result)){ echo("<p>" . $row['tagName']) . "</p>"); } This works well. The distinct values are returned! But now how do I get each distinct value's count to display as well? I would want

How to append distinct records from one table to another

一个人想着一个人 提交于 2019-12-24 01:47:14
问题 How do I append only distinct records from a master table to another table, when the master may have duplicates. Example - I only want the distinct records in the smaller table but I need to insert/append records to what I already have in the smaller table. 回答1: Ignoring any concurency issues: insert into smaller (field, ... ) select distinct field, ... from bigger except select field, ... from smaller; You can also rephrase it as a join: insert into smaller (field, ... ) select distinct b

Need a distinct count on multiple fields that were joined from another collection using mongodb aggregation query

风流意气都作罢 提交于 2019-12-24 00:48:45
问题 I'm trying to use a mongodb aggregation query to join($lookup) two collections and then distinct count all the unique values in the joined array. So my two collections look like this: events- { "_id" : "1", "name" : "event1", "objectsIds" : [ "1", "2", "3" ], } Objects { "_id" : "1", "name" : "object1", "metaDataMap" : { "SOURCE" : ["ABC", "DEF"], "DESTINATION" : ["XYZ", "PDQ"], "TYPE" : [] } }, { "_id" : "2", "name" : "object2", "metaDataMap" : { "SOURCE" : ["RST", "LNE"], "TYPE" : ["text"]

SQL Server : select distinct by one column and by another column value

南笙酒味 提交于 2019-12-23 20:04:34
问题 This is a SQL Server table's data id user_id start_date status_id payment_id ====================================================== 2 4 20-nov-11 1 5 3 5 23-nov-11 1 245 4 5 25-nov-11 1 128 5 6 20-nov-11 1 223 6 6 25-nov-11 2 542 7 4 29-nov-11 2 123 8 4 05-jan-12 2 875 I need to get distinct values by user_id also order by id asc , but only one user_id with highest start_date I need the following output: id user_id start_date status_id payment_id ==============================================

Elasticsearch: how to get the top unique values of a field sorted by matching score?

纵饮孤独 提交于 2019-12-23 19:46:49
问题 I have a collection of addresses. Let's simplify and say the only fields are postcode , city , street , streetnumber and name . I'd like to be able to suggest a list of streets when the user enters a postcode, a city and some query for the street. For example, if the user, in a HTML form, enters: postcode: 75010 city: Paris street: rue des I'd like to get a list of streets like 'rue des petites écuries' 'rue des messageries' ... 'rue du faubourg poissonnière' ... that I could suggest to the

Sqlalchemy with postgres. Try to get 'DISTINCT ON' instead of 'DISTINCT'

僤鯓⒐⒋嵵緔 提交于 2019-12-23 15:52:24
问题 I need to generate query like this: SELECT **DISTINCT ON** (article.code) article.code, article.title First I try to make it via ORM distinct method and send it a list with fields. But it wont work. Second, I try to make it via sqlalchemy.sql.select - and it also generate sql query like this: SELECT DISTINCT article.code, article.title I Need SELECT **DISTINCT ON** (article.code) ... I look at source code and found in sqlalchemy.dialects.postgresql.base.PGCompiler.get_select_precolumns code

Distinct Date with Linq

ぃ、小莉子 提交于 2019-12-23 09:57:22
问题 I have an entity called Billings with a property of DateTime. I need to uniquely find the dates so I can go through them and do some stuff. I tried: var DistinctYearMonthsDays = (from t in db.Billings where t.DateTime.HasValue select t.DateTime.Value.Date).Distinct(); foreach (var day in DistinctYearMonthsDays) but I get (on the foreach ): The specified type member 'Date' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.

SQL: count number of distinct values in every column

南楼画角 提交于 2019-12-23 08:04:59
问题 I need a query that will return a table where each column is the count of distinct values in the columns of another table. I know how to count the distinct values in one column: select count(distinct columnA) from table1; I suppose that I could just make this a really long select clause: select count(distinct columnA), count(distinct columnB), ... from table1; but that isn't very elegant and it's hardcoded. I'd prefer something more flexible. 回答1: try this (sql server 2005 syntax): DECLARE

distinct() function (not select qualifier) in postgres

三世轮回 提交于 2019-12-23 07:58:18
问题 I just came across a SQL query, specifically against a Postgres database, that uses a function named "distinct". Namely: select distinct(pattern) as pattern, style, ... etc ... from styleview where ... etc ... Note this is NOT the ordinary DISTINCT qualifier on a SELECT -- at least it's not the normal syntax for the DISTINCT qualifier, note the parentheses. It is apparently using DISTINCT as a function, or maybe this is some special syntax. Any idea what this means? I tried playing with it a