简体   繁体   English

Ms-Access GROUP BY,TOP查询

[英]Ms-Access GROUP BY, TOP query

I am using the following query on a .mdb file with the following results. 我在.mdb文件上使用以下查询,结果如下。

SELECT tableA.id, tableA.type, tableA.date, SUM(tableA.val) AS total, tableB.SumB
FROM tableA LEFT JOIN tableB ON tableB.id = tableA.id
GROUP BY tableA.id, tableA.type, tableA.date, tableB.SumB ;

results: 结果:

在此输入图像描述

I need only the first record from the group of records with the same type and date . 我只需要具有相同typedate的记录组中的第一条记录。

I would use DISTINCT but the problem is that I have a different field (in this example id ). 我会使用DISTINCT但问题是我有一个不同的字段(在这个例子中是id )。

Any suggestions? 有什么建议么?

Thanks 谢谢

PS Case 2: PS案例2:

Ids are the same for the red records but we have an additional column 'name' with different string values. 红色记录的ID相同,但我们有一个额外的列'name',其中包含不同的字符串值。

Use min of id and remove it from Group by clause 使用min of id并从Group by子句中删除它

SELECT min(tableA.id) as id, tableA.type, tableA.date, 
SUM(tableA.val) AS total, tableB.SumB 
FROM tableA LEFT JOIN tableB ON tableB.id = tableA.id 
GROUP BY tableA.type, tableA.date, tableB.SumB ; 

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

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