2013-08-08 3 views
0

우리 팀이 우리의 모든 요청을 기록하는 데 사용하는 VBA로 코딩 한 Access DB가 있습니다. 다른 기준으로 단일 테이블에서 여러 개수를 가져 오는 쿼리를 만들고 싶습니다. 테이블의 예는 다음과 같습니다하나의 테이블에서 여러 개의 계수와 다른 기준으로 SQL 쿼리 생성

Table TLogging 
    logTypeId - Int [ this is 0 - 5 ] 
    isResolved - Boolean [ this is stored as -1 for true, 0 for false ] 

나는 다음과 같이하는 것이다 출력 뭔가 쿼리를 만들려면 :

Count of LogType1 where isResolved = True 
    Count of LogType1 where isResolved = False 
    Count of LogType1 where isResolved = True OR isResolved = False 

그리고에 대해 동일합니다 :

Resolved, LogType1, LogType2 
    True  101  39 
    False  49  104 
    All  150  144 

그래서 내가 원하는 LogType2.

저는 이미 SO 및 다른 포럼을 검색했지만 해결 방법을 찾지 못했습니다. 또한 Access VBA에서 CASE WHEN이 작동하지 않습니다.

미리 도움 주셔서 감사합니다.

답변

1

이 같은 시작할 수 있습니다 : 그것은 당신이 원하는대로 같은 결과에 대해 제공하지만, 전치

SELECT logTypeId, Count(logTypeId) AS nbTotal, -1 * Sum(isResolved) AS nbTrue, Count(logTypeId) + Sum(isResolved) As nbFalse 
FROM TLogging 
GROUP BY logTypeId; 

.

편집 :하거나, 그것은 추한하지만 난 당신이

SELECT 'True' As Resolved, t0.nb As LogType0, t1.nb As LogType1, t2.nb As LogType2, t3.nb As LogType3, t4.nb As LogType4, t5.nb As LogType5 
FROM (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 0) AS t0 
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 1) AS t1 
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 2) AS t2 
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 3) AS t3 
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 4) AS t4 
    , (Select -1 * Sum(isResolved) As nb From TLogging Where logTypeId = 5) AS t5 
Union All 
SELECT 'False' As Resolved, t0.nb As LogType0, t1.nb As LogType1, t2.nb As LogType2, t3.nb As LogType3, t4.nb As LogType4, t5.nb As LogType5 
FROM (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 0) AS t0 
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 1) AS t1 
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 2) AS t2 
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 3) AS t3 
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 4) AS t4 
    , (Select Count(*) + Sum(isResolved) As nb From TLogging Where logTypeId = 5) AS t5 
Union All 
SELECT 'All' As Resolved, t0.nb As LogType0, t1.nb As LogType1, t2.nb As LogType2, t3.nb As LogType3, t4.nb As LogType4, t5.nb As LogType5 
FROM (Select Count(*) As nb From TLogging Where logTypeId = 0) AS t0 
    , (Select Count(*) As nb From TLogging Where logTypeId = 1) AS t1 
    , (Select Count(*) As nb From TLogging Where logTypeId = 2) AS t2 
    , (Select Count(*) As nb From TLogging Where logTypeId = 3) AS t3 
    , (Select Count(*) As nb From TLogging Where logTypeId = 4) AS t4 
    , (Select Count(*) As nb From TLogging Where logTypeId = 5) AS t5 
; 
+0

하이 진을 원하는 정확히 결과를 제공합니다 생각합니다. 대답을 주셔서 감사합니다, 그러나 이것은 단순히 모든 것을 여러 컬럼에 집어 넣기 때문에 나의 필요를 충족시키지 못합니다. – cardycakes

+0

어떨까요 (편집 참조)? –

+0

예, 그렇습니다. 고마워. 진. – cardycakes

관련 문제