嗨,大家好,我试图在MS Access的SQL中使用unpivot,但我在网上找到了以下代码:
SELECT CustomerID, Phone FROM ( SELECT CustomerID, Phone1, Phone2, Phone3 FROM dbo.CustomerPhones ) AS cp UNPIVOT ( Phone FOR Phones IN (Phone1, Phone2, Phone3) ) AS up;
从此网页:https : //www.mssqltips.com/sqlservertip/3000/use-sql-servers-unpivot-operator-to- help-normalize-output/
但是,当我在Access上尝试使用完全相同的代码时,它总是说该FROM子句有错误。
FROM
我想知道这是否是因为访问语法与SQL Server中的语法有所不同吗?如果有人可以告诉我如何使此代码运行,我将非常感激。
只需使用union all:
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;
如果您要承担删除重复项的开销,请使用UNION。
UNION