Unpivot in Access SQL

梦想与她 提交于 2019-12-02 13:12:56

Just use union all:

SELECT CustomerID, Phone1 as Phone
FROM dbo.CustomerPhones
UNION ALL
SELECT CustomerID, Phone2 as Phone
FROM dbo.CustomerPhones
UNION ALL
SELECT CustomerID, Phone3 as Phone
FROM dbo.CustomerPhones;

If you want to incur the overhead of removing duplicates, then use UNION.

Bear in mind, if you are really reading from the actual database, you can make use of all RDMBS-specific functionality (in this case MS SQL Server) by using a pass-thru instead of an Access-specific query. The only thing you lose is the designer tools. What I'm saying is you can literally paste your query as-is into Access and use it like any other Access query.

If the table is already in Access (divorced from the linked data source) or isn't supported via pass-thru, this might work. It's pretty brute-force, but if you create a table (call it "Sequence" in this example) with a single field called ID:

ID
1
2
3

Then you can do a cross (Cartesian) join against your Phones table:

select
  p.[CustomerId],
  s.[ID],
  switch (
    s.[ID] = 1, p.[Phone1],
    s.[ID] = 2, p.[Phone2],
    s.[ID] = 3, p.[Phone3]
  ) as Phone
from
  [CustomerPhones] as p,
  [Sequence] as s

The sequence table may have another workaround... Postgresql has a function called generate_series that would be perfect, but I don't know the equivalent in Access.

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