 |
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|

July 23rd, 2007, 01:44 AM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
help merge same column data
example :my table( fruits) the column are oren and apple
[u]oren</u>
a
b
c
d
e
[u]apple</u>
b
c
a
l
k
and i want to display the table like below
oren apple
a a
b b
c c
so what is the query??
------------------------------------------------------------
i try to entry this query
select * from fruit where oren in (select apple from fruit)
but the result is not my expected..
oren apple
a b
b c
c a
|

July 23rd, 2007, 08:25 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
That is because, the row with Oren having "a", there is "b" with Apple and so on...
_________________________
- Vijay G
Strive for Perfection
|

July 23rd, 2007, 08:41 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
|
|
I can't think of other way of doing it. ;)
Code:
select oren into #oren from table1
select apple into #apple from table1
select * from #oren, #apple where #oren.oren = #apple.apple
drop table #oren
drop table #apple
Hope that helps.
Cheers
_________________________
- Vijay G
Strive for Perfection
|

July 23rd, 2007, 09:04 AM
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
How about a self join of the fruits table:
SELECT F1.oren, F2.apple
FROM Fruits F1
INNER JOIN Fruits F2 ON F1.oren = F2.apple
But why would you have a table structured this way? I'm having trouble understanding the relationship between the two columns you are trying to express.
Jeff Mason
[email protected]
|

July 23rd, 2007, 08:47 PM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Jeff Mason
How about a self join of the fruits table:
SELECT F1.oren, F2.apple
FROM Fruits F1
INNER JOIN Fruits F2 ON F1.oren = F2.apple
But why would you have a table structured this way? I'm having trouble understanding the relationship between the two columns you are trying to express.
Jeff Mason
[email protected]
|
|

July 23rd, 2007, 09:09 PM
|
Authorized User
|
|
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Quote:
quote:Originally posted by Jeff Mason
|
Quote:
How about a self join of the fruits table:
SELECT F1.oren, F2.apple
FROM Fruits F1
INNER JOIN Fruits F2 ON F1.oren = F2.apple
But why would you have a table structured this way? I'm having trouble understanding the relationship between the two columns you are trying to express.
Jeff Mason
[email protected]
|
thank you can work....:)
actually my table still have 1 column for ID and the ID is based on oren column, so because of this i need to find out the ID for the same data...
|

July 24th, 2007, 01:36 AM
|
Friend of Wrox
|
|
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Code:
-- Prepare sample data
DECLARE @Sample TABLE (oren CHAR, Apple CHAR)
INSERT @Sample
SELECT 'a', 'b' UNION ALL
SELECT 'b', 'c' UNION ALL
SELECT 'c', 'a' UNION ALL
SELECT 'd', 'l' UNION ALL
SELECT 'e', 'k'
-- Peso
SELECT theValue AS oren,
theValue AS Apple
FROM (
SELECT oren AS theValue
FROM @Sample
UNION ALL
SELECT Apple
FROM @Sample
) AS d
GROUP BY theValue
HAVING COUNT(*) > 1
-- Mason
SELECT s1.oren,
s2.apple
FROM @Sample AS s1
INNER JOIN @Sample AS s2 ON s2.oren = s1.apple
|
|
 |