473,569 Members | 2,698 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

query for relation between 2 fields

3 New Member
Hi All, I hope someone is able to help me with this.

I am having a table with partnumber(s) like shown here (actually a access2003 database)

ID (autonumber, primary key)
Partnumber (text)
Replacment (text)
Description (text)
Comment (text)
..... <Some other fields>.

Data look like this:
ID Partnumber Replacement Description Comment
x 5030331-001 Partx
x 5030332-001 5030332-002 Part1 from vendor 1 Obsolete
x 5030332-002 8063737-001 Part1 from vendor 1 Obsolete
x 5030333-001 Partx
x .....
x 8063737-001 Part1 from vendor2

There are a lot of Partnumbers which are obsolete (this partnumber has a replacement part). I am searching for a query where (with one parameter, = partnumber to look for), where i can see complete relation. So if I am searching for partnumber 8063737-001 I get same result as when I am searching for 5030332-001 or 5030332-002.

Result should be like this:
ID Partnumber Replacment Description Comment ...........
x 5030332-001 5030332-002 Part1 from vendor 1 Obsolete
x 5030332-002 8063737-001 Part1 from vendor 1 Obsolete
x 8063737-001 Part1 from vendor2


has anyone any idea how to create this kind of query?

Ludo
Nov 22 '09 #1
1 2048
nbiswas
149 New Member
Hi,
I have made a table populated with some data based on what I have understood.
Then I am using a recursive CTE for fetching the result.
I am using SQL SERVER 2005+

Test the test table first and include the appropriate data over there and then run the query.

--Table creation and sample data population

Expand|Select|Wrap|Line Numbers
  1. declare @t table(
  2.     ID int identity
  3.     ,Partnumber  varchar(100)
  4.     ,Replacment varchar(100)
  5.     ,Description varchar(100)
  6.     ,Comment varchar(100))
  7. insert into @t
  8.     select '5030331-001','Part 1',null,null union all
  9.     select '5030332-001','5030332-002','Part1 from vendor 1','Obsolete' union all
  10.     select '5030332-002','8063737-001','Part1 from vendor 1','Obsolete' union all
  11.     select '5030333-001','Part 4',null,null union all
  12.     select '8063737-001',null,'Part1 from vendor 2',null
The output of the sample table is
Expand|Select|Wrap|Line Numbers
  1. ID    Partnumber    Replacment    Description    Comment
  2. 1    5030331-001    Part 1    NULL    NULL
  3. 2    5030332-001    5030332-002    Part1 from vendor 1    Obsolete
  4. 3    5030332-002    8063737-001    Part1 from vendor 1    Obsolete
  5. 4    5030333-001    Part 4    NULL    NULL
  6. 5    8063737-001    NULL    Part1 from vendor 2    NULL
Based on the above table structure, here is the program

Expand|Select|Wrap|Line Numbers
  1. ;with cte as
  2. (
  3.     select id,Partnumber,Replacment,Description,Comment
  4.     from @t
  5.     where Partnumber = '8063737-001' -- Enter the partnumber here
  6.     union all
  7.     select t.id,t.Partnumber,t.Replacment,t.Description,t.Comment
  8.     from @t t
  9.     inner join 
  10.     cte c
  11.     on c.Partnumber = t.Replacment
  12. )
  13.  
  14. select * from cte
  15. order by id
  16. option (maxrecursion 0)
And the output is:

Expand|Select|Wrap|Line Numbers
  1. id    Partnumber    Replacment    Description    Comment
  2. 2    5030332-001    5030332-002    Part1 from vendor 1    Obsolete
  3. 3    5030332-002    8063737-001    Part1 from vendor 1    Obsolete
  4. 5    8063737-001    NULL    Part1 from vendor 2    NULL
Hope this helps.

Let me know in case of any concern
Nov 23 '09 #2

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

Similar topics

1
3680
by: Robert Marshall | last post by:
Hi all, Hoping someone can help me with an issue I'm having. I'm using AccessXP to create a database of U.S. Census records. After keying the data in, I decided to run a query to create a report for each domocile address, and the three names that appear at each street address for the three censii. After running the query, the report...
11
2395
by: deko | last post by:
If I release a new version of my mbd (in mde format) and users want to upgrade - how do they migrate their data? For example, if the original was released as data1.mde and then I release data2.mde (same table/data structures - just revised VBA code with perhaps a new/updated form or two), what's the best way for users to get their data into...
3
1853
by: pw | last post by:
Hi, I am having a mental block trying to figure out how to code this. Two tables: "tblQuestions" (fields = quesnum, questype, question) "tblAnswers" (fields = clientnum, quesnum, questype, answer) They are related by quesnum and questype. There are records in
4
7736
by: Apple | last post by:
1. I want to create an autonumber, my requirement is : 2005/0001 (Year/autonumber), which year & autonumber no. both can auto run. 2. I had create a query by making relation to a table & query, but I can't update record in query or in form. I believe the problem is due to the source query. In source query, there is a filter to show the...
2
2381
by: Fendi Baba | last post by:
I created a person table with various fields such as Suffix, Salutation, etc, Some of these fields may not be mandatory for example suffix. In the actual table itself, I only have a field for suffix ID where 1=Phd, 2= MD. To display all of these to the user, I created a form with an underlying query. The problem I am encountering is this,...
5
2865
by: themastertaylor | last post by:
I've got a system to manage various quotes for building materials for a number of sites. i want a query to produce a report that shows me who HASN'T quoted for which sites. basically so i can print it off and ring round and hurry them up a bit. I have my quote table which has the supplier ID, Site ID and an ID for each quote.(I enter each...
5
3828
by: wugon.net | last post by:
question: db2 LUW V8 UNION ALL with table function month() have bad query performance Env: db2 LUW V8 + FP14 Problem : We have history data from 2005/01/01 ~ 2007/05/xx in single big table, we try separate this big table into twelve tables and create a view
9
1526
by: Miro | last post by:
VS2008 I have created 3 tables. Vendors Customers PhoneNumbers each have their own key Vendor has: VendorID - int unique identifier Customer has: CustomerID - int unique identifier
3
1379
by: LudoS | last post by:
Hi All, I hope someone is able to help me with this. I am having a table with partnumber(s) like shown here ID (autonumber, primary key) Partnumber (text) Replacment (text) Description (text) Comment (text) ..... <Some other fields>.
0
7697
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7924
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8120
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7672
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5512
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5219
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3653
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3640
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2113
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.