473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

how to merge two rows with only one unique column

I have a problem,The table like this

emp_id emp_name dept
------------ ----------------- ---------

First Row-> 1 Sachin Phy

Second Row-> 1 Sachin Maths

Third Row-> 2 Dravid Che


After executing a query I need the table like this
emp_id emp_name dept1 dept2
------------ ----------------- --------- --------

First Row->1 Sachin Phy Maths

Second Row-> 2 Dravid Che

Please help me...............Thank you in advance
Aug 14 '07 #1
2 9287
azimmer
200 Expert 100+
I have a problem,The table like this

emp_id emp_name dept
------------ ----------------- ---------

First Row-> 1 Sachin Phy

Second Row-> 1 Sachin Maths

Third Row-> 2 Dravid Che


After executing a query I need the table like this
emp_id emp_name dept1 dept2
------------ ----------------- --------- --------

First Row->1 Sachin Phy Maths

Second Row-> 2 Dravid Che

Please help me...............Thank you in advance
If you have only two depts an emp may belong to, the following code helps. If you have an arbitrary number of such depts a lot more complex process is needed (and will still not get proper columns but a string). If you need real columns in the latter case an even more complex solution is needed. Please let us know what you do need.

Expand|Select|Wrap|Line Numbers
  1. select t1.emp_id, t1.emp_name, t1.dept, t2.dept
  2. from mytable t1 inner join mytable t2 on t1.emp_id=t2.emp_id
  3. where (t1.dept<t2.dept)
  4. union 
  5. select t1.emp_id, t1.emp_name, t1.dept, NULL
  6. from mytable t3
  7. where t3.emp_id not in (select t1.emp_id from mytable t1 inner join mytable t2 on t1.emp_id=t2.emp_id
  8. where (t1.dept<t2.dept))
  9.  
Aug 15 '07 #2
you can create the temp table that the fields are 'emp_id,emp_name,dept1,dept2'

checking the value in dept1, if there has value, put it to dept2.




I have a problem,The table like this

emp_id emp_name dept
------------ ----------------- ---------

First Row-> 1 Sachin Phy

Second Row-> 1 Sachin Maths

Third Row-> 2 Dravid Che


After executing a query I need the table like this
emp_id emp_name dept1 dept2
------------ ----------------- --------- --------

First Row->1 Sachin Phy Maths

Second Row-> 2 Dravid Che

Please help me...............Thank you in advance
Aug 16 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Simon Jackson | last post by:
I am trying to convert a column to a unique column. Tried to look at some examples on-line but failed to find the correct syntax I need. So, while I'm waiting for my SQL book to arrive, a pointer...
3
by: becoolmun | last post by:
It seems with V7 of DB2 I can now use Fetch First 1 Rows Only in a Select stmt, whic is great, because I don't have to use a cursor. Unfortunately, it doesn't allow ORDER BY in the same Select and...
5
by: Bernd Hohmann | last post by:
Is there any way to create a dynamic row limit like "fetch first ? rows only" using the JDBC driver and a PreparedStatement? Bernd -- "Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch...
2
by: Mattyw | last post by:
Hi I have a sqlcommand that returns all the rows in a column and then pass that to a datareader. I am new to VS.Net and so far I can only return the first row in the first column using ...
5
by: jdwyer05 | last post by:
Hello, I am trying to populate a combo box with only unique values. Currently I am using an access database and VB6 Enterprise. The program populates the combo box fine however, there are several...
0
by: princego1 | last post by:
I 've got a table ,and I want to check for a unique column ,so that there is no dups in my mysql. So how is it done using jdbc ? This is my part code / Create PreparedStatement object String...
2
by: hrgilley | last post by:
I hope someone here can help me. I am trying to merge rows of data within a table. Here's what I have Guest id: Row: Section: Seat: 567 A CC 102 567 ...
1
by: venkuforu | last post by:
Hi, How to Make Only one column Editable in vsfflexgrid.....
2
by: Bob | last post by:
Hi, How can I force a listview to have only one column so that it resembles a listbox? Thanks, Bob
2
by: kaviyarasan | last post by:
I have a table called COLUMN_VALUE which has only one column. Inside a procedure, I like to check whether chkmp(this is field name of other table) is equal to any of the values of column_value......
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.