return value from other table in the same Database

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

return value from other table in the same Database

Post by sal21 »

I have this sql:

Code: Select all

    OCONN_RS.Open "SELECT AT_4, AGENZIA_4, AGENZIA_CENTRO, REGION, MERCATO_DISTRIB, ZONA FROM TGERARCHIA WHERE TIPO_UO_1='21' AND DES_STATO_PTF='ATTIVO' AND DENDVAL='" & Format(DtaInizio, "mm/dd/yyyy") & "' ORDER BY AGENZIA_4", GAF_CONN, adOpenForwardOnly, adLockReadOnly
in the same databse have Table1 with field name AGENZIA_4x and a field NUMBS.

Is possible to integrate the sql to match AGENZIA_4 with AGENZIA_4x and return the value of NUMBS...
I think is possible with Join statement but dont know the sintiax...

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: return value from other table in the same Database

Post by HansV »

Try

OCONN_RS.Open "SELECT TGERARCHIA.AT_4, TGERARCHIA.AGENZIA_4, TGERARCHIA.AGENZIA_CENTRO, TGERARCHIA.REGION, TGERARCHIA.MERCATO_DISTRIB, TGERARCHIA.ZONA, Table1.NUMBS FROM TGERARCHIA LEFT JOIN Table1 ON TGERARCHIA.AGENZIA_4 = Table1.AGENZIA_4x WHERE TGERARCHIA.TIPO_UO_1='21' AND TGERARCHIA.DES_STATO_PTF='ATTIVO' AND TGERARCHIA.DENDVAL='" & Format(DtaInizio, "mm/dd/yyyy") & "' ORDER BY TGERARCHIA.AGENZIA_4", GAF_CONN, adOpenForwardOnly, adLockReadOnly
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: return value from other table in the same Database

Post by sal21 »

HansV wrote:Try

OCONN_RS.Open "SELECT TGERARCHIA.AT_4, TGERARCHIA.AGENZIA_4, TGERARCHIA.AGENZIA_CENTRO, TGERARCHIA.REGION, TGERARCHIA.MERCATO_DISTRIB, TGERARCHIA.ZONA, Table1.NUMBS FROM TGERARCHIA LEFT JOIN Table1 ON TGERARCHIA.AGENZIA_4 = Table1.AGENZIA_4x WHERE TGERARCHIA.TIPO_UO_1='21' AND TGERARCHIA.DES_STATO_PTF='ATTIVO' AND TGERARCHIA.DENDVAL='" & Format(DtaInizio, "mm/dd/yyyy") & "' ORDER BY TGERARCHIA.AGENZIA_4", GAF_CONN, adOpenForwardOnly, adLockReadOnly
Sorry me!

But during the work i have changed the sql with a group by, sql new:

Code: Select all

OCONN_RS.Open "SELECT AT_4, AGENZIA_4, AGENZIA_CENTRO, REGION, MERCATO_DISTRIB, ZONA FROM TGERARCHIA WHERE TIPO_UO_1='21' AND DES_STATO_PTF='ATTIVO' AND DENDVAL='" & Format(DtaInizio, "mm/dd/yyyy") & "' GROUP BY AT_4, AGENZIA_4, AGENZIA_CENTRO, REGION, MERCATO_DISTRIB, ZONA  ORDER BY AGENZIA_4", GAF_CONN, adOpenForwardOnly, adLockReadOnly

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: return value from other table in the same Database

Post by HansV »

So what do you want to do with NUMBS? Sum, average, count, ...?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: return value from other table in the same Database

Post by sal21 »

HansV wrote:So what do you want to do with NUMBS? Sum, average, count, ...?
Sample the value of NUMBS....

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: return value from other table in the same Database

Post by HansV »

So you want to group by NUMBS?
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4362
Joined: 26 Apr 2010, 17:36

Re: return value from other table in the same Database

Post by sal21 »

HansV wrote:So you want to group by NUMBS?
Yes!

User avatar
HansV
Administrator
Posts: 78545
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: return value from other table in the same Database

Post by HansV »

You should be able to add the field to the SELECT part and the GROUP BY part yourself now.
Best wishes,
Hans