|
-
Apr 20th, 2008, 10:12 PM
#1
Thread Starter
Member
Using INNER JOIN on multiple table columns for sql query in VB
Hello!
I am trying to find common elements between 24 tables in access using VB 6.0. Currently I have developed the code for finding common elements between two columns in two tables only, you can see my pasted code.
The elements in the column "CustomerNo" have integer values, and the sql string returns the common elements found from the CustomerNo columns of two tables. This is the code:
Code:
Dim sql(0 To 11) As String
Dim month(0 To 11) As String
Dim lngPosition As Long
Dim mtype As Variant
mtype = Array("Month1", "Month2", "Month3", "Month4", "Month5", "Month6", "Month7", "Month8", "Month9", "Month10", "Month11", "Month12", "Month13", "Month14", "Month15", "Month16", "Month17", "Month18", "Month19", "Month20", "Month21", "Month22", "Month23", "Month24")
Dim mno As Variant
mno = Array("Month_1_and_2", "Month_3_and_4", "Month_5_and_6", "Month_7_and_8", "Month_9_and_10", "Month_11_and_12", "Month_13_and_14", "Month_15_and_16", "Month_17_and_18", "Month_19_and_20", "Month_21_and_22", "Month_23_and_24")
'INNER JOIN 24 records to obtain 12 records
For lngPosition = LBound(month) To UBound(month)
sql(lngPosition) = "SELECT DISTINCT " & mtype(lngPosition) & ".CustomerNo " _
& "FROM " & mtype(lngPosition) & " INNER JOIN " & mtype(lngPosition + 1) & "" _
& " ON " & mtype(lngPosition) & ".CustomerNo = " & mtype(lngPosition + 1) & ".CustomerNo"
Set rs = cn.Execute(sql(lngPosition))
.........................
.....................
.................
Currently I am using this string, to find common elements amongst two table columns "CustomerNo", and the result from this is used with the third table column and so on for all 24 columns (uses a looping mechanism) to find the common values in all 24 table columns. The code is tidious and very slow indeed and not very effective.
If I want to find the common for the CustomerNo in between 24 tables? Is it possible to use this sql string or looping in my case is better? The 24 tables have the CustomerNo column, however the number of rows are different in each table. My only concern is using 24 Tables with INNER JOIN separately, would be very complex.. Any ideas on how to solve this?
regards,
JD
Last edited by awyeah; Apr 20th, 2008 at 10:17 PM.
Reason: Changing content..
-
Apr 20th, 2008, 11:51 PM
#2
Re: Using INNER JOIN on multiple table columns for sql query in VB
You could try something like:
vb.net Code:
Dim mtype As String() = {"Month1", "Month2", "Month3", "Month4"} Dim Month As String() = {"0", "1", "2", "3"} Dim _SQL As String = "SELECT DISTINCT CustomerNo " & _ "FROM " & mtype(LBound(Month)) For lngPosition = LBound(Month) + 1 To UBound(Month) _SQL &= " INNER JOIN " & mtype(lngPosition) & " ON " & mtype(lngPosition) & ".CustomerNo = " & mtype(lngPosition - 1) & ".CustomerNo" Next
That would give you:
Code:
SELECT DISTINCT CustomerNo FROM Month1
INNER JOIN Month2 ON Month2.CustomerNo = Month1.CustomerNo
INNER JOIN Month3 ON Month3.CustomerNo = Month2.CustomerNo
INNER JOIN Month4 ON Month4.CustomerNo = Month3.CustomerNo
Why do you have two arrays though? Is it just to match the indices? Because you could simply do this:
vb.net Code:
Dim mtype As String() = {"Month1", "Month2", "Month3", "Month4"} Dim _SQL As String = "SELECT DISTINCT CustomerNo " & _ "FROM " & mtype(0) For X = 1 To mtype.Length - 1 _SQL &= " INNER JOIN " & mtype(X) & " ON " & mtype(X) & ".CustomerNo = " & mtype(X - 1) & ".CustomerNo" Next
Last edited by MaximilianMayrhofer; Apr 20th, 2008 at 11:58 PM.
-
Apr 20th, 2008, 11:55 PM
#3
Re: Using INNER JOIN on multiple table columns for sql query in VB
That is a bad designed database. All months tables should be combined in one with an extra field [Month] added if required.
However, try this (I only try on 6 tables):
Noted: The parentheses in SQL are critical.
Code:
Dim SQL As String
Dim m As Integer
SQL = "Month1"
For m = 2 To 24
SQL = "(" & SQL & " INNER JOIN Month" & m & " ON Month1.CustomerNo = Month" & m & ".CustomerNo)"
Next
'-- create Common Table
SQL = "SELECT DISTINCT Month1.CustomerNo INTO Common FROM " & SQL
DoCmd.RunSQL SQL
'-- or select records only:
'SQL = "SELECT Month1.CustomerNo FROM " & SQL
'... ...
-
Apr 21st, 2008, 05:31 AM
#4
Thread Starter
Member
Re: Using INNER JOIN on multiple table columns for sql query in VB
Last edited by awyeah; Apr 21st, 2008 at 05:46 AM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|