Unpivot columns from another table [duplicate] - sql

This question already has an answer here:
unpivot with dynamic columns plus column names
(1 answer)
Closed 8 years ago.
I have used Unpivot to get data from a table I am trying to manipulate. I use this query to rearrange my columns to rows;
SELECT Id, ownername, ownervalue
FROM Contacts UNPIVOT (ownervalue FOR ownername IN (column1, column2, column3)) unpiv;
This works great. However I would prefer to get my column names from another table instead of hard-coding them in the query. Ideally i would like this, but it does not work;
SELECT Id, ownername, ownervalue
FROM Contacts UNPIVOT (ownervalue FOR ownername IN (SELECT * FROM ColumnsTable)) unpiv;
Is it possible to get my list of columns from another table like this?

As far as I know (please correct me if I'm wrong) it is not possible to use dynamic columnnames without the use of a dynamic query, which is executed with for example exec.
Take a look at the following question unpivot with dynamic columns plus column names

Related

Can't execute select query on some columns of table in oracle [duplicate]

This question already has answers here:
View based on apex collection
(2 answers)
Can't use column names in select query on sqlfiddle (oracle)
(3 answers)
Closed 2 months ago.
I have a table with 43 columns. When I execute "Select * from My_Table", it works; And shows data of all columns.
But if I perform "Select" query on some of the columns ( In my case, the first 29 columns of the table)
I receive an error that says "INVALID IDENTIFIER".
Other columns work just fine.
I can't perform "group by" or "order by" using these 29 columns either.
What do you think is the problem?
Any help is appreciated.
Some screenshots are attached for better understanding.
Looking at screenshots, it seems that you created table using mixed letter case and enclosed column names into double quotes. If that's so, well - that's usually bad idea in Oracle as you'll always have to identify columns that way: match letter case and use double quotes.
Therefore, that would be e.g.
select "Order_Id", "Customer_name", "DATA_DATE"
from your_table
Looks awful ... will you remember that customer name doesn't have initial capital letters, but e.g. trace number does?
For you own sake, if possible, drop that table and create a new one as
create table your_table
(order_id number,
customer_name varchar2(20),
trace_number number,
...
);
and reference such a table and columns using any letter case (as it'll work because - by default - Oracle stores names in uppercase (into data dictionary), but lets you reference them any way you want):
select order_id, CUSTOMER_name, TRacE_NumBER ...

Is it possible to avoid specifying a column list in a SQL Server CTE?

Is it possible to avoid specifying a column list in a SQL Server CTE?
I'd like to create a CTE from a table that has many columns so that the structure is identical. There probably is a way to accomplish this without relisting every column name.
I've tried (unsuccessfully):
with pay_cte as
(select * from payments)
select * from pay_cte
I'm encouraged in my quest by this statement in the msdn documentation:
The list of column names is optional only if distinct names for all resulting columns are supplied in the query definition.
https://msdn.microsoft.com/en-us/library/ms175972.aspx
Yes, assuming you mean that you don't have to name every column in the with cte(Col1, Col2) as section.
You can easily try this yourself with a very simple test query along the lines of:
with cte as
(
select *
from sys.tables
)
select *
from cte

T-SQL using how to use PIVOT function

I have the following table structure in SQL (using T-SQL):
sqlfiddle: http://sqlfiddle.com/#!6/e5edc/1/0
The data would look something like this:
Now I would like to transpose the structure so I get the following:
columns [01_amount] to [12_amount] and columns [01_active] to [12_active] as rows instead of columns
All rows of [initials] to be separate columns
Should look like this:
How would I go about this? The Pivot function looks rather complicated as I'm new to SQL. Can someone help me in the right direction? :-)
Ok you will need first to unpivot your data, which is done in cte. Then you will need to pivot again:
;with cte as(select initials, v, col from main
unpivot(v for col in([01_amount], [02_amount])) u)
select * from cte
pivot(max(v) for initials in([rw],[nb]))p
In unpivot part just add all 24 column names for amounts and active bits. In pivot part just add all possible values for initials.
But if you don't want to manually list all possible values for initials then you will need to make some dynamic query with unpivoting and pivoting.
Here is demo for 2 columns and you will easily expand it http://sqlfiddle.com/#!6/4cf36/2

How to unpivot sql table by 2 colums [duplicate]

This question already has answers here:
SQL Server : Columns to Rows
(7 answers)
Closed 8 years ago.
I have a single row of data (name, address, question_ID_1, Answer_1, question_ID_2, Answer_2, etc) and wish to unpivot this so that it reads:
row1: name, address, question_ID_1, Answer_1;
row2: name, address, question_ID_2, Answer_2;
row3: name, address, question_ID_3, Answer 3; etc.
I have managed to get a variation of this but not the final result. I have concatenated the Question and Answer fields and dont mind unpivoting using this -
so...
row1: name, address, QA1;
row2: name, address, QA2;
row3: name, address, QA3 etc.
I have a variable number of columns depending on the import too.
I am importing a flat file (using SSIS) into a SQLServer 2005 table,and then transforming the data into the chosen format using a stored procedure. Any code you can supply would be a great help.
You can use UNPIVOT to turn the columns into rows.
SELECT name, address, question, answer
FROM questions
UNPIVOT
(question for q in (question_ID_1, question_ID_2)) as q
UNPIVOT
(answer for a in (answer_1, answer_2)) as a
WHERE RIGHT(q,1) = RIGHT(a,1)
Here's a SqlFiddle to show this: http://sqlfiddle.com/#!3/e6919/3

Possible to exclude or reorder a column from `*`? [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
SQL exclude a column using SELECT * [except columnA] FROM tableA?
Is it possible to exclude a column from a select * from table statement with SQL Server?
I have a need for this and this is my only option other than parsing a raw SQL string to get out the required field names (I really don't want to do that).
Just to be bold. When the query is made I do not have access to the list of fields needed from the table but I do know which field I do not need. This is part of a complex multi-part query.
Surely there must be some way even if it's "hackish" such as using table variables or views
My other option is to reorder the columns. My problem is with ExecuteScalar SQL functions which get the first row and first column.
EDIT
I can't add an answer since this is now closed but the way I ended up doing it was like so:
;with results_cte as (
select (calculation) as calculated_column, * from table
)
select * into #temptable from results_cte
where calculated_column<10 /*or whatever*/
alter table #temptable
drop column calculated_column
select * from #temptable
drop table #temptable
Nope. You'll have to build your statement manually or just select *.
No.
Instead, you could check syscolumns to get all of the field names, or (perhaps) SELECT * and ignore that column.
If you use dynamic SQL, you can generate the query from metadata about the table or view (INFORMATION_SCHEMA.COLUMNS) and exclude columns that way. I do this a lot to generate triggers or views.
But there is nothing in the SQL language which supports this.
The best way to handle this would be to select * and then just not present the excluded column to your users in your frontend. As others have noted, SQL has no direct capability of doing an all-columns-except construct.