SQL ORDER BY command not working with GROUP BY

group by and order by together oracle
order by and group by in sql
order by and group by in same query
sql order by number
order by before group by
sql use order by with group by
order by after group by linq
sql sum group by order by

I am creating a new table that sums the values from the stock table for each item_id and then puts them into the new table. This works fine using this code:

create table total_stock as (
select item_id,
sum (stock) total_stock
from stock_tbl
group by item_id
);

which works fine, adding all the values from the stock table and putting them into the new table, but when I try to order it by item_id it doesn't work. Any help would be appreciated. The error given is a syntax error.

create table total_stock as (
select item_id,
sum (stock) total_stock
from stock_tbl
group by item_id
order by item_id
);

Rows in a database table are stored without any particular order. That is why specifying order by when filling a table doesn't work.

You have to order by when selecting rows from a table, not when entering rows into a table.

ORDER BY and GROUP BY in SQL, SQL defaults with ascending order. You would just implement it like so: ORDER BY username DESC. GROUP BY SQL Keyword. Example. SELECT * FROM  If you have no GROUP BY clause, then the statement considers the entire table as a group, and the ORDER BY clause sorts all its rows according to the column (or columns) that the ORDER BY clause specifies.


Technically, you can order the rows in a table in most databases. You do this by creating a clustered index on the keys used for the ordering.

Guess what?

select t.*
from t

still doesn't return the rows in order, even in tables that have a clustered index. *At least, there is no guarantee. *SQL result sets are unordered unless an ORDER BY is explicitly included for the outermost SELECT. The only guarantee on the ordering of a result set is to use order by. In your case, that would be:

select item_id, sum(stock) as total_stock
from stock_tbl
group by item_id
order by item_id;

or:

select ts.*
from total_stock ts
order by item_id;

By the way, using a temporary table for this is not recommended unless you have a specific need to materialize the result set.

SQL: How do I use group by and order by together?, "Group By" and "Order By" can be used together in SQL as shown in the example​: SELECT deptno, sum (sal + comm) Why does this sql query not return my expected results? In SQL, what exactly is a 'CROSS JOIN' and how does it work? SELECT Country, Region, SUM (Sales) AS TotalSales FROM Sales GROUP BY ROLLUP (Country, Region) UNION ALL SELECT Country, Region, SUM (Sales) AS TotalSales FROM Sales GROUP BY CUBE (Country, Region); SQL does not consolidate duplicate groups generated for a GROUPING SETS list.


Updated You can use Common Table Expression and then use this data to insert into your table, as

with CTE as (
select item_id, sum(stock) total stock
from stock_table
) Insert into total_stock
select * from cte group by item_id order by item_id

This should solve your problem

MySQL Bugs: #32202: ORDER BY not working with GROUP BY, Bug #32202, ORDER BY not working with GROUP BY Additionaly, the code discarded ASC/DESC modifiers from ORDER BY even if its columns SQL mode set in MySQL. mysql> select * from a group by id order by sorter;  The SQL GROUP BY Statement The GROUP BY statement groups rows that have the same values into summary rows, like "find the number of customers in each country". The GROUP BY statement is often used with aggregate functions (COUNT, MAX, MIN, SUM, AVG) to group the result-set by one or more columns.


ORDER BY in MySQL: DESC & ASC, Both DESC and ASC work in conjunction with the ORDER BY keyword. [​WHERE condition | GROUP BY `field_name(s)` HAVING condition] ORDER BY `​field_name(s)` [ASC | DESC]; Note: NULL values means no values (not zero or empty string) . Oracle PL/SQL BULK COLLECT: FORALL Example. The GROUP BY clause is a SQL command that is used to group rows that have the same values. The GROUP BY clause is used in the SELECT statement .Optionally it is used in conjunction with aggregate functions to produce summary reports from the database.


SQL ORDER BY, The ORDER BY command sorts the result set in ascending order by default. To sort the records in descending order, use the DESC keyword. The following SQL​  The SQL GROUP BY clause is used in collaboration with the SELECT statement to arrange identical data into groups. This GROUP BY clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY clause. Syntax. The basic syntax of a GROUP BY clause is shown in the following code block.


(My)SQL mistakes. Do you use GROUP BY correctly?, Often I see a SQL problem solved incorrectly and I do not mean FROM bets GROUP BY user_id ORDER BY bet_date DESC LIMIT 3; Do You Really Understand SQL’s GROUP BY and HAVING clauses? Posted on December 4, 2014 September 29, 2016 by lukaseder There are some things in SQL that we simply take for granted without thinking about them properly.