I think my life has got a great leap today, thanks for Daniel and Bob .
I totally sucess using "JOIN" and "UNION" across two different databases,
now I get the result very easy and also, very fast.
I would like to give publicity to these statements, to those who hope to
understand how to make a clause across two different database.
JOIN
Goods = Server.MapPath("Goods.mdb")
BRecord = Server.MapPath("BRecord.mdb")
SQL = "Select b.ProductID, SUM(b.Qty) as Qty, g.Name From " & BRecord &
".BRecord as b"
SQL = SQL & " INNER JOIN " & Goods & ".Goods as g On b.ProductID =
g.ProductID"
SQL = SQL & " Where b.Year = 2009 And b.Month = 6"
SQL = SQL & " Group By b.ProductID, g.Name"
SQL = SQL & " Order By b.ProductID"
Set rs = GetMdbRecordset( "BRecord.mdb" , SQL)
By using "Response.write SQL" return the following statement :
Select b.ProductID, SUM(b.Qty) as Qty, g.Name From
D:\Sites\Pos\BRecord.mdb.BRecord as b INNER JOIN
D:\Sites\Pos\Goods.mdb.Goods as g On b.ProductID = g.ProductID Where b.Year
= 2009 And b.Month = 6 Group By b.ProductID, g.Name Order By b.ProductID
I have two databases and want to combine the data like below:
BRecord.mdb.BRecord
ProductID Qty Year Month
A101 20 2009 6
A102 30 2009 6
Goods.mdb.Goods
ProductID Name
A101 Flower
A102 Dog
I want to get the result as below
ProductID Qty Name
A101 20 Flower
A102 30 Dog
----------------------------------------------------------
UNION
DMonth = 2009
DYear = 6
SAC = Server.MapPath("MDSAC.mdb")
Hope = Server.MapPath("MDHope.mdb")
Unhcr = Server.MapPath("MDUnhcr.mdb")
DIM SQL, rs
Set objconn = GetMdbConnection( "Total.mdb") ' This mdb is no use, no table
inside, however must exist
SQL = "Select FNo, FName, SUM(DAmount) As EEE From ("
SQL = SQL & "Select FNo, FName, DAmount From " & SAC & ".MData Where DYear
= " & DYear & " And DMonth = " & DMonth & " And IsNull(Void) = True"
SQL = SQL & " UNION ALL "
SQL = SQL & "Select FNo, FName, DAmount From " & Hope & ".MData Where DYear
= " & DYear & " And DMonth = " & DMonth & " And IsNull(Void) = True"
SQL = SQL & " UNION ALL "
SQL = SQL & "Select FNo, FName, DAmount From " & Unhcr & ".MData Where DYear
= " & DYear & " And DMonth = " & DMonth & " And IsNull(Void) = True"
SQL = SQL & ") Group By FNo, FName Order By SUM(DAmount) desc"
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open SQL, objConn
While Not rs.EOF
.....rs("FNo") & "-" & rs("FName") & "-" & rs("EEE")
rs.MoveNext
Wend
By using "Response.write SQL" return the following statement :
Select FNo, FName, SUM(DAmount) As EEE From (Select FNo, FName, DAmount From
D:\Sites\fund\MDSAC.mdb.MData Where DYear = 2009 And DMonth = 6 And
IsNull(Void) = True UNION ALL Select FNo, FName, DAmount From
D:\Sites\fund\MDHope.mdb.MData Where DYear = 2009 And DMonth = 6 And
IsNull(Void) = True UNION ALL Select FNo, FName, DAmount From
D:\Sites\fund\MDUnhcr.mdb.MData Where DYear = 2009 And DMonth = 6 And
IsNull(Void) = True) Group By FNo, FName Order By SUM(DAmount) desc
Hope this help to other people !!