I'm using MS-Access. I have a table A looks like :
id isin typ1 typ2
1 aa typA typB
2 bb typD typC
a Table C Like:
id isin code1 assetName BBweight
1 aa x x x
2 bb x x x
... .. .. .. ..
I need to insert both tables into table B that looks like:
ID isin fld fldValue BBweight code1 assetName
1 aa typ1 typA w1 x x
2 aa typ2 typB w2 x x
3 bb typ1 typD ... ... ...
4 bb typ2 typC ... ... ...
What's the SQL string, compatible with MS-Access, that I can use?
This is the string i'm using that doesn't work:
sql = "insert into B (isin, code1, assetName, BBweight, fld, [fldValue]) " _
& "SELECT isin, code1, assetName, BBweight, fld, [fldValue] from " _
& "(select isin, code1, assetName, BBweight from C) " _
& "Union " _
& "(select isin, 'typ1' as fld, typ1 as [fldValue] from A " _
& "Union " _
& "select isin, 'typ2' as fld, typ2 as [fldValue] from A) as R "
Here's a tutorial with vb if you're trying to be fancy with code, but the SQL you're looking for is a JOIN.
SELECT *
FROM Table A
JOIN Table B on a.id = b.id and a.isin = b.isin
StackOverflow is here to help when you've hit a roadblock , not build it for you; please show us your attempts and your specific issue, with code, after you've tried it a few times.
EDIT :
Thank you for providing your attempt! I see I skipped over a key element of the requirement. You nearly had it; you want to JOIN against your UNION'd statement, not UNION again. This is the clean SQL for it, that you need to put into your VB:
SELECT
newTableA.*,
tableC.code1,
tableC.assetName,
tableC.BBWeight
FROM
(
SELECT isin, 'typ1' as fld, typ1 as fldValue FROM tableA
UNION
SELECT isin, 'typ2' as fld, typ2 as fldValue FROM tableA
) newTableA
JOIN TableC on newTableA.isin = TableC.isin
You can try
insert into B (isin, fld, fldvalue, BBweight, code1, assetName)
select isin, fld, fldvalue, BBweight, code1, assetName from
(select isin, "typ1" as fld, typ1 as fldvalue from A
union all
select isin, "typ2" as fld, typ2 from A) as R
inner join C on R.isin = C.isin
order by R.isin, fld
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.