我在MS Access中工作,需要从[ref]字段中删除重复的值,而无需使用不同的(无法在Access中的特定字段上使用)和分组依据(因为我想按[type]分组)。
我尝试使用其他子查询对[ref]字段进行重复数据删除,但是无法使其正常工作。下面的代码有效,但是在[ref]字段中包含重复的值。
SELECT
switch(LEFT(t1.[type],1)='C',"Covers",LEFT(t1.[type],1)='P',"PIP") AS type
, COUNT(t1.[ref])
, COUNT(t2.[posted]) AS Total_posted
, format(COUNT(t2.[posted]) / COUNT(t1.[ref]),"0.00%") AS Percentage_posted
FROM
Some_Table AS t1
LEFT JOIN
(
SELECT
t2.[ID]
, t2.[posted]
FROM
Some_Table AS t2
WHERE
t2.[posted] NOT LIKE ('NA')
)
AS t2
ON
t1.[ID] = t2.[ID]
GROUP BY
LEFT(t1.[type],1)
任何帮助表示赞赏!
编辑:以下示例场景
当前代码将返回t1的COUNT。[ref]为7,但是我希望输出返回5的计数。
进一步编辑:现在,如下所示编写代码,但是t3。[ref]仍未提供不同的值。知道我在哪里错了吗?
SELECT Switch(Left(t1.[type],1)='C',"Covers",Left(t1.[type],1)='P',"PIP") AS type, Count(t3.[ref]) AS CountOfref, Count(t2.[posted]) AS Total_posted, Format(Count(t2.[posted])/Count(t1.[ref]),"Percent") AS Percentage_posted
FROM (additional_data_fields AS t1 LEFT JOIN (Select t2.[ID],t2.[posted] from additional_data_fields as t2 where t2.[posted] not like ('NA')) AS t2 ON t1.[ID] = t2.[ID]) LEFT JOIN (select distinct t3.[ref] from additional_data_fields as t3) AS t3 ON t1.[ref] = t3.[ref]
GROUP BY Left(t1.[type],1)