How can I add a column from a select query but the value from the new column will be the row count of the select query for example.
select quantity from menu;
and returns like this
+--------+
|quantity|
+--------+
| 50 |
| 32 |
| 23 |
+--------+
but I want somthing like this
+----------+--------+
|new column|quantity|
+----------+--------+
| 1 | 50 |
| 2 | 32 |
| 3 | 23 |
+----------+--------+
the new column should start from 1 and end from row count of the select query statement. Any answer would help Thanks
SQL Server CREATE INDEX statement In this syntax: First, specify the name of the index after the CREATE NONCLUSTERED INDEX clause. Note that the NONCLUSTERED keyword is optional. Second, specify the table name on which you want to create the index and a list of columns of that table as the index key columns.
To create indexes, use the CREATE INDEX command: CREATE INDEX index_name ON table_name (column_name); You can an index on multiple columns.
Since you can access the latest version of MySQL, we can simply use the Row_Number()
functionality:
SELECT
ROW_NUMBER() OVER () AS new_column,
quantity
FROM menu;
You can use:
select row_number() over (order by quantity desc) as col1, quantity
from menu;
This assumes that you want the rows enumerated by quantity in descending order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With