使用SQL计算Access数据库列中的字符

时间:2013-11-10 03:49:52

标签: sql ms-access access-vba

我有下表

col1  col2  col3  col4
====  ====  ====  ====
1233  4566  ABCD  CDEF 
1233  4566  ACD1  CDEF
1233  4566  D1AF  CDEF

我需要计算col3中的字符数,因此从上表中的数据可以看出:

char  count
====  =====
A         3
B         1
C         2
D         3
F         1
1         2

这可以通过仅使用SQL来实现吗?

目前我正在考虑将参数传递给SQL查询并逐个计算字符然后求和,但是我还没有启动VBA部分,坦率地说不想这样做。

这是我目前的询问:

PARAMETERS X Long;
SELECT First(Mid(TABLE.col3,X,1)) AS [col3 Field], Count(Mid(TABLE.col3,X,1)) AS Dcount
FROM TEST
GROUP BY Mid(TABLE.col3,X,1)
HAVING (((Count(Mid([TABLE].[col3],[X],1)))>=1));

非常感谢创意和帮助,因为我通常不使用Access和SQL。

4 个答案:

答案 0 :(得分:1)

您可以使用Numbers表在纯Access SQL中完成任务。在这种情况下,Numbers表必须包含1到大于源数据中最长字符串的某个数字的整数值。在此示例中,要处理的字符串位于[CharacterData]:

CharacterList
-------------
GORD         
WAS          
HERE         

和[Numbers]表只是

 n
--
 1
 2
 3
 4
 5

如果我们使用交叉联接来提取字符(消除n超过Len(CharacterList)导致的任何空字符串)...

SELECT 
    Mid(cd.CharacterList, nb.n, 1) AS c 
FROM 
    CharacterData cd, 
    Numbers nb
WHERE Mid(cd.CharacterList, nb.n, 1) <> ""

......我们得到......

c
--
G
W
H
O
A
E
R
S
R
D
E

现在我们可以将它包装在聚合查询中

SELECT c AS Character, COUNT(*) AS CountOfCharacter
FROM
    (
        SELECT 
            Mid(cd.CharacterList, nb.n, 1) AS c 
        FROM 
            CharacterData cd, 
            Numbers nb
        WHERE Mid(cd.CharacterList, nb.n, 1) <> ""
    )
GROUP BY c

给了我们

Character  CountOfCharacter
---------  ----------------
A                         1
D                         1
E                         2
G                         1
H                         1
O                         1
R                         2
S                         1
W                         1

答案 1 :(得分:0)

知道colum3的固定长度为4,这个问题很容易。

假设有一个视图V,其中包含四列,每列包含第3列中的一个字符。

V(c1, c2, c3, c4)

不幸的是,我不熟悉Access特定的SQL,但这是您需要的通用SQL语句:

SELECT c, COUNT(*) FROM
(
SELECT c1 AS c FROM V
UNION ALL
SELECT c2 FROM V
UNION ALL
SELECT c3 FROM V
UNION ALL
SELECT c4 FROM V
)
GROUP BY c

答案 2 :(得分:0)

您不想考虑使用VBA,这是一种耻辱;你不需要像你想象的那样多:

Public charCounts As Dictionary

Sub LoadCounts(s As String)
    If charCounts Is Nothing Then Init
    Dim length As Integer, i As Variant
    length = Len(s)
    For i = 1 To length
        Dim currentChar As String
        currentChar = Mid(s, i, 1)
        If Not charCounts.Exists(currentChar) Then charCounts(currentChar) = 0
        charCounts(currentChar) = charCounts(currentChar) + 1
    Next
End Sub

Sub Init()
    Set charCounts = New Scripting.Dictionary
    charCounts.CompareMode = TextCompare 'for case-insensitive comparisons; otherwise use BinaryCompare
End Sub

然后,执行一次查询:

SELECT LoadCount(col3)
FROM Table1

最后,你读出了词典中的值:

Dim key As Variant
For Each key In charCounts
    Debug.Print key, charCounts(key)
Next

请注意,在查询执行之间,您必须调用Init来清除旧值。

答案 3 :(得分:-1)

请试试这个,,,我希望这会有效

with cte  as
(
    select row_number() over(order by (select null)) as i from Charactor_Count
)

 select substring( name, i, 1 ) as char, count(*) as count
      from Charactor_Count, cte
     where cte.i <= len(Charactor_Count.name)
     group by substring(name,i,1)
     order by substring(name,i,1)