I have the following table1:
ITEM1|ITEM2
COOKIE|CHAIR
PIE|TABLE
PIE APPLE|PENCIL
BANANA CAKE|PEN
expected output results:
ITEM_ALL
COOKIE
PIE
PIE APPLE
BANANA CAKE
CHAIR
TABLE
PENCIL
PEN
Can someone help with the query, for transpose column to rows? Thank you before.
You can use union all:
select item1 as item_all from table1
union all
select item2 as item from table1;
If you want to remove duplicates, then use union rather than union all.
Related
i have the data in this format in an amazon redshift database:
id
answer
1
house
1
apple
1
moon
1
money
2
123
2
xyz
2
abc
and what i am looking for would be:
id
answer
1
house, apple, moon, money
2
123, xyz, abc
any idea? the thing is that i cannot hard code the answers as they will be variable, so preferably a solution that would simply scoop the answers for each id's row and put them together separated by a delimiter.
you can use aggregate function listagg:
select id , listagg(answer,',')
from table
group by id
You can use string_agg(concat(answer,''),',') with group by so it will be like that:
select id , string_agg(concat(answer,''),',') as answer
from table
group by id
tested here
Edit:
you don't need concatenate, you can just use string_agg(answer,',')
I have a sql qustion. its suppose to be simple but i cant figure it out.
I have 2 tables:
**customer1 -**
id name
1 aaa
2 bbb
3 ccc
4 ddd
**customer2**-
id name
1 aaa
2 bbb
5 eee
6 fff
I need to get ALL the customers in both tables.
i need to query in a way that if a customer is on both tables I will see him twice in the results (e.g aaa, bbb), and all the rest only one.
Thanks a lot.
select id, name from customer1
union all
select id, name from customer2
Pay attention to UNION and UNION ALL , with the first one you will not get duplicate , with the second one you will get even duplicate as result of your query.
Let's talk about the performance :
A UNION is highly optimized and really fast, except in cases where one query finishes long before the other, and you have to wait to get the whole result set.
When you don't mind about duplicates , UNION ALL will be faster.
I would have put this as a comment to : Raghavendra Kedlaya post; but it wouldn't let me.
I like to know from whence the data came when using union all..
SELECT id, name, 'Customer1' as src
FROM customer1
UNION ALL
SELECT id, name, 'Customer2' as src
FROM customer2
So I've put together a union query in access, but it displays in this fashion.
Name Column1 Column2
John 1 0
Jim 2 0
Mike 3 0
John 0 2
Jim 0 1
Mike 0 3
I would like for it to display like this:
Name Column1 Column2
John 1 2
Jim 2 1
Mike 3 3
In my select statement I'm setting column2 to 0 in the first portion of the union statement, and setting column1 to 0 in the second part. Now I realize that's why I'm getting the 0s in the end result, but is there a way to achieve my desired display with a union or do I need something more complex.
EDIT: The reason I set those columns to 0, was to avoid it asking me to enter a value when it could not find a value for column1 or 2.
PS. this is not my actual query, but rather just a quick example I was able to throw on here for question purposes.
select [Name], max([Column1]) as col1, max([Column2]) as col2
from ( put union query here ) as x
group by [Name]
edit -- didn't notice that your data was a query result and not a table. Put your current query where indicated in the from clause (as an inline view)
I have an issue here that is arising from poor data formatting (not on my behalf). I had a large CSV file downloaded from an external entity with nation wide data - it has about 5,000,000+ rows so that its too large of a file to open, let alone manually manipulate the data. I did get it uploaded to our SQL database, but getting the data into a usable format is difficult; each row has 10 different category codes, and can have multiple codes in each category. Unfortunately, they added new columns to handle this instead of adding a new row. Its tough to describe without an example:
ID A_Code1 A_Code2 A_Code3 B_Code1 B_Code2 B_Code3
1 123 765 654 qwe asd zxc
2 987 345 567 poi lkj mnb
and this is what I need:
ID A_Code B_Code
1 123 qwe
1 765 asd
1 654 zxc
2 987 poi
2 345 lkj
2 567 mnb
The way it is set up now makes querying nearly impossible as there are about 10 different types of on each row, and there are 10 columns for each code type. This means I have to query 100 different columns when I should only have to query 10.
If somebody knows a way to do this, it would be greatly appreciated. I have not been able to find anything like this so far, so I am getting desperate!
Thank you!
You need to unpivot the multiple columns of data into multiple rows, depending on your version of SQL Server there are several ways to get the result.
You can use CROSS APPLY and UNION ALL if using SQL Server 2005+:
select id, A_Code, B_Code
from yourtable
cross apply
(
select A_Code1, B_Code1 union all
select A_Code2, B_Code2 union all
select A_Code3, B_Code3
) c (A_Code, B_Code);
See SQL Fiddle with Demo.
You can also use CROSS APPLY with VALUES if using SQL Server 2008+:
select id, A_Code, B_Code
from yourtable
cross apply
(
values
(A_Code1, B_Code1),
(A_Code2, B_Code2),
(A_Code3, B_Code3)
) c (A_Code, B_Code);
See SQL Fiddle with Demo.
This allows you to convert the columns into rows in pairs - meaning A_Code1 and B_Code1 will be matched in the final result.
You could also use a UNION ALL:
select id, A_Code = A_Code1, B_Code = B_Code1
from yourtable
union all
select id, A_Code = A_Code2, B_Code = B_Code2
from yourtable
union all
select id, A_Code = A_Code3, B_Code = B_Code3
from yourtable ;
See SQL Fiddle with Demo
I have an execl datasheet with data looking like this
id desc part no 1 Qty 1 part no 2 Qty 2 part no 3 Qty 3
1 PartsName 382A012-3-0 3 382A023-3 3 382A012-25 3
And need it to look like this
id desc part no Qty
1 PartsName 382A012-3-0 3
1 PartsName 382A023-3/42-0 3
1 PartsName 382A012-25/86-0 3
This from a SQL Table so I could do it in SQL if that makes it easier
Anybody any suggestions as how to best to sort this?
Simply make a UNION in the SQL
SELECT id, desc, partNo, qty FROM parts
UNION SELECT id, desc, partNo2 as partNo, qty2 as qty FROM parts
UNION SELECT id, desc, partNo3 as partNo, qty3 as qty FROM parts
ORDER BY id
If you don´t have the option of using SQL and need to use Excel. You can use the TRANSPOSE function. It is an array type function so you need to use the {}. If you haven´t used it before I recommend reading the help first. I however don´t think you can use transpose to get it exactly as you describe it. The id and desc column have to handled separately.
You need to add next formulas on a new sheet:
column A=MOD(ROW()+1,3)
it's like a skeleton :)
First row is headers
column B
=IF($A2=0,OFFSET(Sheet1!$A$1,COUNTIF($A$2:$A2,0),COLUMN()-2),B1)
autofil col C with it
column D
=OFFSET(Sheet1!$A$1,COUNTIF($A$2:$A2,0),COLUMN()+CHOOSE($A2+1,-2,0,2))
autofil col E with it
one more - your datasheet is "sheet1"