Я пытаюсь запустить следующую инструкцию SQL:
SELECT
q1.ORDERID, q1.LINE, q1.HOURS,
q1.HOURS / (
SELECT COUNT(*) FROM Query3 q2 WHERE q2.ORDERID = q1.ORDERID
)
FROM Query3 q1;
Но запрос вызывает ошибку 3072, описанную здесь .
Погуглил по поводу вышеуказанной ошибки, я думаю, это потому, что сравнение между разными типами (или что-то еще я не вижу). Итак, играя с запросом, я обнаружил, что следующий запрос выполняется правильно:
SELECT
q1.ORDERID, q1.LINE, q1.HOURS,
q1.HOURS / (
SELECT COUNT(*) FROM Query3 q2 WHERE q2.ORDERID = "4000"
)
FROM Query3 q1;
но следующий не делает (поднять ошибку в заголовке):
SELECT
q1.ORDERID, q1.LINE, q1.HOURS,
q1.HOURS / (
SELECT COUNT(*) FROM Query3 q2 WHERE "4000" = q1.ORDERID
) AS Expr1
FROM Query3 q1;
Перед этим советом я попытался преобразовать значения подзапроса следующим образом:
CLng(Nz(q1.HOURS)) /
(SELECT COUNT(*) FROM Query3 q2 WHERE CStr(Nz(q2.ORDERID), "")) = CStr(Nz(q2.ORDERID, "")))
Но мне не повезло ...
ORDERID и LINE имеют строковые типы; ЧАС имеет целочисленный тип. Средой, в которой выполняется запрос SQL, является Access 2007.
Query3 получен из Query2, который получен из Query1. Многие таблицы участвуют. Я думаю, что проблема вызвана сложной структурой базы данных; Я попытался выполнить эти запросы на более простой базе данных, и все работает правильно.
Что может вызвать эту ошибку?
Вот определение Query3:
SELECT
[Qry REQUESTS ALL Authorized].Site,
[Qry RDA and ORDERS].[Work Centre] AS [PO Work Center],
[Qry RDA and ORDERS].[Work Centre Descr],
[Qry RDA and ORDERS].DS_WORK_CENTER_MAIN,
[Qry RDA and ORDERS].[ID SHAREPOINT] AS [SHAREPOINT ID],
[Qry RDA and ORDERS].[QT Ordinamento] AS [PO QT Ordinam],
[Qry RDA and ORDERS].[BUDGET ACTIVITY] AS [PO BDG Activ],
[Qry RDA and ORDERS].RDA,
[Qry RDA and ORDERS].[RDA Line],
[Qry RDA and ORDERS].[PURCHASE ORDER],
[Qry RDA and ORDERS].[PO Line],
[Qry RDA and ORDERS].RESOURCE AS [PO Resource],
[Qry RDA and ORDERS].[EFFECTIVE START DATE],
[Qry RDA and ORDERS].[EFFECTIVE FINISH DATE],
[Qry RDA and ORDERS].[PO LINE HOURS],
[Qry Actual].Booking_Number AS [Act BookNum],
Sum([Qry Actual].ACTUAL_HOURS) AS [Act Hours],
IIf([PO LINE HOURS] IS NOT NULL,[Remaining],[PO LINE HOURS]-[Remaining]) AS [Remaining Hours],
[PO LINE HOURS]-[Act Hours] AS Remaining,
[Qry REQUESTS ALL Authorized].Program,
[Qry REQUESTS ALL Authorized].[Ord Progr],
[Qry REQUESTS ALL Authorized].[Main Program],
[Qry REQUESTS ALL Authorized].[Ord Main Progr],
[Qry RDA and ORDERS].ORD_WCM, [Qry RDA and ORDERS].ORD_WCS,
[Qry RDA and ORDERS].DS_WORK_CENTER_SYSTEM,
[Qry REQUESTS ALL Authorized].[Work Centre] AS [Author Work Center],
[Qry REQUESTS ALL Authorized].QUARTER,
[Qry REQUESTS ALL Authorized].Year,
[Qry REQUESTS ALL Authorized].ISSUE,
[Qry RDA and ORDERS].Supplier AS [PO Supplier],
Sum([Qry RDA and ORDERS].[PO JS]) AS [PO JS],
Sum([Qry RDA and ORDERS].[PO WP]) AS [PO WP],
[Qry RDA and ORDERS].[PO WP] AS [Work Package],
[Qry RDA and ORDERS].[PO JS] AS [Job Shopper],
[Qry Actual].BudgetActivity AS Actual_BDG_Act
FROM
[Qry REQUESTS ALL Authorized] LEFT JOIN
(
[Qry RDA and ORDERS] LEFT JOIN [Qry Actual] ON ([Qry RDA and ORDERS].[PURCHASE ORDER] = [Qry Actual].PO_Code) AND ([Qry RDA and ORDERS].[PO Line] = [Qry Actual].PO_Row)
) ON [Qry REQUESTS ALL Authorized].[SHAREPOINT ID] = [Qry RDA and ORDERS].[ID SHAREPOINT]
GROUP BY
[Qry REQUESTS ALL Authorized].Site,
[Qry RDA and ORDERS].[Work Centre],
[Qry RDA and ORDERS].[Work Centre Descr],
[Qry RDA and ORDERS].DS_WORK_CENTER_MAIN,
[Qry RDA and ORDERS].[ID SHAREPOINT],
[Qry RDA and ORDERS].[QT Ordinamento],
[Qry RDA and ORDERS].[BUDGET ACTIVITY],
[Qry RDA and ORDERS].RDA,
[Qry RDA and ORDERS].[RDA Line],
[Qry RDA and ORDERS].[PURCHASE ORDER],
[Qry RDA and ORDERS].[PO Line],
[Qry RDA and ORDERS].RESOURCE,
[Qry RDA and ORDERS].[EFFECTIVE START DATE],
[Qry RDA and ORDERS].[EFFECTIVE FINISH DATE],
[Qry RDA and ORDERS].[PO LINE HOURS],
[Qry Actual].Booking_Number,
[Qry REQUESTS ALL Authorized].Program,
[Qry REQUESTS ALL Authorized].[Ord Progr],
[Qry REQUESTS ALL Authorized].[Main Program],
[Qry REQUESTS ALL Authorized].[Ord Main Progr],
[Qry RDA and ORDERS].ORD_WCM,
[Qry RDA and ORDERS].ORD_WCS,
[Qry RDA and ORDERS].DS_WORK_CENTER_SYSTEM,
[Qry REQUESTS ALL Authorized].[Work Centre],
[Qry REQUESTS ALL Authorized].QUARTER,
[Qry REQUESTS ALL Authorized].Year,
[Qry REQUESTS ALL Authorized].ISSUE,
[Qry RDA and ORDERS].Supplier,
[Qry RDA and ORDERS].[PO WP],
[Qry RDA and ORDERS].[PO JS],
[Qry Actual].BudgetActivity
HAVING ((([Qry RDA and ORDERS].[Work Centre]) Is Not Null))
ORDER BY
[Qry REQUESTS ALL Authorized].Site,
[Qry RDA and ORDERS].[Work Centre],
[Qry RDA and ORDERS].[ID SHAREPOINT];
Итак, интересные поля запроса поступают из [Qry RDA и ORDERS]. [ЗАКАЗ ПОКУПКИ] , [Qry RDA и ORDERS]. [PO Line] и [Qry RDA и ЗАКАЗЫ]. [PO LINE HOURS] .
Запрос Qry RDA и ORDERS определяется как
SELECT
ORDINI_RIGHE_ORDINE.[ID SHAREPOINT],
ORDINI.CdC AS [Work Centre],
Anag_CdC.DESCRIZIONE AS [Work Centre Descr],
ORDINI.FORNITORE AS Supplier,
ORDINI_RIGHE_ORDINE.RDA,
ORDINI_RIGHE_ORDINE.RIGA_RDA AS [RDA Line],
ORDINI.[PURCHASE ORDER],
ORDINI_RIGHE_ORDINE.[RIGA ORDINE] AS [PO Line],
ORDINI_RIGHE_ORDINE.[PO LINE HOURS],
Qry_Attivita.[QT Ordinamento],
ORDINI_RIGHE_ORDINE.[BUDGET ACTIVITY],
[HOURS VALUE EURO]+[HOURS VALUE POUNDS]+[TRAVEL COSTS EURO]+[OTHER COSTS EURO]+[TRAVEL COSTS POUNDS]+[OTHER COSTS POUNDS] AS ORDER_VALUE,
[HOURS VALUE EURO]+[HOURS VALUE POUNDS] AS HOURS_VALUE,
[TRAVEL COSTS EURO]+[OTHER COSTS EURO]+[TRAVEL COSTS POUNDS]+[OTHER COSTS POUNDS] AS OTHER_COSTS_VALUE,
ORDINI_RIGHE_ORDINE.[HOURS VALUE EURO],
ORDINI_RIGHE_ORDINE.[TRAVEL COSTS EURO],
ORDINI_RIGHE_ORDINE.[OTHER COSTS EURO],
[TRAVEL COSTS EURO]+[OTHER COSTS EURO] AS Other_Costs_Euro,
ORDINI_RIGHE_ORDINE.[HOURS VALUE POUNDS],
ORDINI_RIGHE_ORDINE.[TRAVEL COSTS POUNDS],
ORDINI_RIGHE_ORDINE.[OTHER COSTS POUNDS],
[TRAVEL COSTS POUNDS]+[OTHER COSTS POUNDS] AS Other_Costs_Pounds,
ORDINI_RIGHE_ORDINE.RESOURCE,
ORDINI_RIGHE_ORDINE.[EFFECTIVE START DATE],
ORDINI_RIGHE_ORDINE.[EFFECTIVE FINISH DATE],
ORDINI_RIGHE_ORDINE.[OPEN / CLOSE],
ORDINI_RIGHE_ORDINE.NOTES,
ORDINI.WORKPACKAGE AS [PO WP],
ORDINI.[JOB SHOPPER] AS [PO JS],
ORDINI.[RDA CHECKED],
ORDINI.[RDA AUTHORISED],
ORDINI.[ID RICHIESTA RIGA],
Qry_Work_Center.ORD_WCS,
Qry_Work_Center.DS_WORK_CENTER_SYSTEM,
Qry_Work_Center.ORD_WCM,
Qry_Work_Center.DS_WORK_CENTER_MAIN,
Qry_Work_Center.Site
FROM
Qry_Work_Center INNER JOIN (
Anag_CdC INNER JOIN (
(ORDINI INNER JOIN ORDINI_RIGHE_ORDINE ON ORDINI.RDA = ORDINI_RIGHE_ORDINE.RDA) INNER JOIN Qry_Attivita ON ORDINI_RIGHE_ORDINE.[BUDGET ACTIVITY] = Qry_Attivita.[Budget Activity]) ON Anag_CdC.CDC = ORDINI.CdC
) ON Qry_Work_Center.[Work Centre] = ORDINI.CdC
GROUP BY
ORDINI_RIGHE_ORDINE.[ID SHAREPOINT],
ORDINI.CdC,
Anag_CdC.DESCRIZIONE,
ORDINI.FORNITORE,
ORDINI_RIGHE_ORDINE.RDA,
ORDINI_RIGHE_ORDINE.RIGA_RDA,
ORDINI.[PURCHASE ORDER],
ORDINI_RIGHE_ORDINE.[RIGA ORDINE],
ORDINI_RIGHE_ORDINE.[PO LINE HOURS],
Qry_Attivita.[QT Ordinamento],
ORDINI_RIGHE_ORDINE.[BUDGET ACTIVITY],
[HOURS VALUE EURO]+[HOURS VALUE POUNDS]+[TRAVEL COSTS EURO]+[OTHER COSTS EURO]+[TRAVEL COSTS POUNDS]+[OTHER COSTS POUNDS],
[HOURS VALUE EURO]+[HOURS VALUE POUNDS],
[TRAVEL COSTS EURO]+[OTHER COSTS EURO]+[TRAVEL COSTS POUNDS]+[OTHER COSTS POUNDS], ORDINI_RIGHE_ORDINE.[HOURS VALUE EURO],
ORDINI_RIGHE_ORDINE.[TRAVEL COSTS EURO],
ORDINI_RIGHE_ORDINE.[OTHER COSTS EURO],
[TRAVEL COSTS EURO]+[OTHER COSTS EURO],
ORDINI_RIGHE_ORDINE.[HOURS VALUE POUNDS],
ORDINI_RIGHE_ORDINE.[TRAVEL COSTS POUNDS],
ORDINI_RIGHE_ORDINE.[OTHER COSTS POUNDS],
[TRAVEL COSTS POUNDS]+[OTHER COSTS POUNDS],
ORDINI_RIGHE_ORDINE.RESOURCE,
ORDINI_RIGHE_ORDINE.[EFFECTIVE START DATE],
ORDINI_RIGHE_ORDINE.[EFFECTIVE FINISH DATE],
ORDINI_RIGHE_ORDINE.[OPEN / CLOSE],
ORDINI_RIGHE_ORDINE.NOTES,
ORDINI.WORKPACKAGE, ORDINI.[JOB SHOPPER],
ORDINI.[RDA CHECKED],
ORDINI.[RDA AUTHORISED],
ORDINI.[ID RICHIESTA RIGA],
Qry_Work_Center.ORD_WCS,
Qry_Work_Center.DS_WORK_CENTER_SYSTEM,
Qry_Work_Center.ORD_WCM,
Qry_Work_Center.DS_WORK_CENTER_MAIN,
Qry_Work_Center.Site
ORDER BY
ORDINI_RIGHE_ORDINE.[ID SHAREPOINT],
ORDINI.CdC;
Итак, исходные таблицы имеют тип ORDINI и ORDINI RIGHE , которые определены как String , допускающие значения NULL.