sql-order-by

Linq: OrderBy Sum of multiple fields

强颜欢笑 提交于 2019-12-24 09:21:46
问题 I'm trying to create a linq expression whereby I can COUNT the number of event comments as well as SUM the number of Votes. Here's what I've got, but I'm not getting the appropriate results. My example is done in VB.Net, but I can deal with examples in C# as well. Public Function GetHotEvents(ByVal skip As Integer) As List(Of Domain.Event) Implements IEventService.GetHotEvents ''# Our order by sequence just takes the number of votes and multiplies ''# it by two. Then takes the number of

ascending or descending order according to boolean column

怎甘沉沦 提交于 2019-12-24 08:48:15
问题 This is the table structure in PostgreSQL 10. Even though "date" is an int , it represents a yyyy mm dd date. I am trying to write a SELECT that orders by date and when it is BC=true it is desc , so dates will be in the right order -500 01 02 then -500 01 03 (yyyy mm dd) and when is is BC=false it is asc , so dates will be in the right order again 1500 01 02, then 150 01 03 (yyyy mm dd) I came up with this SELECT * FROM test ORDER BY bc=true desc, bc=false asc; that does great on BC dates,

How to sort on a column in Lexicographical order in php?

早过忘川 提交于 2019-12-24 08:25:25
问题 I am working on the website in which I want to sort everything on the basis of customer_name (which is the 1st column from the table) .. Below is my query in php code I have: ..... ..... $y = isset($filters['items']['yaxis']) ? $filters['items']['yaxis'] : null; $x = isset($filters['items']['xaxis']) ? $filters['items']['xaxis'] : null; switch ($y) { case 'customer_name': $query->order( columns: $y . ' ' . (strtoupper($x) == 'DESC' ? 'DESC' : 'ASC')); break; } Problem Statement : I am

Postgres RETURNING clause with join and order

谁说胖子不能爱 提交于 2019-12-24 07:50:03
问题 I've got this query that updates some rows and returns the updated rows in the RETURNING clause. However, even though I've specified ORDER BY mycolumn in the inner query, the rows returned by RETURNING aren't ordered. UPDATE mytable SET status = 'A' FROM ( SELECT id FROM mytable WHERE status = 'B' ORDER BY mycolumn LIMIT 100 FOR UPDATE ) sub JOIN jointable j ON j.id = sub.id WHERE mytable.id = sub.id RETURNING * I tried putting an ORDER BY in the outer query, like after the JOIN or after the

sorting error in union query

Deadly 提交于 2019-12-24 06:51:12
问题 SELECT DISTINCT rt . d_rev_id , rt . d_rev_code , rt . d_reason , rt . d_rev_status , rt . d_apb , rt . d_cb , pt . d_partid , pt . d_part_no , pt . d_ab , pt . d_abd , pt . d_status , rt . d_part_name , rt . d_part_desc , rt . d_part_type , pnv . d_pn_val , pnv . d_pn_id , cfv . d_optionname , rt . d_projectid , rt . d_abd , rt . d_apbd FROM ( ( design_parts pt INNER JOIN design_part_number_val pnv USING ( d_partid ) INNER JOIN design_revision_temp rt USING ( d_partid ) ) LEFT JOIN design_pn

sorting error in union query

 ̄綄美尐妖づ 提交于 2019-12-24 06:51:09
问题 SELECT DISTINCT rt . d_rev_id , rt . d_rev_code , rt . d_reason , rt . d_rev_status , rt . d_apb , rt . d_cb , pt . d_partid , pt . d_part_no , pt . d_ab , pt . d_abd , pt . d_status , rt . d_part_name , rt . d_part_desc , rt . d_part_type , pnv . d_pn_val , pnv . d_pn_id , cfv . d_optionname , rt . d_projectid , rt . d_abd , rt . d_apbd FROM ( ( design_parts pt INNER JOIN design_part_number_val pnv USING ( d_partid ) INNER JOIN design_revision_temp rt USING ( d_partid ) ) LEFT JOIN design_pn

Sort Order in SQL Server

て烟熏妆下的殇ゞ 提交于 2019-12-24 06:26:31
问题 Can I change the default sort order in a SQL server database so that nulls and zero length strings are displayed last. Its SQL Server 2000 I stress, I want to change the default order for all queries, if possible 回答1: You can do almost any sort using a case in an order by . Here's the null columns first, then the empty strings, and the rest ordered on col1 and col3: select * from YourTable order by case when col1 is null then 1 when col1 = '' then 2 else 3 end , col2 , col3 desc 回答2: No you

Mysql - return last 3 results in table

我的梦境 提交于 2019-12-24 06:17:33
问题 i was wondering if there was an easy way with just an sql statement to return the last three results in the table but in that order i.e. if there are a hundered results it would return in the order of 98, 99, 100 not simply ordering by id DESC and limit 3 which would return in order 100, 99, 98 Any help much appreciated. p.s. in this instance, lets say I don't know the amount of results and don't really want to send 2 sql requests just to find the amount ( for any OFFSET answers ). 回答1: One

Using regex with LIKE to sort alphabets first then symbols SQL

余生颓废 提交于 2019-12-24 03:52:33
问题 I have the following MySQL Query SELECT * FROM `travels`.`destinations` AS `Des` WHERE `Des`.`name` LIKE '%act%' AND `Des`.`sold` = 'N' AND `Des`.`active` = '1' GROUP BY `Des`.`name` ORDER BY CASE WHEN `Des`.`name` REGEX 'act*' THEN 0 WHEN `Des`.`name` LIKE '%act' THEN 1 WHEN `Des`.`name` LIKE '%act%' THEN 2 ELSE 3 END, name LIMIT 10 What I am trying to achieve: actabc actzzz abcact zzzact abcactzzz act-act When I use this group by mechanism, it is showing hyphenated result first, which it

Django Admin: Custom ordering according to concatenated charfields of a related manytomany model

狂风中的少年 提交于 2019-12-24 03:43:34
问题 Here are my simplified models: class MasterFood(models.Model): class Meta: verbose_name_plural = 'Master Foods' FOOD_TYPE_CHOICES = ( ('F', 'Fats'), ('C', 'Carbohydrates'), ('P', 'Proteins'), ('O', 'Others'), ) food_name = models.CharField(max_length=255) food_type = models.CharField(max_length=20,choices=FOOD_TYPE_CHOICES) def __str__(self): return self.food_name class MasterMeal(models.Model): class Meta: verbose_name_plural = 'Master Meal Plan Meals' proteins = models.ManyToManyField(to=