Compressing multiple fields to a single column in MS-Access

不羁岁月 提交于 2019-12-25 06:59:02

问题


I am attempting to normalize a database that uses multiple columns for the same thing. For instance, Street, Street2, ..., Street12. My current plan is to create a separate Street table, containing columns for Street and mainID, (plus a new ID field for this table,) where mainID is the primary key for the main database. (Example) Each mainID could be linked to multiple Street records as needed, allowing for the removal of the Street columns in the main table.

I'm planning to reference this new table through subqueries (as per this page) to streamline our search form. (As you can imagine, it's somewhat difficult to maintain right now.) A sample query for this structure might be SELECT name FROM mainDB WHERE fID IN (SELECT fID FROM idStreets WHERE street=[inputItem]);.

The problem is that the database already has some 17000 entries; far too many to reasonably correct by hand. How should I go about automating the conversion?


回答1:


First, to create the Streets table run a union query then output it to a table:

SELECT mainID, Street1
FROM maintable;
UNION 
SELECT mainID, Street2
FROM maintable;
...
UNION
SELECT mainID, Street12
FROM maintable;

Then the make-table query where afterwards you add an autonumber id.

SELECT * INTO Street FROM unionqry;

Finally, your subquery can very well be a simple join:

SELECT name FROM mainDB 
WHERE fID IN 
   (SELECT fID FROM idStreets WHERE street=[inputItem]);

Becomes:

SELECT DISTINCT name FROM mainDB 
INNER JOIN Streets ON mainDB.fID = Streets.mainID
WHERE street=[inputItem]);


来源:https://stackoverflow.com/questions/31748908/compressing-multiple-fields-to-a-single-column-in-ms-access

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!