473,498 Members | 359 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

create 2 columns from 1

1 New Member
As part of a web page development I have been driven nearly mad trying to create this query and have failed. I have created a simplified list to get at the heart of my question:
Given this list:

Person-----Owns
1-------------Toyota
1-------------Ford
1-------------Kia
2-------------Kia
2-------------GM
2-------------Mercedes
2-------------Volvo


Create a query such that it displays as follows:

Person1---------Person2
Kia----------------Kia
Toyota----------GM
Ford-------------Mercedes
--------------------Volvo

Few points:
I suspect I can do it in PHP but was trying to create a simple(!) single step solution in mysql.
I want to avoid having 7 rows, 3 of which have nulls in one column and 4 of which have nulls in the other column.
The way they are listed here creates 5 rows-it simply puts a null in the person1 field since there is an extra value in Person2
The ORDER in which they display is not important nor whether they match up in any way.
My suspicion is that a Union query is required of joins on the person field but this may be my newbie status showing through.
I hope I am posting this in the correct forum.
Thank you!
Jan 30 '08 #1
2 1101
Stang02GT
1,208 Recognized Expert Top Contributor
Maybe try a Union Query
Feb 1 '08 #2
amitpatel66
2,367 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT MAX(CASE WHEN id = 1 THEN name else null END) AS ID_1, MAX(CASE WHEN id = 2 THEN name ELSE NULL END) AS ID_2 FROM
  3. (SELECT id,name, row_number() OVER(PARTITION BY ID ORDER BY ID)rn FROM
  4. (SELECT 1 AS ID, 'Toyota' AS NAME FROM DUAL
  5. UNION
  6. SELECT 1,'Ford' FROM DUAL
  7. UNION
  8. SELECT 1, 'Kia' FROM DUAL
  9. UNION
  10. SELECT 2, 'Kia' FROM DUAL
  11. UNION
  12. SELECT 2, 'GM' FROM DUAL
  13. UNION
  14. SELECT 2, 'Mercedes' FROM DUAL))
  15. GROUP BY rn
  16.  
  17.  
I dont have the table structure and data so I had to use the multiple select statements for the data. You can substitute your table name in the query and try executing it.
Feb 2 '08 #3

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

Similar topics

3
2014
by: Michael Lauzon | last post by:
This is not for a class, I have a group on SourceForge, this is what one of the Developers is asking; the more advanced you can make it right off all the better!: Can someone please create...
3
8256
by: anon | last post by:
I have been used to using DAO in the past, and then converted to ADO. Now I am having to use VB.Net(2000) and ADO.NET and am experiencing difficulties with the creation and population of an mdb....
3
1248
by: AW | last post by:
I am creating my columns automatically at run time, but I also want to align the text columns to the left and the number columns to the right. Is there anyway to do this?? I can't see how to...
6
12257
by: Bruce | last post by:
I want to create a new table based on an existing table, but I don't want the tables to have any enforced relationship. Is this possible without having to do a CREATE TABLE and an INSERT? ...
1
3408
by: sunlight_sg | last post by:
Hello, i am using ADOX + VB .NET to create a Access Database programmatically. I plan to set some properties of the column such primary key. The code is as follows: Dim cat As ADOX.Catalog...
27
3739
by: max | last post by:
Hello, I am a newbye, and I'm trying to write a simple application. I have five tables with three columns; all tables are identical; I need to change some data in the first table and let VB...
13
2573
by: =?Utf-8?B?UGV0ZXI=?= | last post by:
I want to create a new column in a datatable from two existing columns. I have no problem to create the new column using the datatable.columns.add method. The problem is the value of the new...
3
3476
by: Niranjan | last post by:
I want create a report based on the crosstab query which normally returns about 50 - 60 columns. The columns have names of the counties and they keep changing for every session. Is there a way to...
1
3353
by: TG | last post by:
Hi! I have an application in which I have some checkboxes and depending which ones are checked those columns will show in the datagridview from sql server or no. After that I have 2 buttons:...
1
6162
by: alhomam | last post by:
hi all i have a table with many columns and i need to create a report that the user can select the columns he needs to show in the report. is it possible? thanks
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6998
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
7163
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
7200
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...
1
6884
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
5460
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,...
1
4904
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4586
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
651
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.