我在Access中有一个表,我正在从Excel查询。我需要sql语句的帮助。 首先,我需要根据Where子句中指示的SampleType匹配条件过滤表。只有3个选项:“mel”,“col”或“lun”。最终目标是将选定SampleType的SampleID列中的不同记录数传递给变量,并将其输出到UserForm上。 SampleType列具有重复值,只需要计算不同的值。从我在网上找到的'COUNT(DISTINCT('+ column_name +'))可行,但我不知道如何使用它。
感谢您的提前帮助
Dim var6 As String
Dim var7 As String
Dim var8 As String
var6 = "mel"
var7 = “lun”
var8 = “col”
SQLwhere = "WHERE "
If CheckBox5 = True Then
SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & var6 & "%" & "' AND "
‘count a total district records in column SampleID.
End If
If CheckBox6 = True Then
SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & var7 & "%" & "' AND "
End If
If CheckBox7 = True Then
SQLwhere = SQLwhere & "[Table1].[SampleType] LIKE '" & var & "%" & "' AND "
End If
StrSql = "SELECT * FROM [Table1] "
'Remove the last AND applicable
If SQLwhere = "WHERE " Then
SQLwhere = ""
Else
SQLwhere = Left(SQLwhere, Len(SQLwhere) - 5)
End If
StrSql = StrSql & SQLwhere
答案 0 :(得分:0)
你的问题相当广泛,所以答案是通用的。与MS Access DISTINCT SQL的语法相关,它可能如下所示:
SELECT COUNT(*) AS UniqueRecordsCount FROM (SELECT DISTINCT [ColumnName] FROM [SampleTable]) AS T1
或简化如:
SELECT COUNT(*) AS UniqueRecordsCount FROM (SELECT DISTINCT [ColumnName] FROM [SampleTable])
与评论中的其他问题相对应,请参阅以下内容:
GetDistinctValue = rs("UniqueRecordsCount")
希望这可能会有所帮助。