Change Display order of Columns in SQL [duplicate] - sql

This question already has answers here:
How do I alter the position of a column in a PostgreSQL database table?
(10 answers)
Change column order in table of postgres
(1 answer)
How to change the position of column in postgresql without dumping
(1 answer)
Closed 2 years ago.
When a new column is added to a current table, this appears in the last column. eg:
added AGE columns to Students table
SELECT * FROM Students; "would show the AGE column right at the end.
Question: is it possible to change the order of these columns when such commands are executed?

If you have an existing table, you could create a VIEW with the columns reorganized, then of course, you could also type out the columns. You could also create a new table.

Related

update two join table in one statement [duplicate]

This question already has answers here:
How to update two tables in one statement in SQL Server 2005?
(10 answers)
Closed 2 years ago.
I have a problem. I have a 2 table which is table orders and table delivery. table order has column order_recieved. by default NULL. and table delivery has column delivery_status with value order shipped. now, if customer click a button to update order_received, the column will change NULL value to Recieved value. and my problem is how column delivery_status will also change to order shipped value to complete value? the PK table orders is order_no and FK table delivery is orderFK.
Can someone help me how to update the value?
You can only update one table per update statement. I'd recommend sticking them both in the same stored procedure so that you can make one database call and have both values updated.

How to divide all columns of a numeric table by the last column in SQL Server except ID column and the last column as divisor? [duplicate]

This question already has an answer here:
How to divide all columns of a numeric table by the last column in SQL Server? [closed]
(1 answer)
Closed 2 years ago.
In SQL Server I am going to divide all columns of a table by the last column in that ID column and the last column itself are not included.
Suppose the data is numeric.
The simple way is to use the following code consider with 4 columns and one ID column:
update Table1
set col1 = col1/col4, col2 = col2/col4, col2 = col2/col4
where id = ...
and this must be repeated for updating other records too.
The problem is that we have no idea how many columns are there. So, it seems we need some variables and and loops to detect columns and update them for each record.
and after updating
I need a code to do it for any table with numeric values.

Columns names in new table from query. UNPIVOT [duplicate]

This question already has answers here:
SQL Server dynamic PIVOT query?
(9 answers)
SQL dynamic unpivot table as function
(1 answer)
Closed 2 years ago.
I have table:
I want use UNPIVOT to create table, where CategoryNames rows are columns names.
So, now I can just write this as
SELECT Year, Seafood, Meat/Poultry, [...]
FROM [...] UNPIVOT(...) AS Example
but I don't want write every row manually as column name, so is any option to use
SELECT CategoryName
FROM Pods
query result to create them automatically.
For this example its only 8 row, but with bigger count this could be very helpful.

How to select rows from Oracle as recently inserted row should be at first? (No Identity column present) [duplicate]

This question already has answers here:
Default row ordering for select query in oracle
(8 answers)
Closed 7 years ago.
Hi allWhile working on Oracle I came across a situation where I need to select the rows from a table in which the recently inserted row should be at the top of my selection.
But here in my table I am not using any Identity column. I know how to do the same when there is an Identity column present, but is there any way to do this without Identity column?
Unless you have a sequential ID or Timestamp or some other field you can ORDER BY, Oracle provides no guarantee of order and you should not rely on it.

SQL for MS Access how to pick random row from a table? [duplicate]

This question already has answers here:
Randomly Select a Row with SQL in Access
(4 answers)
Closed 7 years ago.
I'm trying to make a Query from my Table "Questions", this table has the following fields: id(auto number), Question(text) ; Category(text), difficulty (number)
The query I'm trying to make must return one random row for every difficulty (1-5), for a given Category.
In SQL you can do something like this:
SELECT TOP 1 * FROM Questions ORDER BY NEWID()