New answers tagged order-by
Have a look at below example DROP TABLE IF EXISTS products; create table products(pname CHAR(30),pdescription CHAR(30),price DECIMAL(10,2),manufacturer CHAR(30)); INSERT INTO products VALUES ('Toys','These are toys',15.25,'ABC'), ('Dolls','These are Dolls',35.25,'PQR'), ('DustPan','These are DustPan',75.25,'AZD'), ('Doors','These are ...
You can create a new field in the SELECT statement to specify that text like 'D%' is the preferred sort order then order by the new field. Example: SELECT pname , pdescription , price , CASE WHEN pname LIKE 'D%' THEN 1 ELSE 2 END AS sortpreference FROM products WHERE manufacturer = 3 ORDER BY sortpreference;
Because the where clause is applied first, and the order by is applied to the results, you need to use a subselect as follows: select * from ( select * from table order by timestamp desc ) where rownum <= 5;
Why don't you try: `Select * From my_table Where Row_number<6 Order by Timestamp Desc: ` I think it will work
Try this, using a CTE as an intermediate result set: WITH Randomized AS( SELECT Books.BookID, Books.Authors, Books.ShortTitle, NEWID() as 'ID' FROM Books inner join Publishers on Books.PublisherID = Publishers.PublisherID ) SELECT -- TOP 4 BookID, Authors, ShortTitle, ...
If you want to rewrite the condition slightly you could do something like this: with id_list (id, sort_order) as ( values (13589, 1), (16674, 2), (13588, 3) ) select u.id from users u join id_list il on il.id = u.id order by il.sort_order; You can also put the values clause directly into the join: select u.id from users u join ...
WITH ORDINALITY in Postgres 9.4+ You will be most interested in the new feature WITH ORDINALITY in the upcoming version 9.4 (devel status as of writing). Per documentation: When a function in the FROM clause is suffixed by WITH ORDINALITY, a bigint column is appended to the output which starts from 1 and increments by 1 for each row of the ...
Are you talking about the error as shown in this SQL Fiddle? When you add semi-colons, do you get results from the first query and none from the second like in this SQL Fiddle? If you are expecting results like in this SQL Fiddle, you will likely need to use a different client. The difference is the batch terminator.
Top 50 recent answers are included