sql-order-by

Linq Query with SUM and ORDER BY

落花浮王杯 提交于 2019-11-30 11:43:53
I have a (C#) class called Hit with an ItemID (int) and a Score (int) property. I skip the rest of the details to keep it short. Now in my code, I have a huge List on which I need to do the following select (into a new List): I need to get the sum of all Hit.Score's for each individual Hit.ItemID, ordered by Score. So if I have the following items in the original list ItemID=3, Score=5 ItemID=1, Score=5 ItemID=2, Score=5 ItemID=3, Score=1 ItemID=1, Score=8 ItemID=2, Score=10 the resulting List should contain the following: ItemID=2, Score=15 ItemID=1, Score=13 ItemID=3, Score=6 Can somebody

SQL order by a column from another table

 ̄綄美尐妖づ 提交于 2019-11-30 11:09:55
I have 3 tables: people, groups and memberships. Memberships is a join table between people and groups, and have 3 columns: personId, groupId and description (text). I want to select entries from the memberships table depending on a groupId but sorting the result by the names of people associated to the found memberships (name is a column of people table) SELECT * FROM "memberships" WHERE ("memberships".groupId = 32) ORDER BY (?????) Is it possible to achieve this in one single query? Join to the people table and then order by the field that you want. SELECT m.* FROM "memberships" AS m JOIN

Sort by day of the week from Monday to Sunday

浪尽此生 提交于 2019-11-30 11:05:44
If I write select ename, to_char(hiredate,'fmDay') as "Day" order by "Day"; Then it sorts the result based on Day like; from Friday, then Monday and last Wednesday, like sorting by characters. But I want to sort it by day of the week; from Monday to Sunday. You're getting it in the order you are because you're ordering by a string (and this wouldn't work because you're not selecting from anything). You could order by the format model used to create the day of the week in numeric form, D , but as Sunday is 1 in this I would recommend using mod() to make this work. i.e. assuming the table create

How to Select and Order By columns not in Groupy By SQL statement - Oracle

北慕城南 提交于 2019-11-30 10:32:51
I have the following statement: SELECT IMPORTID,Region,RefObligor,SUM(NOTIONAL) AS SUM_NOTIONAL From Positions Where ID = :importID GROUP BY IMPORTID, Region,RefObligor Order BY IMPORTID, Region,RefObligor There exists some extra columns in table Positions that I want as output for "display data" but I don't want in the group by statement. These are Site, Desk Final output would have the following columns: IMPORTID,Region,Site,Desk,RefObligor,SUM(NOTIONAL) AS SUM_NOTIONAL Ideally I'd want the data sorted like: Order BY IMPORTID,Region,Site,Desk,RefObligor How to achieve this? It does not make

JPA/hibernate sorted collection @OrderBy vs @Sort

断了今生、忘了曾经 提交于 2019-11-30 10:29:47
问题 I would like to have a collection of child objects (here cat-kitten example) that are ordered. And keep their order on adding of new elements. @Entity public class Cat { @OneToMany(mappedBy = "cat", cascade = CascadeType.ALL) @OrderBy("name ASC") private List<Kitten> kittens; public void setKittens(List<Kitten> kittens) { this.kittens = kittens; } public List<Kitten> getKittens() { return kittens; } } When I do cat.getKittens.add(newKitten) the order by name will be broken. Is it possible to

ORDER BY AND CASE IN SQL SERVER

 ̄綄美尐妖づ 提交于 2019-11-30 10:02:35
I need to have an order by functionality inside a stored procedure. A value is posted to a webservice and based on that value I have to order the results in a certain way i.e. When ColName is posted order by ColName When ColName2 is posted order by ColName2 I was looking into using Case but I am getting an error: Incorrect syntax near '@version' ORDER BY CASE WHEN @OrderBy ='Seller (code)' THEN A_SNO WHEN @OrderBy ='Lot' THEN A_LOTNO WHEN @OrderBy ='Ring Type' THEN RN_NUM WHEN @OrderBy ='Aim Error Code' THEN AimRejectionCode ELSE A_SNO END DECLARE @version varchar(50) SET @version = (SELECT

How to use CASE function in ORDER BY?

柔情痞子 提交于 2019-11-30 09:41:47
问题 My friend asked a question a few times ago. Also there is a answer under that and it is good, but not for my case. The idea of that solution is joining the current table to itself. That seems expensive and not effective for me, Because in reality there is four join on these tables ( votes , favorites , comments , viewed ) in my query. Now I want to know, how can I do that using CASE function? Something like this: ... ORDER BY Type, CASE WHEN AcceptedAnswerId = Id THEN 1 ELSE 0, timestamp Or

How to dynamically order by certain entity properties in Entity Framework 7 (Core)

萝らか妹 提交于 2019-11-30 08:45:18
问题 I have a project where the front-end JavaScript specifies a list of columns to order by. Then in the back-end I have multi-layer application. Typical scenario Service layer (the service models' (DTO) properties match whatever the client-side wants to order by) Domain layer (it exposes repository interfaces to access persisted objects) ORM layer (it implements the repository and it uses Entity Framework 7 (a.k.a Entity Framework Core) to access a SQL Server database) Please note that System

Extremely slow PostgreSQL query with ORDER and LIMIT clauses

好久不见. 提交于 2019-11-30 07:54:39
I have a table, let's call it "foos", with almost 6 million records in it. I am running the following query: SELECT "foos".* FROM "foos" INNER JOIN "bars" ON "foos".bar_id = "bars".id WHERE (("bars".baz_id = 13266)) ORDER BY "foos"."id" DESC LIMIT 5 OFFSET 0; This query takes a very long time to run (Rails times out while running it). There is an index on all IDs in question. The curious part is, if I remove either the ORDER BY clause or the LIMIT clause, it runs almost instantaneously. I'm assuming that the presence of both ORDER BY and LIMIT are making PostgreSQL make some bad choices in

LEFT JOIN order and limit

我的梦境 提交于 2019-11-30 07:24:49
问题 This is my query: SELECT `p`.`name` AS 'postauthor', `a`.`name` AS 'authorname', `fr`.`pid`, `fp`.`post_topic` AS 'threadname', `fr`.`reason` FROM `z_forum_reports` `fr` LEFT JOIN `forums` `f` ON (`f`.`id` = `fr`.`pid`) LEFT JOIN `forums` `fp` ON (`f`.`first_post` = `fp`.`id`) LEFT JOIN `ps` `p` ON (`p`.`id` = `f`.`author_guid`) LEFT JOIN `ps` `a` ON (`a`.`account_id` = `fr`.`author`) My problem is this left join: SELECT `a`.`name`, `a`.`level` [..] LEFT JOIN `ps` `a` ON (`a`.`account_id` =