在MS Access中查找以下问题的查询 -
以下是我的数据集,其中最后一行在Value列中为NULL。另外,通过为每个Name + Office + Person + Category执行Max(Value),我已经提取了这些数据以避免多行具有值
ID Name Office Person Category Value
1 FMR Americas Ben Global 7
1 FMR London Ben Global 5
1 FMR London Ben Overall 4.2
156 Asset London Ben Global 13
156 Asset London Ben Overall
157 WSR Paris Zen Global 2
我的预期结果集如下 - 我期待十字标记或任何指示符,表明对于ID,姓名,办公室,人员组合具有全局/超量类别的值或不在单行中。我知道它有点类似于“字符串聚合”
ID Name Office Person Global Overall
1 FMR Americas Ben X
1 FMR London Ben X X
156 Asset London Ben X
157 WSR Paris Zen X
感谢您的投入..
答案 0 :(得分:0)
我玩了一下这个。我创建了两个选择查询Global和Overall
全局
SELECT ID,Name,Office,Person,Category AS Global 来自[YourTable] WHERE Category =“Global”AND Value IS NOT NULL
总体
SELECT ID,Name,Office,Person,Category AS Overall 来自[YourTable] WHERE Category =“Overall”AND Value IS NOT NULL
然后我创建了一个新查询来加入选择查询
SELECT g.ID,g.Name,g.Office,g.Person,Global,Overall 来自全球g LEFT JOIN总体o ON g.ID = o.ID AND g.Name = o.Name AND g.Office = o.Office AND g.Person = o.Person
希望这有帮助。
答案 1 :(得分:0)
首先,获取唯一ID /名称/办公室组合的列表:
SELECT DISTINCT ID, Name, Office, Person
FROM TableName
接下来,为每个类别创建子查询: 对于全球:
SELECT ID, Name, Office, Person
FROM TableName
WHERE Category="Global"
总体而言:
SELECT ID, Name, Office, Person
FROM TableName
WHERE Category="Overall"
最后,将子查询连接到主查询,并使用表达式显示X:
SELECT DISTINCT ID, Name, Office, Person
Iif(Global.ID Is Not Null, "X") AS IsGlobal,
Iif(Overall.ID Is Not Null, "X") AS IsOverall
FROM (TableName
LEFT JOIN (
SELECT ID, Name, Office, Person
FROM TableName
WHERE Category="Global"
) AS Global
ON TableName.ID=Global.ID
AND TableName.Name=Global.Name
AND TableName.Office=Global.Office
AND TableName.Person=Global.Person)
LEFT JOIN (
SELECT ID, Name, Office, Person
FROM TableName
WHERE Category="Overall"
) AS Overall
ON TableName.ID=Overall.ID
AND TableName.Name=Overall.Name
AND TableName.Office=Overall.Office
AND TableName.Person=Overall.Person
您可以更轻松地将子查询保存为Access查询并按名称引用已保存的查询,而不是将整个子查询包含在此查询中。