ms-access - 分组select语句

时间:2010-07-01 19:03:12

标签: sql ms-access

这是查询:

SELECT [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)],Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [CountOf1 0 Preanalytical (Before Testing)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0

UNION SELECT [Lab Occurrence Form].[2 0 Analytical (Testing Phase)], Count([Lab Occurrence Form].[2 0 Analytical (Testing Phase)]) AS [CountOf2 0 Analytical (Testing Phase)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[2 0 Analytical (Testing Phase)]
HAVING Count([Lab Occurrence Form].[2 0 Analytical (Testing Phase)])<>0

union

SELECT  [Lab Occurrence Form].[3 0 Postanalytical ( After Testing)], Count([Lab Occurrence Form].[3 0 Postanalytical ( After Testing)]) AS [CountOf3 0 Postanalytical ( After Testing)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY  [Lab Occurrence Form].[3 0 Postanalytical ( After Testing)]
HAVING Count([Lab Occurrence Form].[3 0 Postanalytical ( After Testing)])<>0

UNION SELECT [Lab Occurrence Form].[4 0 Other], Count([Lab Occurrence Form].[4 0 Other]) AS [CountOf4 0 Other]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between [Forms]![Meeting_Reasons_Frequency]![Text4] And [Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[4 0 Other]
HAVING Count([Lab Occurrence Form].[4 0 Other])<>0
ORDER BY 1, 2;

它生成的内容如下所示:

1 0 Preanalytical (Before Testing)  CountOf1 0 Preanalytical (Before Testing)
1.11 Other-reject per practice  1
1.11 Other-req form marked Validity Only    1
1.11 Other-req form only, no specimen   3
1.11 Other-spec sent by mistake 1
1.11 Other-Test Req marked validity only    1
1.11 Other-validity only    3
1.11 Other-validity only-TP updated 1
1.3 QNS-Quantity Not Sufficient 103
1.4 Tests Missed/ Wrong Test Ordered    1
1.4 Tests Missed/ Wrong Test Ordered & 1.11 Other-data entry-dup tests  2
1.4 Tests Missed/Wrong Test Ordered 3
1.5 Patient Info. entered Incorrectly   1
1.6 Test Requisition Missing    22
1.6 Test Requisition Missing & 1.7 Specimen Lost    2
1.6 Test Requisition Missing-CSRM   1
1.8 Specimen not handled/processed correctly & 1.10 Operator Error(?)   1
1.8 Specimen not handled/processed correctly,1.10 Operator Error & 1.11 Other-no THC INSD recovery  1
1.9 QNS- Specimen Spilled in transit    88
2.1 Delay in Testing/resulting  1
2.1 Delay in Testing/resulting & 2.22 Instrument Problem    4

我想按数字对结果进行分组和总结

而不是拥有一堆1.11其他人,我想总结一下并得到:

1.11 11
1.3 103
1.4 4

所以我想我需要做的是选择第一列的前4个字符,然后按第一列分组,并在第二列上进行求和?请帮我这个选择声明

2 个答案:

答案 0 :(得分:1)

使用Val()

?val("1.11 Other-validity only-TP updated")

 1.11 

也适用于查询。

答案 1 :(得分:1)

也许:

SELECT left(TestType, 4), COUNT(*) FROM 
(SELECT [Lab Occurrence Form].[1 0 Preanalytical Before Testing)] As TestType,
Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [CountOf1 0     Preanalytical (Before Testing)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between
[Forms]![Meeting_Reasons_Frequency]![Text4] And 
[Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]
HAVING Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])<>0
UNION
SELECT [Lab Occurrence Form].[2 0 Analytical (Testing Phase)],
Count([Lab Occurrence Form].[2 0 Analytical (Testing Phase)]) AS 
[CountOf2 0 Analytical (Testing Phase)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between
[Forms]![Meeting_Reasons_Frequency]![Text4] And 
[Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[2 0 Analytical (Testing Phase)]
HAVING Count([Lab Occurrence Form].[2 0 Analytical (Testing Phase)])<>0
union
SELECT  [Lab Occurrence Form].[3 0 Postanalytical ( After Testing)], 
Count([Lab Occurrence Form].[3 0 Postanalytical ( After Testing)]) AS 
[CountOf3 0 Postanalytical ( After Testing)]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between 
[Forms]![Meeting_Reasons_Frequency]![Text4] And 
[Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY  [Lab Occurrence Form].[3 0 Postanalytical ( After Testing)]
HAVING Count([Lab Occurrence Form].[3 0 Postanalytical ( After Testing)])<>0
UNION 
SELECT [Lab Occurrence Form].[4 0 Other], Count([Lab Occurrence Form].[4 0 Other]) 
AS [CountOf4 0 Other]
FROM [Lab Occurrence Form]
WHERE ((([Lab Occurrence Form].[Occurrence Date]) Between  
[Forms]![Meeting_Reasons_Frequency]![Text4] And 
[Forms]![Meeting_Reasons_Frequency]![Text2]))
GROUP BY [Lab Occurrence Form].[4 0 Other]
HAVING Count([Lab Occurrence Form].[4 0 Other])<>0) A
GROUP BY left(TestType, 4)