我不得不总结来自不同表的两个计算值的问题:
表格
T_AACDB_Daily_Status.STATUS_ID = T_AACDB_Service_Costs.SERVICE_COSTS_ID = T_AACDB_Services.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]));
答案 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])))
)
;