访问:IIF语句和COUNT

时间:2010-06-30 18:30:47

标签: sql ms-access

这是我的代码,感谢Jim b:

SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 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)
ORDER BY Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) DESC;

它返回:

1.1 Specimen Mislabeled 159
1.3 QNS-Quantity Not Sufficient 84
1.9 QNS- Specimen Spilled in transit    72
1.6 Test Requisition Missing    17
1.11 Other  3
1.11 Other  3
1.1 Specimen Mislabeled-new ID # given  2
1.11 Other  2
1.11 Other  2
1.1 Specimen Mislabeled & 1.6 Test Requisition Missing  1
1.11 Other  1
1.11 Other  1
1.11 Other  1
1.11 Other  1
1.11 Other  1
? Nothing in comments portion of QuikLab    1
1.11 Other  1
1.11 Other  1
1.4 Tests Missed/ Wrong Test Ordered    1
1.4 Tests Missed/Wrong Test Ordered 1
1.6 Test Requisition Missing & 1.7 Specimen Lost    1
1.8 Specimen not handled/processed correctly & 1.10 Operator Error(?)   1
1.11 Other  1
1.11 Other  1

这正是我所需要的,但还有一件小事。我需要它计算'1.11其他'

我该怎么办?换句话说,这就是我需要的输出:

? Nothing in comments portion of QuikLab    1
1.1 Specimen Mislabeled 159
1.1 Specimen Mislabeled & 1.6 Test Requisition Missing  1
1.1 Specimen Mislabeled-new ID # given  2
1.11 Other  19
1.3 QNS-Quantity Not Sufficient 84
1.4 Tests Missed/ Wrong Test Ordered    1
1.4 Tests Missed/Wrong Test Ordered 1
1.6 Test Requisition Missing    17
1.6 Test Requisition Missing & 1.7 Specimen Lost    1
1.8 Specimen not handled/processed correctly & 1.10 Operator Error(?)   1
1.9 QNS- Specimen Spilled in transit    72

正如您所看到的,1.11 Other只出现一次,总数为19

JIM B建议:

Use your IIF statement all the way through your group by and having clauses – Jim B

但我不明白如何实现它。请帮忙!

3 个答案:

答案 0 :(得分:1)

您在列语句中使用的解决方案也需要包含在count函数中。因为您希望它根据修订的列数据而不是原始列数据进行计数。

答案 1 :(得分:1)

更多的sql server家伙,所以我不确定你是否可以这样做,但你试过了吗?

SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 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 IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])
HAVING ((Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]))<>0)
ORDER BY Count([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) DESC;

答案 2 :(得分:1)

也许是这样的?

SELECT IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)]) AS [Occurrence Code], Count([Lab Occurrence Form].[1 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 IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])
HAVING ((Count(IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])))<>0)
ORDER BY Count(IIf([Lab Occurrence Form].[1 0 Preanalytical (Before Testing)] Like '*1.11 Other*','1.11 Other',[Lab Occurrence Form].[1 0 Preanalytical (Before Testing)])) DESC;