计算表中的出现次数

时间:2012-05-09 14:22:16

标签: sql ms-access vba ms-access-2007

假设我想计算列中字符串中包含的名称的总出现次数,并在该名称旁边的新列中显示该名称的所有出现次数旁边的总数。例如,如果我有:

Name  | Home Address | Special ID 
==================================
Frank | 152414       | aTRF342
Jane  | 4342342      | rRFC432
Mary  | 423432       | xTRF353
James | 32111111     | tLZQ399
May   |    4302443   | 3TRF322

如何计算“TRF”,“RFC”或“LZQ”等特殊标签的出现次数,以便表格如下所示:

Name  | Home Address | Special ID  | Occurrences
================================================
Frank | 152414       | aTRF342     |     3
Jane  | 4342342      | rRFC432     |     1
Mary  | 423432       | xTRF353     |     3
James | 32111111     | tLZQ399     |     1
May   |    4302443   | 3TRF322     |     3

目前正在使用Access 2007.这是否可以使用SQL查询?感谢

5 个答案:

答案 0 :(得分:0)

您必须GROUP BY Special ID的子字符串。在MS Access中,您可以阅读有关如何计算子字符串here的信息。

您的问题是,Special ID列中的数据不符合标准模式,可以通过substring功能轻松提取。您可能需要使用regular expressions来提取此类值,然后将GROUP BY应用于它们。

使用MSSQL,Oracle,PostgreSQL,您将能够声明一个存储过程(MS SQL Server中的example CLR function),可以为您执行此操作。 MS Access不确定。

答案 1 :(得分:0)

你可以这样做:

select Name, [Home Address], [Special ID], 
       (select count(*) from [your table] where [Special ID] = RemoveNonAlphaCharacters([Special ID]) ) as Occurrences
from [your table]

辅助功能(来自此link):

Create Function [dbo].[RemoveNonAlphaCharacters](@Temp VarChar(1000))
Returns VarChar(1000)
AS
Begin

    While PatIndex('%[^a-z]%', @Temp) > 0
        Set @Temp = Stuff(@Temp, PatIndex('%[^a-z]%', @Temp), 1, '')

    Return @Temp
End

答案 2 :(得分:0)

假设您的第一个表名为'table_with_string'

以下代码将根据Special ID列中字符串的前3个字符显示出现的情况。因为不清楚你是如何传递字符串以匹配

select tws.Name,tws.HomeAddress,tws.SpecialID,str_count.Occurrences from    
table_with_string tws
left join
(select SpecialID,count(*) from table_with_string where specialID like(substring  
(specialid,0,3))
group by specialId) as str_count(id,Occurrences)
on str_count.id=tws.SpecialID

答案 3 :(得分:0)

我建议明确地将其作为连接进行,因此您清楚它是如何工作的:

select tws.Name, tws.HomeAddress, tws.SpecialID, str_count.Occurrences
from table_with_string tws 
join
(
  select substring(spcecialid, 2, 3) as code, count(*) as Occurrences
  from table_with_string tws
  group by substring(spcecialid, 2, 3)
) s
  on s.code = substring(tws.spcecialid, 2, 3)

答案 4 :(得分:0)

使用Access 2007,我将示例数据存储在名为 tblUser1384831 的表中。下面的查询返回此结果集。

Name  Home Address Special ID special_tag Occurrences
----  ------------ ---------- ----------- -----------
Frank 152414       aTRF342    TRF                   3
Jane  4342342      rRFC432    RFC                   1
Mary  423432       xTRF353    TRF                   3
James 32111111     tLZQ399    LZQ                   1
May   4302443      3TRF322    TRF                   3

虽然您的问题有vba标记,但您不需要使用VBA过程。您可以使用SQL和Mid()函数来完成。

SELECT
    base.[Name],
    base.[Home Address],
    base.[Special ID],
    base.special_tag,
    tag_count.Occurrences
FROM
        (
            SELECT
                [Name],
                [Home Address],
                [Special ID],
                Mid([Special ID],2,3) AS special_tag
            FROM tblUser1384831
        ) AS base
    INNER JOIN
        (
            SELECT
                Mid([Special ID],2,3) AS special_tag,
                Count(*) AS Occurrences
            FROM tblUser1384831
            GROUP BY Mid([Special ID],2,3)
        ) AS tag_count
    ON base.special_tag = tag_count.special_tag;