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

Retrieving data that matches a list

7
I've got two tables that I need to match data, the part that has me stymied is that in table1 there is a field that can hold 1 to 3 codes, that will match up against table 2.

Table 1
----------
Schedule_Date (DateTime)
Work_Order_Num (nchar(8))
FCode (nvarchar(9)) <--- 3, 3 character codes



Table 2
-----------
FCode (nchar(3))
FCodeDescription (nvarchar(100))


What would be the best way of going about this query?

Dallas
Mar 18 '08 #1
5 1024
I've got two tables that I need to match data, the part that has me stymied is that in table1 there is a field that can hold 1 to 3 codes, that will match up against table 2.

Table 1
----------
Schedule_Date (DateTime)
Work_Order_Num (nchar(8))
FCode (nvarchar(9)) <--- 3, 3 character codes




Table 2
-----------
FCode (nchar(3))
FCodeDescription (nvarchar(100))


What would be the best way of going about this query?

Dallas
hope this will help

select a.* , b.*
from table1 a, table2 b
where a.Fcode = b.FCode

------------------------------------------------------------

select * from Table1
where FCode in (select FCode from Table2)
Mar 18 '08 #2
debasisdas
8,127 Expert 4TB
The second query should perform faster.
Mar 18 '08 #3
gadall
7
My problem is I need *multiple* FCodeDescriptions returned for each record in Table 1.

Some Sample data:
Table 1
----------
Schedule_Date Work_Order_Num FCode
03/12/2008 13259127 F02F03


Table 2
-----------
FCode FCodeDescription
F01 Work Not Completed
F02 Trash in Work Area
F03 Drop Not Tagged


I need the query to return:

03/12/2008, 13258127, Trash in Work Area Drop Not Tagged


FYI the previous examples do not return a match for records that have more than one FCode in the Table 1 record.
Mar 18 '08 #4
ck9663
2,878 Expert 2GB
My problem is I need *multiple* FCodeDescriptions returned for each record in Table 1.

Some Sample data:
Table 1
----------
Schedule_Date Work_Order_Num FCode
03/12/2008 13259127 F02F03


Table 2
-----------
FCode FCodeDescription
F01 Work Not Completed
F02 Trash in Work Area
F03 Drop Not Tagged


I need the query to return:

03/12/2008, 13258127, Trash in Work Area Drop Not Tagged


FYI the previous examples do not return a match for records that have more than one FCode in the Table 1 record.
Expand|Select|Wrap|Line Numbers
  1. select Schedule_Date, Work_Order_Num,
  2. tblCode1.FCodeDescription , tblCode2.FCodeDescription, tblCode3.FCodeDescription
  3. from
  4. (select Schedule_Date, Work_Order_Num, 
  5. Code1 =
  6.    case when substring(rtrim(Table1.FCode) + space(9),1,3) = '' then NULL else  substring(rtrim(Table1.FCode) + space(9),1,3) end,
  7. Code2 =
  8.    case when substring(rtrim(Table1.FCode) + space(9),4,3) = '' then NULL else  substring(rtrim(Table1.FCode) + space(9),4,3) end,
  9. Code3 =
  10.    case when substring(rtrim(Table1.FCode) + space(9),7,3) = '' then NULL else  substring(rtrim(Table1.FCode) + space(9),7,3) end,
  11. from Table1) tbl1
  12. left join table2 tblCode1 on tbl1.Code1 = tblCode1.FCode
  13. left join table2 tblCode2 on tbl1.Code2 = tblCode2.FCode
  14. left join table2 tblCode3 on tbl1.Code3 = tblCode3.FCode
  15.  
You can still clean this code up and join the table directly using case..when - substring. I just do it this way to make it cleaner so that what am trying to say would actually look like make sense :)

-- CK
Mar 19 '08 #5
gadall
7
Expand|Select|Wrap|Line Numbers
  1. select Schedule_Date, Work_Order_Num,
  2. tblCode1.FCodeDescription , tblCode2.FCodeDescription, tblCode3.FCodeDescription
  3. from
  4. (select Schedule_Date, Work_Order_Num, 
  5. Code1 =
  6.    case when substring(rtrim(Table1.FCode) + space(9),1,3) = '' then NULL else  substring(rtrim(Table1.FCode) + space(9),1,3) end,
  7. Code2 =
  8.    case when substring(rtrim(Table1.FCode) + space(9),4,3) = '' then NULL else  substring(rtrim(Table1.FCode) + space(9),4,3) end,
  9. Code3 =
  10.    case when substring(rtrim(Table1.FCode) + space(9),7,3) = '' then NULL else  substring(rtrim(Table1.FCode) + space(9),7,3) end,
  11. from Table1) tbl1
  12. left join table2 tblCode1 on tbl1.Code1 = tblCode1.FCode
  13. left join table2 tblCode2 on tbl1.Code2 = tblCode2.FCode
  14. left join table2 tblCode3 on tbl1.Code3 = tblCode3.FCode
  15.  
You can still clean this code up and join the table directly using case..when - substring. I just do it this way to make it cleaner so that what am trying to say would actually look like make sense :)

-- CK
Thanks this works with the minor edit of removing the "," after the last end of the case statements.
Mar 19 '08 #6

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

Similar topics

4
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system...
6
by: Dave Hopper | last post by:
Hi I am using the following SQL to retrieve a value in a list box using a unique ID held in the list box call cntID. The list box is used on an order form to list appointments that have been...
5
by: Mystilleef | last post by:
Hello, Is there a simple flag to set to allow overlapping matches for the findall() regular expression method? In other words, if a string contains five occurrences of the string pattern "cat",...
1
by: tucker95112 | last post by:
I am trying to have a search form feed criteria to a query which is the basis for a list box in a search matches form. I cant seem to get the variables to transfer from the search form to the...
2
by: Avi Kak | last post by:
Does Tkinter provide a function that returns all the event descriptors for a given widget class? I am looking for something similar to what you get in Perl/Tk when you call bind() with a single...
0
by: Christoph Haas | last post by:
Hi, list... I have written an application in Perl some time ago (I was young and needed the money) that parses multiple large text files containing nested data structures and allows the user to...
9
ADezii
by: ADezii | last post by:
One question which pops up frequently here at TheScripts is: 'How do I retrieve data from a Recordset once I've created it?' One very efficient, and not that often used approach, is the GetRows()...
3
ADezii
by: ADezii | last post by:
Last Tip, we demonstrated the technique for retrieving data from a DAO Recordset, and placing it into a 2-dimensional Array using the GetRows() Method. This week, we will cover the same exact Method...
34
by: vpriya6 | last post by:
Hi guys, I am new to Ajax, xml and javascript. I want to know how can I retrieve data from xml and display in the html page? please help me out. suppose my xml file is customer.xml the code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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
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...
0
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.