遗留数据库具有表示为多个字段的关联类。即使关联类没有被标准化,我也需要标准化值以消除轻微的拼写变化。
我想在单个UNION查询中累积所有值,并且还想计算出现次数。
查询会累积所有值,但每个值的count字段总是为1。我在表中看到了相同值的多个条目。
这里的问题可能是MS Access,因为它出现在其他Stackoverflow条目中,这适用于其他类型的SQL。
SELECT [Allofem].Keyword, Count(*) as Occurrences
FROM
(
SELECT Keyword1 as Keyword
FROM srctab
WHERE (Keyword1 Is Not Null And Keyword1<>"")
UNION
SELECT Keyword2 as Keyword
FROM srctab
WHERE (Keyword2 Is Not Null And Keyword2<>"")
UNION
SELECT Keyword3 as Keyword
FROM srctab
WHERE (Keyword3 Is Not Null And Keyword3<>"")
UNION
SELECT Keyword4 as Keyword
FROM srctab
WHERE (Keyword4 Is Not Null And Keyword4<>"")
UNION
SELECT Keyword5 as Keyword
FROM srctab
WHERE (Keyword5 Is Not Null And Keyword5<>"")
UNION
SELECT Keyword6 as Keyword
FROM srctab
WHERE (Keyword6 Is Not Null And Keyword6<>"")
) AS [Allofem]
GROUP BY [Allofem].Keyword
ORDER BY [Allofem].Keyword;
更新:这里有一些非常奇怪的事情,因为当你只选择一个关键字字段(即没有UNION)时,与其他关键字字段选择的UNIONED相比,会有更多的记录。