2010-06-30 92 views
0

,這裏是我的代碼工作的感謝Jim B:訪問:IIF語句和COUNT

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 

但我不明白如何實現它。請幫忙!

回答

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; 
+0

非常感謝這個wroked! – 2010-06-30 18:40:45

1

您在列語句中使用的解決方案還需要包含在計數函數中。因爲您希望根據修訂的列數據而不是原始列數據進行計數。

+0

請讓我看看代碼謝謝你 – 2010-06-30 18:34:59

+0

是什麼意思? http://pastebin.com/siESpmUs它實際上是返回相同的東西,但沒有錯誤 – 2010-06-30 18:38:17

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; 
+0

非常感謝我的幫助 – 2010-06-30 18:54:04