我是SQL和VBA的新手。我试图在VBA中使用OPENQUERY执行以下Oracle SQL查询来查询链接服务器:
with
untagged_affiliates as (
select
ife.entityid,
'Untagged affiliate' as TYPE_CHANGE,
to_number(
case when IFE.OLDVALUE is not null
then EXTRACTVALUE(xmltype(IFE.OLDVALUE), '/DocumentElement/ORG_AFFILIATIONS/AFFILIATED_ORGID')
else null
end) as RELATED_ID,
ife.analystuserid as ANALYST,
ife.auditdatetime
from ife.audittrail ife
where
IFE.ATTRIBUTEID = 314
and
ife.newvalue is null
and
ife.oldvalue is not null
and
ife.AUDITDATETIME
between
TO_DATE('27/11/2016 00:00:00','dd/mm/yyyy hh24:mi:ss')
and
TO_DATE('03/12/2016 23:59:59 ','dd/mm/yyyy hh24:mi:ss')
),
untagged_unv_subs as (
select
to_number(ife.oldvalue) as EntityID,
'Untagged Unverified Sub' as TYPE_CHANGE,
ife.entityid as RELATED_ID,
ife.analystuserid as ANALYST,
ife.auditdatetime
from ife.audittrail ife
left join oa.organizations o on ife.entityid = o.orgid
where
IFE.ATTRIBUTEID = 268
and
ife.newvalue is null
and
ife.oldvalue is not null
and
o.org_is_verified = 0
and
o.org_immediate_parent_orgid is null
and
ife.AUDITDATETIME
between
TO_DATE('27/11/2016 00:00:00','dd/mm/yyyy hh24:mi:ss')
and
TO_DATE('03/12/2016 23:59:59 ','dd/mm/yyyy hh24:mi:ss')
),
union_queries as
(
select * from untagged_affiliates
union
select * from untagged_unv_subs
)
select
uq.*,
case
when (L.LOCATION_NAME = 'Bangalore') then 'BLR'
when (L.LOCATION_NAME = 'Beijing' or L.LOCATION_NAME = 'Singapore') then 'Asia'
when (L.LOCATION_NAME = 'Gdansk' or L.LOCATION_NAME = 'Nicosia') then 'EMEA'
else 'Other'
end as REGION,
C.COUNTRYDESC,
u.USERID,
u.username as ANALYST
from union_queries uq
left join oa.organizations o on uq.entityid = o.orgid
left join admin.USERS U on UQ.ANALYST = U.USERID
inner join admin.USERSUSERGROUPS ug on u.userid = ug.userid and ug.groupcode in (1,2,4,29,30,31,34,42,43,45,46,55,56)
left join DOMAINS.COUNTRYCODES C on coalesce(o.org_country_of_domicile,o.org_country_of_incorporation) = C.COUNTRYCODE
left join domains.TR_LOCATIONS l on u.PRIMARYLOCATION = l.LOCATION_ID
我用来查询链接服务器的相应VBA代码如下:
'SQL_String = "select * from openquery([OA],'with " & _
'"untagged_affiliates as (select ife.entityid, to_number( case when IFE.OLDVALUE is not null then EXTRACTVALUE(xmltype(IFE.OLDVALUE), ''/DocumentElement/ORG_AFFILIATIONS/AFFILIATED_ORGID'') else null end) as RELATED_ID, ife.analystuserid as ANALYST, ife.auditdatetime from ife.audittrail ife where IFE.ATTRIBUTEID = 314 and ife.newvalue is null and ife.oldvalue is not null and ife.AUDITDATETIME between TO_DATE(''" & from_date_string & " 00:00:00'',''dd/mm/yyyy hh24:mi:ss'') and TO_DATE(''" & to_date_string & " 23:59:59 '',''dd/mm/yyyy hh24:mi:ss'') ), " & _
'"untagged_unv_subs as (select to_number(ife.oldvalue) as EntityID, ife.entityid as RELATED_ID, ife.analystuserid as ANALYST, ife.auditdatetime from ife.audittrail ife left join oa.organizations o on ife.entityid = o.orgid where IFE.ATTRIBUTEID = 268 and ife.newvalue is null and ife.oldvalue is not null and o.org_is_verified = 0 and o.org_immediate_parent_orgid is null and ife.AUDITDATETIME between TO_DATE(''" & from_date_string & " 00:00:00'',''dd/mm/yyyy hh24:mi:ss'') and TO_DATE(''" & to_date_string & " 23:59:59 '',''dd/mm/yyyy hh24:mi:ss'') ), " & _
'"union_queries as (select * from untagged_affiliates union select * from untagged_unv_subs ) select uq.*, case when (L.LOCATION_NAME = ''Bangalore'') then ''BLR'' when (L.LOCATION_NAME = ''Beijing'' or L.LOCATION_NAME = ''Singapore'') then ''Asia'' when (L.LOCATION_NAME = ''Gdansk'' or L.LOCATION_NAME = ''Nicosia'') then ''EMEA'' else ''Other'' end as REGION, C.COUNTRYDESC, u.USERID, u.username as ANALYST " & _
'"from union_queries uq left join oa.organizations o on uq.entityid = o.orgid left join admin.USERS U on UQ.ANALYST = U.USERID inner join admin.USERSUSERGROUPS ug on u.userid = ug.userid and ug.groupcode in (1,2,4,29,30,31,34,42,43,45,46,55,56) left join DOMAINS.COUNTRYCODES C on coalesce(o.org_country_of_domicile,o.org_country_of_incorporation) = C.COUNTRYCODE left join domains.TR_LOCATIONS l on u.PRIMARYLOCATION = l.LOCATION_ID')"
'recset.Open SQL_String, con
'ThisWorkbook.Sheets("Untagged").Range("A2").CopyFromRecordset recset
因此,我认为我无法进行单个查询,其中两个表具有相同名称的列,并且它们将它们用于UNION。但是,如果我不能在两者上为列ANALYST分配相同的名称,我该如何统一表?