Wrox Programmer Forums
Go Back   Wrox Programmer Forums > SQL Server > SQL Server 2000 > SQL Server 2000
|
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
 
Old July 23rd, 2007, 01:44 AM
Authorized User
 
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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


 
Old July 23rd, 2007, 08:25 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

That is because, the row with Oren having "a", there is "b" with Apple and so on...

_________________________
- Vijay G
Strive for Perfection
 
Old July 23rd, 2007, 08:41 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

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
 
Old July 23rd, 2007, 09:04 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

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]
 
Old July 23rd, 2007, 08:47 PM
Authorized User
 
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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]
 
Old July 23rd, 2007, 09:09 PM
Authorized User
 
Join Date: Jun 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default


 
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...

 
Old July 24th, 2007, 01:36 AM
Friend of Wrox
 
Join Date: May 2006
Posts: 246
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
DataSet - Merge Data Tables srish ADO.NET 0 September 8th, 2007 10:04 AM
Disappearing Column Data dalezjc Classic ASP Databases 1 November 21st, 2006 09:42 AM
merge data in tables-access 2003 megank Access 2 April 5th, 2006 11:31 AM
Compare two Items of data(in column A and column B ever Excel VBA 6 February 13th, 2004 02:19 PM
DTC or other way to merge data Mitch SQL Server DTS 0 January 2nd, 2004 05:37 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.