converting column names to use as row fields in Access

空扰寡人 提交于 2020-01-03 06:00:06

问题


I am importing sql into my Access database and am working on parsing the data into the correct tables and fields. I have run into an issue as my import creates column names and enters the values into the columns but the database uses those column names as row values and the values in a separate column

Current table

 SC | DO | temp | pH | etc

 val|val | val  |val | val

table I am attempting to parse into

 Characteristic_Name | Result_Value

 SC                  |  val
 DO                  |  val
 temp                |  val
 pH                  |  val
 etc.                |  val

I have done a union query to get the results column to populate nicely but I cannot get the column names to parse Is there a way to do this?


回答1:


Since you are using MS Access, there is no UNPIVOT function so you can use a UNION ALL query:

select 'SC' as Characteristic_Name, SC as Val
from yourtable
union all
select 'DO' as Characteristic_Name, DO as Val
from yourtable
union all
select 'temp' as Characteristic_Name, temp as Val
from yourtable
union all
select 'pH' as Characteristic_Name, pH as Val
from yourtable
union all
select 'etc' as Characteristic_Name, etc as Val
from yourtable;

As a side note, when you are doing a UNION ALL or UNPIVOT, the datatypes must be the same so you might have to convert the data in the val column so it is the same.




回答2:


You can create a VBA function similar to this one:

Set db = CurrentDb()
Set rs1 = db.OpenRecordset("CurrentTable")
Dim fld As DAO.Field
Dim SQL as string
For Each fld In rs1.Fields
    SQL = SQL & fld.Name & ","
Next
...

With this you can extract field names and add the code needed to compose a SQL string for insert data in your normalized table.




回答3:


If you wish to avoid SQL code directly you can do the above using the Access Query editor.

Select all the fields in your table after pressing the Query Design button from the design toolbar.

Remove the fields SC, DO, temp, pH etc.

In the query editor tab, add new fields:

  • Characteristic_Name: "SC"
  • Characteristic_Value: SC

and press the make table button on the design toolbar, choose a new table name and then run the query to create this table.

Then open the same query again and do the following field edits:

  • Characteristic_Name: "DO"
  • Characteristic_Value: DO

and press the append table button on the design toolbar.

Choose the same new table name as above and run the query.

Check that you have double the number of rows.

Repeat again for edits:

  • Characteristic_Name: "temp"
  • Characteristic_Value: temp

etc.

At the end check you have multiplied the number of rows by the number of columns you wish to drop.

Although not as quick as the above anyone can use on their own problem, especially those who are worried about getting code wrong.



来源:https://stackoverflow.com/questions/20227458/converting-column-names-to-use-as-row-fields-in-access

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