如何在联合查询中使用别名
How do I use aliases in a Union Query
下面的查询没有问题
SELECT [TblA].fieldA,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<1,[fieldA],Left([fieldA],InStr([fieldA],",")-1))) AS a,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<1,'',IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))=1,Right([fieldA],len([fieldA])-len([a])-1), Mid([fieldA],Len([a])+2,Instr(Len([a])+2,[fieldA],",")-Len([a])-2)))) AS b,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<2,'',IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))=2,Right([fieldA],len([fieldA])-len([a])-len([b])-2), Mid([fieldA],Len([a])+Len([b])+3,Instr(Len([a])+Len([b])+3,[fieldA],",")-Len([a])-Len([b])-3)))) AS c,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<3,'',IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))=3,Right([fieldA],len([fieldA])-len([a])-len([b])-len([c])-3), ""))) AS d
FROM TblA;
但是,当我尝试在联合查询中执行相同操作时,它不起作用(Enter Parameter Value
for a
)
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<1,[fldA],Left([fldA],InStr([fldA],",")-1))) AS a FROM tblA
UNION
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<1,'',IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))=1,Right([fldA],len([fldA])-len([a])-1),Mid([fldA],Len([a])+2,Instr(Len([a])+2,[fldA],",")-Len([a])-2)))) AS b FROM tblA
UNION
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<2,'',IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))=2,Right([fldA],len([fldA])-len([a])-len([b])-2),Mid([fldA],Len([a])+Len([b])+3,Instr(Len([a])+Len([b])+3,[fldA],",")-Len([a])-Len([b])-3)))) AS c FROM tblA
UNION
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<3,'',IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))=3,Right([fldA],len([fldA])-len([a])-len([b])-len([c])-3), ""))) As d FROM tblA
我的问题是:
有没有办法在 Union 查询中使用别名(例如 As a
)
无法从一个 SELECT 引用另一个 SELECT 中的字段(别名与否)。这些不是嵌套语句。此外,只有第一个 SELECT 定义字段名称。后续的 AS 子句将被忽略,可以删除。
在进行这些计算的地方构建第一个查询对象。然后使用第一个查询作为源构建 UNION。
SELECT a AS Data, "A" AS Category FROM query
UNION SELECT b, "B" FROM query
UNION SELECT c, "C" FROM query
UNION SELECT d, "D" FROM query;
UNION 不允许重复行。使用 UNION ALL 包括所有记录,即使有重复项。
下面的查询没有问题
SELECT [TblA].fieldA,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<1,[fieldA],Left([fieldA],InStr([fieldA],",")-1))) AS a,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<1,'',IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))=1,Right([fieldA],len([fieldA])-len([a])-1), Mid([fieldA],Len([a])+2,Instr(Len([a])+2,[fieldA],",")-Len([a])-2)))) AS b,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<2,'',IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))=2,Right([fieldA],len([fieldA])-len([a])-len([b])-2), Mid([fieldA],Len([a])+Len([b])+3,Instr(Len([a])+Len([b])+3,[fieldA],",")-Len([a])-Len([b])-3)))) AS c,
iif(isnull([fieldA]),"",IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))<3,'',IIF(Len([fieldA]) - Len(Replace([fieldA], ",", ""))=3,Right([fieldA],len([fieldA])-len([a])-len([b])-len([c])-3), ""))) AS d
FROM TblA;
但是,当我尝试在联合查询中执行相同操作时,它不起作用(Enter Parameter Value
for a
)
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<1,[fldA],Left([fldA],InStr([fldA],",")-1))) AS a FROM tblA
UNION
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<1,'',IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))=1,Right([fldA],len([fldA])-len([a])-1),Mid([fldA],Len([a])+2,Instr(Len([a])+2,[fldA],",")-Len([a])-2)))) AS b FROM tblA
UNION
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<2,'',IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))=2,Right([fldA],len([fldA])-len([a])-len([b])-2),Mid([fldA],Len([a])+Len([b])+3,Instr(Len([a])+Len([b])+3,[fldA],",")-Len([a])-Len([b])-3)))) AS c FROM tblA
UNION
SELECT iif(isnull([fldA]),"",IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))<3,'',IIF(Len([fldA]) - Len(Replace([fldA], ",", ""))=3,Right([fldA],len([fldA])-len([a])-len([b])-len([c])-3), ""))) As d FROM tblA
我的问题是:
有没有办法在 Union 查询中使用别名(例如 As a
)
无法从一个 SELECT 引用另一个 SELECT 中的字段(别名与否)。这些不是嵌套语句。此外,只有第一个 SELECT 定义字段名称。后续的 AS 子句将被忽略,可以删除。
在进行这些计算的地方构建第一个查询对象。然后使用第一个查询作为源构建 UNION。
SELECT a AS Data, "A" AS Category FROM query
UNION SELECT b, "B" FROM query
UNION SELECT c, "C" FROM query
UNION SELECT d, "D" FROM query;
UNION 不允许重复行。使用 UNION ALL 包括所有记录,即使有重复项。