How to unpivot sql table by 2 colums [duplicate] - sql

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

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 ...

Unpivot columns from another table [duplicate]

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

Split content of field into 2 fields [duplicate]

This question already has answers here:
sql create a field from a field
(4 answers)
Closed 9 years ago.
I have to run a query in Access sql or using the Query Wizard to split the data of field into 2.
The field has data such as 1234 ave willie and has email addresses such as haha#yahoo.com .
I need to put 1234 ave willie in a new field named St address and haha#yahoo.com in Email.
Can someone help? Is there a wildcard operator like LIKE etc?
You should look into using LIKE to determine which field is an email and which field is an address. Something like this would work:
SELECT Field, 'Email' FieldType
FROM YourTable
WHERE Field Like '*#*'
UNION ALL
SELECT Field, 'Address' FieldType
FROM YourTable
WHERE Field Not Like '*#*'
You mention you need to split these in 2 separate fields -- depends on the table structure. But assuming you add 2 new fields to the original table, then something like this should work:
UPDATE YourTable SET Email = Field WHERE Field Like '*#*'
UPDATE YourTable SET Address = Field WHERE Field Not Like '*#*'
You can find a better algorithm to search by email address -- this is just an example. But assuming any field with an # symbol is an email, then the above will work fine. Here's an SO post with SQL Email validation that can help get you started:
Sql script to find invalid email addresses

comma separated value in sql [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Concat all column values in sql
I have a table
ID | Name
1 | X
2 | y
3 | z
I have to show values in column Name as comma separated i.e x,y,z.
One way I can do is looping the values of column "Name" and displaying as comma separated.
Is there is a other way to do it.Please help.
Even though this is a duplicate of several other questions, I'd like to give an answer, because the easiest way to do this has changed recently. Oracle has provided the very nifty LISTAGG function with the following syntax:
SELECT
LISTAGG(name, ',') WITHIN GROUP (ORDER BY name)
FROM
my_table;
LISTAGG is available since Oracle 11.2.

Query to get column value comma separated [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicates:
Is there an Oracle SQL query that aggregates multiple rows into one row?
Agregate rows in Oracle SQL statement
I am working with Oracle 10g. I want to have a comma separated String from a column in a table.
e.g.
Table : Customer
Columns: id and name
Data:
id-------name
1-------John
2-------Galt
3-------Howard
4-------Roark
Output of query should be Jon,Galt,Howard,Roark
Ok, got it, all I wanted was this:
SELECT WM_CONCAT(NAME) FROM CUSTOMER;
Marking all comments as +1. Thanks guys.