473,385 Members | 2,269 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,385 software developers and data experts.

Self join one table

2
Hello,

It is a bit tricky to explain, but I will try with sample. I have one table:

ID, server
1, server1
2, server2
3, server1
4, server3
5, server4

I need to fill second table with records like this:

IDFrom, IDTo, Server1, Server2
1, 2, server1, server2
2, 5, server2, server5
3, 4, server1, server4
4, 3, server3, server1
5, 1, server4, server1

Goal is to create in first column same ID's as in first table and same server as in first table. Second ID is created using random order of ID's from same table, but taking in care that servers cannot be equal. Example of wrong records are:

1, 1, server1, server1 (same servers)
1, 3, server1, server1 (different ID's but same servers again)

I am struggling with this for over 2 weeks and I have no acceptable solution.

Actually I have make this work by creating one temp table which contains all possible ID to ID records and then using additional query I can extract desired records. Problem in this case is that when table have 10.000 records. In this case temp table will contain 10.000x10.000 records = 100 M of records which is too much. As I plan to run this on as many as 1 M records in first table, it looks like impossible task.

Is this possible with SQL?

Thanks!
Nov 20 '07 #1
2 1542
deepuv04
227 Expert 100+
Hello,

It is a bit tricky to explain, but I will try with sample. I have one table:

ID, server
1, server1
2, server2
3, server1
4, server3
5, server4

I need to fill second table with records like this:

IDFrom, IDTo, Server1, Server2
1, 2, server1, server2
2, 5, server2, server5
3, 4, server1, server4
4, 3, server3, server1
5, 1, server4, server1

Goal is to create in first column same ID's as in first table and same server as in first table. Second ID is created using random order of ID's from same table, but taking in care that servers cannot be equal. Example of wrong records are:

1, 1, server1, server1 (same servers)
1, 3, server1, server1 (different ID's but same servers again)

I am struggling with this for over 2 weeks and I have no acceptable solution.

Actually I have make this work by creating one temp table which contains all possible ID to ID records and then using additional query I can extract desired records. Problem in this case is that when table have 10.000 records. In this case temp table will contain 10.000x10.000 records = 100 M of records which is too much. As I plan to run this on as many as 1 M records in first table, it looks like impossible task.

Is this possible with SQL?

Thanks!
try the following query


SELECT S.ID,S.SERVER,t.id,T.SERV
FROM TABLE1 S, (SELECT ID,SERVER FROM TABLE1) AS T
WHERE T.SERVER <> S.SERVER
ORDER BY S.ID


IF the above query is returning the values you want then use insert statement along with that
Nov 21 '07 #2
banem
2
try the following query


SELECT S.ID,S.SERVER,t.id,T.SERV
FROM TABLE1 S, (SELECT ID,SERVER FROM TABLE1) AS T
WHERE T.SERVER <> S.SERVER
ORDER BY S.ID


IF the above query is returning the values you want then use insert statement along with that

Thanks, but what about random order?
Nov 21 '07 #3

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

Similar topics

4
by: Denis St-Michel | last post by:
Hello All, Hope some Guru will be able to help me with this. Let's take this example table A ------------------------------------------------------------------------------- id | ...
2
by: Tech | last post by:
I have a table tblEmails where the columns are id,list_id,address_id. I have many lists. I need to find out if a couple of lists (list_ids - 1000,1001,1002) have same address_ids in common or...
7
by: Andrew Mayo | last post by:
Here's a really weird one for any SQL Server gurus out there... We have observed (SQL Server 2000) scenarios where a stored procedure which (a) begins a transaction (b) inserts some rows into...
4
by: jbm05 | last post by:
Hi, I'm curious about the computational complexity of a query I have. The query contains multiple nested self left joins, starting with a simple select, then doing a self left join with the...
6
by: Christopher Harrison | last post by:
Is there a way to store an indefinite number of keys in one field and self join them? Imagine, for example, you have a table of users with a "friends" column. Say user 1 is friends with users 9, 7,...
4
by: Shahzad | last post by:
dear respected gurus, I would like to knew how to apply append,insert query for a self table where no primary keys issues. i do have problem say there are 5 rows of single record, this is data...
6
by: davegb | last post by:
I'm trying to create a self-join table to show the relationship between employee and supervisor. In another thread, I was advised to create a SupervisorID in the employee table, a separate...
2
by: Ev | last post by:
I have a database table in SQL Server that has a self join. In C# I have a DataTable with a self-join. I have defined a foreign key constraint on the DataTable for the self join. The...
3
by: sks | last post by:
I have a table that contains keywords (Varchars) each one mapped to a product. so the database schema is just an id column, product column and keyword column. I want to select the products that...
2
by: Darragh | last post by:
Hi all! I'm having a bit of an issue making a self join in access (similar to the example explained on Allen Browne's excellent site - http://allenbrowne.com/ser-06.html). I've made the self...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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
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
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...

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.