Home » SQL & PL/SQL » SQL & PL/SQL » Column to Row (ORACLE PL SQL)
Column to Row [message #649391] Thu, 24 March 2016 01:59 Go to next message
reach2nm
Messages: 5
Registered: March 2016
Junior Member
Hi,

Please help me to get the below result in oracle

1. String 'WELCOME' needs to be display like below using oracle function
W
E
L
C
O
M
E

2. Create table Test(col1 char(1),col2 number);

Table created

insert into test values ('A',2);
insert into test values ('B',3);

select * from test;

Col1 Col2
A 2
B 3

I want to write a query which will display the result as below

Rows will be display based on col2 values.

Col1 Col2
A 2
A 2
B 3
B 3
B 3

3. I want to generate a 10 sequence number from an empty table without using sequence.

create table emp_test(id number,ename varchar2(30));





Re: Column to Row [message #649392 is a reply to message #649391] Thu, 24 March 2016 02:03 Go to previous messageGo to next message
John Watson
Messages: 8938
Registered: January 2010
Location: Global Village
Senior Member
Welcome to the forum. Please read our OraFAQ Forum Guide and How to use [code] tags and make your code easier to read

For school homework questions like this, you need to show th SQL that you have already tried and explain what yu are stuck on.
Re: Column to Row [message #649394 is a reply to message #649392] Thu, 24 March 2016 02:11 Go to previous messageGo to next message
reach2nm
Messages: 5
Registered: March 2016
Junior Member
I have not stuck on any work related to above questions.
Provide your solution to get the desired result if any.
Re: Column to Row [message #649395 is a reply to message #649394] Thu, 24 March 2016 02:13 Go to previous messageGo to next message
John Watson
Messages: 8938
Registered: January 2010
Location: Global Village
Senior Member
Sorry, I don't understand. Does this
Quote:
I have not stuck on any work related to above questions
mean that you are not stuck? That you have finished the homework? In rthat case, you can post your solutions as examples that will help other people.
Re: Column to Row [message #649396 is a reply to message #649391] Thu, 24 March 2016 02:21 Go to previous messageGo to next message
Michel Cadot
Messages: 68665
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

In addition to John's link, always post your Oracle version, with 4 decimals, as solution depends on it.
With any SQL or PL/SQL question, please, Post a working Test case: create table (including all constraints) and insert statements along with the result you want with these data then we will be able work with your table and data. Explain with words and sentences the rules that lead to this result.

Hint: read row generator.

Re: Column to Row [message #649397 is a reply to message #649396] Thu, 24 March 2016 02:36 Go to previous messageGo to next message
reach2nm
Messages: 5
Registered: March 2016
Junior Member
John,Michel,

We are posting our quires in forum to find the solution not to disturb others. I m still finding the solution for this.
If you know the solution then provide otherwise no need to reply.

i am using ORACLE PL/SQL Release 11.2.0.3.0
Re: Column to Row [message #649398 is a reply to message #649397] Thu, 24 March 2016 02:40 Go to previous messageGo to next message
Michel Cadot
Messages: 68665
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

If you don't want to learn and just want to cheat you haven't reached the good site for you.
Here, we help you to learn but will not give you basic solutions.
Note that the last link I gave contains the answer to your questions, so don't be so lazy and make the simple effort to read it.

Re: Column to Row [message #649399 is a reply to message #649398] Thu, 24 March 2016 03:54 Go to previous messageGo to next message
reach2nm
Messages: 5
Registered: March 2016
Junior Member
I have gone through the link. non of topics is providing any solution or hint to get the desire result.
Re: Column to Row [message #649400 is a reply to message #649399] Thu, 24 March 2016 04:03 Go to previous messageGo to next message
John Watson
Messages: 8938
Registered: January 2010
Location: Global Village
Senior Member
Take your first homework question:
Quote:
1. String 'WELCOME' needs to be display like below using oracle function
W
E
L
C
O
M
E
What have you done so far? Nothing?? I would begin by writing a CREATE TABLE statement and then one INSERT statement. That will set up the problem. Then you need to write a SELECT statement. Start with a simple one, and then look up the various functions and constructs that can convert one row into seven.
Re: Column to Row [message #649401 is a reply to message #649400] Thu, 24 March 2016 04:53 Go to previous messageGo to next message
reach2nm
Messages: 5
Registered: March 2016
Junior Member
First check this problem which is not required any table creation.

1. String 'WELCOME' needs to be display like below using oracle function

W
E
L
C
O
M
E

Thanks in advance
Re: Column to Row [message #649403 is a reply to message #649401] Thu, 24 March 2016 05:02 Go to previous messageGo to next message
John Watson
Messages: 8938
Registered: January 2010
Location: Global Village
Senior Member
Sorry, man. I'm not doing your homework. Do bit of work yourself first, and if you get stuck, explain where.

Frankly, I find your attitude odd. I would not employ you until you show a bit more willingness to work.
Re: Column to Row [message #649404 is a reply to message #649399] Thu, 24 March 2016 06:17 Go to previous messageGo to next message
Michel Cadot
Messages: 68665
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
reach2nm wrote on Thu, 24 March 2016 09:54
I have gone through the link. non of topics is providing any solution or hint to get the desire result.


Read it again and read it ENTIRELY as it does.
Of course, you have to, at least, try to understand your problem and what's inside the link.

Re: Column to Row [message #649409 is a reply to message #649404] Thu, 24 March 2016 10:13 Go to previous messageGo to next message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
We have never and we will never spoon feed someone their homework. If you are stuck on a homework problem and you need a hint, then show everything that you have already done and we will give you hints on how to do it. If we just give you the answer, you will never learn how to program and even worse you will not learn how to search for help.
Re: Column to Row [message #649422 is a reply to message #649409] Fri, 25 March 2016 04:18 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
Bill B wrote on Thu, 24 March 2016 15:13
We have never and we will never spoon feed someone their homework. If you are stuck on a homework problem and you need a hint, then show everything that you have already done and we will give you hints on how to do it. If we just give you the answer, you will never learn how to program and even worse you will not learn how to search for help.

But what you WILL do is end up getting a job for which you are not qualified and are not capable of doing properly. Because of this, you will reduce the overall perceived standard of that position thereby reducing its value, subsequently reducing the pay grade associated with it. This will necessarily reduce the costs of the hiring company in terms of wages but will significantly increase their costs in terms of development time (far outstripping the savings in wages). In addition, the quality of work will be lower and the time spent fixing errors will increase. Data errors will cause failures across the business and that, coupled with the increased costs, will cause the decline of that company, throwing many people out of work and by creating a glut in the supply of labour, further devaluing the job and it's standard rates of pay. All this will cause a reduction in the finances available to the market (fewer people working, less money to spend on consumables, lower demand, leads to reduced supply levels, leads to lower profits which leads to lower wages). This race to the bottom will lead to the total collapse of the world's economy and leave us all scratching a living by selling our few possessions and the occasional pretty stone that we find in our hovel, fighting over scraps and generally being uncivilised.


SO, with all that in mind, rather than causing the collapse of civilisation as we know it, how about you put a bit of effort in and try to write the solution to your problem yourself, in doing so, identify what issues you have and come back and ask questions, we like questions (rather than demands that we write code for you). We will help you understand those issues and we will help you find out how to develop the appropriate code to solve your issue.

Save civilisation reach2nm, do the work Smile
Re: Column to Row [message #649449 is a reply to message #649422] Sat, 26 March 2016 00:57 Go to previous messageGo to next message
garan
Messages: 27
Registered: January 2016
Junior Member
Hi

we cannot spoon feed with the code as solution but I can tell you the approach for question 1

1. Create a type TYP_SINGLE_CHAR which is table of varchar2(1) basically create a nested table.
2. Create a Function say GET_CHAR_BY_CHAR which accepts P_NAME as string and returns TYPE TYP_SINGLE_CHAR as output
3. In the function logic loop character by character from P_NAME and store it in the collection TYP_SINGLE_CHAR
4. Call the function something like SELECT * FROM TABLE(GET_CHAR_BY_CHAR('ABC')) which will return each character in a separate line as output in sqlplus

Please try and let me know

garani
Re: Column to Row [message #649450 is a reply to message #649449] Sat, 26 March 2016 01:14 Go to previous messageGo to next message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
garan wrote on Sat, 26 March 2016 05:57
Hi

we cannot spoon feed with the code as solution but I can tell you the approach for question 1

1. Create a type TYP_SINGLE_CHAR which is table of varchar2(1) basically create a nested table.
2. Create a Function say GET_CHAR_BY_CHAR which accepts P_NAME as string and returns TYPE TYP_SINGLE_CHAR as output
3. In the function logic loop character by character from P_NAME and store it in the collection TYP_SINGLE_CHAR
4. Call the function something like SELECT * FROM TABLE(GET_CHAR_BY_CHAR('ABC')) which will return each character in a separate line as output in sqlplus

Please try and let me know

garani

Massively over complex solution. This can be done in sql.
Re: Column to Row [message #649452 is a reply to message #649450] Sat, 26 March 2016 03:07 Go to previous messageGo to next message
garan
Messages: 27
Registered: January 2016
Junior Member
Hi

I agree that it can be done through SQL and I thought the OP wanted a PL/SQL function

Please refer the below URL there any many ways we can accomplish for splitting the comma separated words into separate words

https://lalitkumarb.wordpress.com/2014/12/02/split-comma-delimited-string-into-rows-in-oracle/

The only difference is in our case there is no delimiter and we might have to fine tune

garani

[Updated on: Wed, 30 March 2016 00:28] by Moderator

Report message to a moderator

Re: Column to Row [message #649463 is a reply to message #649452] Sat, 26 March 2016 11:44 Go to previous message
pablolee
Messages: 2882
Registered: May 2007
Location: Scotland
Senior Member
garan wrote on Sat, 26 March 2016 08:07
Hi

I agree that it can be done through SQL and I thought the OP wanted a PL/SQL function
No, even if that's what they asked for, it is not the appropriate solution (based on the question posed).

Quote:
Please refer the below URL there any many ways we can accomplish for splitting the comma separated words into separate words

https://lalitkumarb.wordpress.com/2014/12/02/split-comma-delimited-string-into-rows-in-oracle/

The only difference is in our case there is no delimiter and we might have to fine tune
why would I want to follow the link? I know how to perform the task.

[Updated on: Wed, 30 March 2016 00:28] by Moderator

Report message to a moderator

Previous Topic: Simplify SQL Interface to Oracle R Enterprise
Next Topic: Grouping text
Goto Forum:
  


Current Time: Sun Jun 30 15:41:17 CDT 2024