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

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.

Related

Change Display order of Columns in SQL [duplicate]

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.

Get max ID row from individual rows in SQK Server with only 2 columns [duplicate]

This question already has answers here:
get max ID row from individual rows in sql server
(3 answers)
Closed 2 years ago.
I have data like below with multiple rows with same data which can be identified by ID.
A 2784762
A 2788523
A 2789063
B 3423390
B 3423396
I need the data like below. Get only individual max ID value for every set of duplicate records with can be done by taking individual max ID
A 2789063
B 3423396
Can you help me with this?
Use MAX function to achieve this:
SELECT COL1,MAX(COL2)
FROM TABLE
GROUP BY COL1
Instead of COL1,COL2 use your Table Column Names and Table use your Table Name.

What is the difference in selecting all the columns by * and by specifying all column names in SQL query to retrieve data? [duplicate]

This question already has answers here:
Why is SELECT * considered harmful?
(16 answers)
Closed 4 years ago.
Suppose I have a table with columns id, name, contact.
What difference is there by getting the data with
SQL query #1:
SELECT *
FROM table
and SQL query #2:
SELECT id, name, contact
FROM table
SELECT * FROM table - its will select all column even if those are useful or not.
if you selecting all the column than some memory overhead and time consuming.
For example , suppose you are selecting all the column and storing in some java object then this is not useful instead of this select those column which is useful for your operation.

Is there SQL Query for excluding a single column? [duplicate]

This question already has answers here:
Exclude a column using SELECT * [except columnA] FROM tableA?
(47 answers)
Closed 5 years ago.
I was reading SQL queries and got stuck with a doubt. Is there any SQL query to exclude a single column while using select statement?
For example, I have a table which contains data about student (s_name, address, email_id, ..... many more).
I want to select all the columns except email_id.
I don't think there is a way to do it directly in a Select statement. But you can use this alternative:
How to select all the columns of a table except one column?

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()