sql-order-by

Query rows using Order by A, if A values are the same, Order by B as the second standard

删除回忆录丶 提交于 2021-01-28 14:52:45
问题 I want to query db rows using two standards: A first, B second. That is: Order by A, if A values are the same, Order by B as the second standard How to write the sql? Example: query table: id | A | B _ _ _ _ _ _ 1 | 1 | 1 _ _ _ _ _ _ 2 | 2 | 2 _ _ _ _ _ _ 3 | 2 | 1 _ _ _ _ _ _ 4 | 3 | 1 query result: id 1 3 2 4 回答1: Order by is used to sort the result from a table in ASC | DESC based on one or more column names. It sorts by ASC in default. Example: Select * from Table1 order by A, B In this

sorting data in oracle

纵然是瞬间 提交于 2021-01-28 11:47:01
问题 I have a tricky scenario. Data in the table is as follows CustomerId Transaction Type Transaction Amount 1 Payment 100 1 payment 200 1 ReversePayment -100 1 ReversePayment -200 I have transnational table with transaction types being "Payment", "ReversePayment". There are multiple records for a customer with some records being Payment and some being ReversePayment. Is there a way to sort data as follows CustomerId Transaction Type Transaction Amount 1 Payment 100 1 ReversePayment -100 1

many-to-many relationship OrderBy - Laravel query builder

回眸只為那壹抹淺笑 提交于 2021-01-28 08:27:38
问题 I am trying to order products by thp price in options table the price can be in multi currency for that i add dolor_price calculated as code below. but the result was the following Error SQLSTATE[42S22]: Column not found: 1054 Unknown column 'options.dolor_price' in 'order clause' $products=Product::with(["options"=> function($option){ $option->where('name' ,'unique name' ); $option->selectSub(function ($q) { $rateEruToDolor =2; $rateAedToDolor =3; $q->selectRaw(' IF(currency=0,price * ?, IF

How to show rows in packs of three of two tables in MySQL

三世轮回 提交于 2021-01-28 06:20:32
问题 This is a follow-up question to How to show rows in packs of three in MySQL I have two tables: mytable ID Type Timestamp 1 A 101 2 A 102 3 B 103 4 B 104 5 A 105 6 B 106 7 A 107 8 A 108 9 B 109 10 A 110 11 B 111 12 B 112 mytable 2 ID2 Text Status 1 x 1 2 x 1 3 y 1 4 y 1 5 x 1 6 y 1 7 x 1 8 x 1 9 y 1 10 x 0 11 y 1 12 y 0 I want to show a result sorted by Type and Timestamp where every 3 rows the Type changes like this (rows with Status=0 are skipped): ID Type Timestamp Text 1 A 101 x 2 A 102 x

Ordering parent rows by date descending with child rows ordered independently beneath each

偶尔善良 提交于 2021-01-27 12:44:58
问题 This is a contrived version of my table schema to illustrate my problem: QuoteID, Details, DateCreated, ModelQuoteID Where QuoteID is the primary key and ModelQuoteID is a nullable foreign key back onto this table to represent a quote which has been modelled off another quote (and may have subsequently had its Details column etc changed). I need to return a list of quotes ordered by DateCreated descending with the exception of modelled quotes, which should sit beneath their parent quote,

Sort by most recent but keep together by another ID column

余生颓废 提交于 2021-01-27 07:22:51
问题 I am trying to get some sorting and keep together (not really grouping) working. In my sample data I would like to keep the DealerIDs together, sorted by IsPrimaryDealer DESC, but show the group (ok maybe it is grouping) of dealers by the ones with the most recent entry. Result set 2 is the closest, but Grant and his brother should be displayed as the first two rows, in that order. (Grant should be row 1, Grants Brother row 2 because Grants Brother was the most recently added) DECLARE @temp

Sort by most recent but keep together by another ID column

蓝咒 提交于 2021-01-27 07:22:24
问题 I am trying to get some sorting and keep together (not really grouping) working. In my sample data I would like to keep the DealerIDs together, sorted by IsPrimaryDealer DESC, but show the group (ok maybe it is grouping) of dealers by the ones with the most recent entry. Result set 2 is the closest, but Grant and his brother should be displayed as the first two rows, in that order. (Grant should be row 1, Grants Brother row 2 because Grants Brother was the most recently added) DECLARE @temp

Nested Set Query to retrieve all ancestors of each node

淺唱寂寞╮ 提交于 2021-01-20 18:24:30
问题 I have a MySQL query that I thought was working fine to retrieve all the ancestors of each node, starting from the top node, down to its immediate node. However when I added a 5th level to the nested set, it broke. Below are example tables, queries and SQL Fiddles: Four Level Nested Set: CREATE TABLE Tree (title varchar(20) PRIMARY KEY, `tree` int, `left` int, `right` int); INSERT Tree VALUES ("Food", 1, 1, 18), ('Fruit', 1, 2, 11), ('Red', 1, 3, 6), ('Cherry', 1, 4, 5), ('Yellow', 1, 7, 10),

SELECT DISTINCT with LEFT JOIN, ORDERed BY in t-SQL

穿精又带淫゛_ 提交于 2020-12-01 09:57:00
问题 I have the following table in SQL Server 2008: CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT) INSERT tbl VALUES (1, '05:00', '6:00', 1), (2, '05:00', '7:00', 1), (3, '05:01', '8:00', 1), (4, '05:00', '8:00', 1), (5, '05:00', '6:00', 2), (6, '05:00', '7:00', 2) that selects IDs of all records of the same type, with the same dtIn date, ordered by stOut in ascending order: SELECT DISTINCT tbl.id FROM tbl LEFT JOIN tbl AS t1 ON tbl.type = t1.type AND tbl.dtIn = t1.dtIn ORDER

SELECT DISTINCT with LEFT JOIN, ORDERed BY in t-SQL

馋奶兔 提交于 2020-12-01 09:54:14
问题 I have the following table in SQL Server 2008: CREATE TABLE tbl (ID INT, dtIn DATETIME2, dtOut DATETIME2, Type INT) INSERT tbl VALUES (1, '05:00', '6:00', 1), (2, '05:00', '7:00', 1), (3, '05:01', '8:00', 1), (4, '05:00', '8:00', 1), (5, '05:00', '6:00', 2), (6, '05:00', '7:00', 2) that selects IDs of all records of the same type, with the same dtIn date, ordered by stOut in ascending order: SELECT DISTINCT tbl.id FROM tbl LEFT JOIN tbl AS t1 ON tbl.type = t1.type AND tbl.dtIn = t1.dtIn ORDER