sql-order-by

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 ==============================================

postgres: get top n occurrences of a value within each group

随声附和 提交于 2019-12-23 19:46:48
问题 I have a simple table like this: user letter -------------- 1 A 1 A 1 B 1 B 1 B 1 C 2 A 2 B 2 B 2 C 2 C 2 C I want to get the top 2 occurrences of 'letter' per user, like so user letter rank(within user group) -------------------- 1 B 1 1 A 2 2 C 1 2 B 2 or even better: collapsed into columns user 1st-most-occurrence 2nd-most-occurrence 1 B A 2 C B How can I accomplish this in postgres? 回答1: with cte as ( select t.user_id, t.letter, row_number() over(partition by t.user_id order by count(*)

Concatenate the result of an ordered String_Split in a variable

倖福魔咒の 提交于 2019-12-23 18:30:52
问题 In a SqlServer database I use, the database name is something like StackExchange.Audio.Meta , or StackExchange.Audio or StackOverflow . By sheer luck this is also the url for a website. I only need split it on the dots and reverse it: meta.audio.stackexchange . Adding http:// and .com and I'm done. Obviously Stackoverflow doesn't need any reversing. Using the SqlServer 2016 string_split function I can easy split and reorder its result: select value from string_split(db_name(),'.') order by

Order rows within groups in PostgreSQL

早过忘川 提交于 2019-12-23 16:34:38
问题 I have the following query: SELECT routeid, 'SRID=4326;LINESTRING(' || string_agg(lon || ' ' || lat, ',') || ')' AS the_geom FROM route_table WHERE observation_time BETWEEN '2012-09-12 10:00:00' AND '2012-09-12 10:15:00' GROUP BY routeid HAVING COUNT(lon) > 1 ORDER BY observation_time ASC; The goal of this query is to pull all lon/lat values from the route_table (which consists of a routeid, observation_time, lat, and lon columns), group them by routeid, and have them sorted within each group

Complex sorting based on next and previous records in SQL

非 Y 不嫁゛ 提交于 2019-12-23 14:42:43
问题 This is a follow-up question on Sorting based on next and previous records in SQL But now it gets a little more complex, for example: If any letter of 1 matches any letter of 2, I want to change the ordering, so that the letter matches with the following record. If no matches are found the normal ordering by letter should be done. The IDs are potentially not succeeding and the records are not persé in the correct order. [SQLFiddle Demo] [Create script and SQL Fiddle demo] create table Parent

Complex sorting based on next and previous records in SQL

江枫思渺然 提交于 2019-12-23 14:39:34
问题 This is a follow-up question on Sorting based on next and previous records in SQL But now it gets a little more complex, for example: If any letter of 1 matches any letter of 2, I want to change the ordering, so that the letter matches with the following record. If no matches are found the normal ordering by letter should be done. The IDs are potentially not succeeding and the records are not persé in the correct order. [SQLFiddle Demo] [Create script and SQL Fiddle demo] create table Parent

How can I order selected rows alternatively

六月ゝ 毕业季﹏ 提交于 2019-12-23 14:24:58
问题 Here is my query: SELECT pr1.id AS user_id, pr1.title AS user_name, pr2.id AS liker_id, pr2.title AS liker_name, x.which AS which_table, x.cnt AS total FROM ( SELECT rid, rootid, which, COUNT(*) AS cnt FROM ( SELECT rid, rootid, 'vote' which FROM p_likes UNION ALL SELECT rid, rootid, 'comment' which FROM p_comments UNION ALL SELECT rid, rootid, 'friend' which FROM relations ) y WHERE y.rootid = 1246 AND y.rootid <> y.rid GROUP BY y.rid, y.rootid, y.which ) x INNER JOIN pagesroot pr1 on x

ORDER BY in VIEW returns different results SQL

巧了我就是萌 提交于 2019-12-23 14:01:30
问题 This is my viww: CREATE VIEW [STD_USER].[view_TransInvoice] AS SELECT TOP 999999 Customernr, Referensnr,'2' as a, InvoiceRowData, FileHead FROM [STD_USER].[Transexport] WHERE InvoiceRowData IS NOT NULL UNION SELECT TOP 999999 Customernr, Referensnr,'1' AS a , InvoiceHead , FileHead FROM [STD_USER].[Transexport] WHERE InvoiceHead IS NOT NULL UNION SELECT TOP 999999 Customernr, Referensnr,'3' AS a , InvoiceFoot , FileHead from [STD_USER].[Transexport] WHERE InvoiceFoot IS NOT NULL ORDER BY

Entity Framework ORDER BY issue

青春壹個敷衍的年華 提交于 2019-12-23 13:15:35
问题 I'm attempting to build my first MVC 4 application using the entity framework. All I'm looking for is to create a drop down list with the value and text of each option set to the same value. This works, right up until I throw in the GroupBy() . Create.cshtml @Html.DropDownList("CustomerName",(SelectList)ViewData["companies"]); ticketController.cs ViewBag.companies = new SelectList(oc_db.company.Where(c => c.status == "ACTIVE") .OrderBy(c => c.name_1) .GroupBy(c=>c.name_1) , "name_1", "name_1"

H2 database collation strength: what to choose?

穿精又带淫゛_ 提交于 2019-12-23 12:49:53
问题 After a lot of reading and experimentation, it seems like I want PRIMARY strength for searching, but TERTIARY or IDENTICAL for ordering. Main question: Is that possible to achieve with H2 (or any other DB)? Secondary question: Am I the only one here or would any of you also like the above combination? Some confirmation would be helpful for my sanity. Background: It seems like the collation can only be set at the very beginning when creating the database. So I want to make sure to pick the