简体   繁体   English

MS-Access SQL DISTINCT GROUP BY

[英]MS-Access SQL DISTINCT GROUP BY

I am currently trying to SELECT the DISTINCT FirstNames in a GROUP , using Microsoft Access 2010.我目前正在尝试使用 Microsoft Access 2010 在GROUP SELECT DISTINCT FirstNames
The simplified relevant columns of my table looks like this:我的表的简化相关列如下所示:

+----+-------------+-----------+
| ID | GroupNumber | FirstName |
+----+-------------+-----------+
|  1 |           1 | Peter     |
|  2 |           1 | Bob       |
|  3 |           1 | Peter     |
|  4 |           2 | Rosemary  |
|  5 |           2 | Jamie     |
|  6 |           3 | Peter     |
+----+-------------+-----------+

My actual table contains two columns to which I want to apply this process (separately), but I should be able to simply repeat the process for the other column.我的实际表包含两列我想应用这个过程(单独),但我应该能够简单地对另一列重复这个过程。 The column group number is a simplification, my table actually groups all rows in a ten day interval together, but I've already solved that problem.列组号是一种简化,我的表实际上将十天间隔内的所有行组合在一起,但我已经解决了这个问题。

And I would like it to return this:我希望它返回这个:

+-------------+------------+
| GroupNumber | FirstNames |
+-------------+------------+
|           1 | Peter      |
|           1 | Bob        |
|           2 | Rosemary   |
|           2 | Jamie      |
|           3 | Peter      |
+-------------+------------+

This means that I want all Distinct FirstNames for each Group.这意味着我想要每个组的所有不同的名字。
A regular DISTINCT would ignore group boundaries and only mention Peter once.常规的DISTINCT会忽略组边界并且只提及一次 Peter。 All aggregate functions reduce my output to only one value or don't work on strings at all.所有聚合函数都将我的输出减少到只有一个值,或者根本不处理字符串。 Access also doesn't support SELECT ing columns that are not aggregates or in the GROUP BY statement. Access 也不支持SELECT列不是聚合或在GROUP BY语句中。

All other answers I've found either want an aggregate, are not applicable to MS Access or are solved by working around the data in ways not applicable to my case.我发现的所有其他答案要么需要聚合,要么不适用于 MS Access,要么通过以不适用于我的案例的方式处理数据来解决。 (Standardized languages are a nice thing, aren't they?) (标准化语言是一件好事,不是吗?)

My current (invalid) query looks like this:我当前的(无效)查询如下所示:

SELECT GroupNumber,
    DISTINCT FirstNames -- This is illegal, distinct applies to all
                        -- columns and doesn't respect groups.
FROM Example AS b
-- Complicated stuff to make the groups
GROUP BY GroupNumber;

This query is a one time thing and is used to analyze a 58000 row excel spreadsheet exported from another Database (not my fault), so optimizing for runtime is not necessary.此查询是一次性的,用于分析从另一个数据库导出的 58000 行Excel 电子表格(不是我的错),因此没有必要优化运行时。

I would like to achieve this purely through SQL and without VBA if at all possible.如果可能的话,我想完全通过 SQL 而没有 VBA 来实现这一点。

This should work:这应该有效:

SELECT DISTINCT GroupNumber, FirstNames
FROM Example AS b

A solution for this problem would be group by the columns GroupNumber and FirstNames at the same time.此问题的解决方案是同时按列 GroupNumber 和 FirstNames 进行分组。 The query is presented below:查询如下所示:

Select GroupNumber,  FirstNames
From input
Group By GroupNumber, FirstNames

(Standardized languages are a nice thing, aren't they?) (标准化语言是一件好事,不是吗?)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM