linq distinct or group by multiple properties
It’s groups by needed properties and select: List<Product> result = pr.GroupBy(g => new { g.Title, g.Price }) .Select(g => g.First()) .ToList();
It’s groups by needed properties and select: List<Product> result = pr.GroupBy(g => new { g.Title, g.Price }) .Select(g => g.First()) .ToList();
Use a terms aggregation on the color field. And you need to pay attention to how that field you want to get distinct values on is analyzed, meaning you need to make sure you’re not tokenizing it while indexing, otherwise every entry in the aggregation will be a different term that is part of the … Read more
There’s not an exact equivalent to convert a Postgresql query that makes use of SELECT DISTINCT ON to MySQL. Postgresql SELECT DISTINCT ON In Postgresql, the following query will eliminate all rows where the expressions (col1, col2, col3) match, and it will only keep the “first col4, col5 row” for each set of matched rows: … Read more
This can be done very simple, you were pretty close already SELECT distinct id, DENSE_RANK() OVER (ORDER BY id) AS RowNum FROM table WHERE fid = 64
This is where the window function row_number() comes in handy: SELECT s.siteName, s.siteIP, h.date FROM sites s INNER JOIN (select h.*, row_number() over (partition by siteName order by date desc) as seqnum from history h ) h ON s.siteName = h.siteName and seqnum = 1 ORDER BY s.siteName, h.date
One way to get the list of distinct column names from the database is to use distinct() in conjunction with values(). In your case you can do the following to get the names of distinct categories: q = ProductOrder.objects.values(‘Category’).distinct() print q.query # See for yourself. # The query would look something like # SELECT DISTINCT … Read more
You can achieve the desired result by requesting a list of distinct ids instead of a list of distinct hydrated objects. Simply add this to your criteria: criteria.setProjection(Projections.distinct(Projections.property(“id”))); Now you’ll get the correct number of results according to your row-based limiting. The reason this works is because the projection will perform the distinctness check as … Read more
You can use this: SELECT COUNT(*) FROM (SELECT DISTINCT column_name FROM table_name) AS temp; This is much faster than: COUNT(DISTINCT column_name)
You can use the DISTINCT keyword within the COUNT aggregate function: SELECT COUNT(DISTINCT column_name) AS some_alias FROM table_name This will count only the distinct values for that column.
I know that the question is too old, anyway: SELECT a, b FROM mytable GROUP BY a, b; This will give your all the combinations.