访问:与计数和联合的复杂总和

时间:2014-10-06 09:11:21

标签: ms-access-2010

我不得不总结来自不同表的两个计算值的问题:

表格

  • T_AACDB_Daily_Status(包含STATUS_ID,COM​​PLETION_DATE,DATE和APP_ID)
  • T_AACDB_Service_Costs(包含SERVICE_COSTS_ID和COSTS)
  • T_AACDB_Services(包含SERVICE_ID,DATE和QTY)

T_AACDB_Daily_Status.STATUS_ID = T_AACDB_Service_Costs.SERVICE_COSTS_ID = T_AACDB_Services.SERVICE_ID

需要进行计算:

  • 从T_AACDB_Daily_Status计算STATUS_ID作为CountFromDailyStatus
  • 将CountFromDailyStatus与T_AACDB_Service_Costs的成本相乘作为SUM1
  • 将T_AACDB_Service_Costs的成本与T_AACDB_Services.QTY相乘作为SUM2 最后总结SUM2 + SUM2

查询:

  • 第一个查询部分计算特定时间段内的特定状态
  • 第二个查询部分将成本与关联的service_id
  • 相乘

到目前为止,这是我的查询:

SELECT 
      SUM(t1.[CountFromDailyStatus]* tC.[COSTS]) AS SUM1
FROM 
    (
    T_AACDB_Service_Costs AS tC LEFT JOIN 
          (
          SELECT 
          T_AACDB_Daily_Status.[APP_ID], 
          T_AACDB_Daily_Status.[COMPLETION_DATE], 
          T_AACDB_Daily_Status.[STATUS_ID], 
           COUNT(T_AACDB_Daily_Status.[STATUS_ID]) AS CountFromDailyStatus 
          FROM T_AACDB_Daily_Status 
          GROUP BY T_AACDB_Daily_Status.[APP_ID], T_AACDB_Daily_Status.[COMPLETION_DATE], T_AACDB_Daily_Status.[STATUS_ID]
          )  AS t1 
          ON tC.[SERVICE_COSTS_ID]=t1.[STATUS_ID]
    ) 

LEFT JOIN T_AACDB_Services AS tS 
ON tC.[SERVICE_COSTS_ID]=tS.[SERVICE_ID]

WHERE 
    (NOT(t1.COMPLETION_DATE) Is Null)     
    AND (t1.[COMPLETION_DATE]>=DATEVALUE([Startdatum]))      
    AND (t1.[COMPLETION_DATE]<=DATEVALUE([Enddatum]))
    AND t1.[STATUS_ID]  = 'Code-Review'     
    AND NOT EXISTS          
        (
        SELECT *          
        FROM T_AACDB_Daily_Status t2         
        WHERE t1.[APP_ID] = t2.[APP_ID]          
        AND (Not(t2.COMPLETION_DATE) Is Null)            
        AND (t2.[COMPLETION_DATE]>=DATEVALUE([Startdatum])) 
        AND (t2.[COMPLETION_DATE]<=DATEVALUE([Enddatum]))          
        AND t2.[STATUS_ID] = 'Deployment'         
        );

UNION 
SELECT 
SUM(tC.[COSTS]*tS.[QTY]) AS SUM2
FROM T_AACDB_Services AS tS INNER JOIN T_AACDB_Service_Costs AS tC ON tC.[SERVICE_COSTS_ID]=tS.[SERVICE_ID]
WHERE 
          tS.[SERVICE_ID]="PSEUDO" AND (tS.[Date]>=DATEVALUE([Startdatum])) And (tS.[Date]<=DATEVALUE([Enddatum]));

1 个答案:

答案 0 :(得分:0)

我能够自己解决这个问题:

SELECT 
SUM(Summe+gesamt2) AS gesamtTotal,
SUM(anzahlNeu+anzahlNeu2) AS anzahlTotal

FROM (
SELECT
    sum(t1.[AnzahlAusDailyStatus]* tC.[COSTS]) AS Summe, 
    sum(tC.[COSTS]*tS.[QTY]) AS gesamt2,
    COUNT(t1.[STATUS_ID]) AS anzahlNeu,
    SUM( tS.[QTY]+0)  AS anzahlNeu2

    FROM ((
           T_AACDB_Service_Costs tC 
             LEFT JOIN  (
                     SELECT 
                     T_AACDB_Daily_Status.[APP_ID] ,
                     T_AACDB_Daily_Status.[STATUS_ID] ,
                     T_AACDB_Daily_Status.[COMPLETION_DATE] ,
                     COUNT(T_AACDB_Daily_Status.[STATUS_ID]) AS AnzahlAusDailyStatus 
                     FROM T_AACDB_Daily_Status 
                     GROUP BY   
                     T_AACDB_Daily_Status.[APP_ID] , 
                     T_AACDB_Daily_Status.[STATUS_ID] ,
                      T_AACDB_Daily_Status.[COMPLETION_DATE]
                     )  AS t1 
          ON tC.[SERVICE_COSTS_ID]=t1.[STATUS_ID]
    ) 
    LEFT JOIN T_AACDB_Services tS ON tC.[SERVICE_COSTS_ID]= tS.[SERVICE_ID])

    WHERE 
    (NOT(t1.COMPLETION_DATE) Is Null)     
    AND (t1.[COMPLETION_DATE]>=DATEVALUE([Startdatum]))      
    AND (t1.[COMPLETION_DATE]<=DATEVALUE([Enddatum]))
    AND t1.[STATUS_ID]  = 'Code-Review'     
    AND NOT EXISTS          
        (
        SELECT *          
        FROM T_AACDB_Daily_Status t2         
        WHERE t1.[APP_ID] = t2.[APP_ID]          
        AND (Not(t2.COMPLETION_DATE) Is Null)            
        AND (t2.[COMPLETION_DATE]>=DATEVALUE([Startdatum])) 
        AND (t2.[COMPLETION_DATE]<=DATEVALUE([Enddatum]))          
        AND t2.[STATUS_ID] = 'Deployment'         
        )
    OR (((tS.SERVICE_ID)="PSEUDO-CR") AND ((tS.Date)>=DateValue([Startdatum]) And (tS.Date)    
    <=DateValue([Enddatum]))) 
)
;