我正在尝试运行以下SQL语句:
SELECT
q1.ORDERID, q1.LINE, q1.HOURS,
q1.HOURS / (
SELECT COUNT(*) FROM Query3 q2 WHERE q2.ORDERID = q1.ORDERID
)
FROM Query3 q1;
但查询导致错误3072,描述为here。
谷歌搜索上述错误,我认为这是因为不同类型之间的比较(或其他我看不到的东西)。因此,使用查询,我发现以下查询正确运行: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 有字符串类型; HOUR有整数类型。运行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和ORDERS]。[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值。
答案 0 :(得分:0)
SELECT q1.ORDERID,
q1.LINE,
q1.HOURS,
CDbl(q1.HOURS) /
( SELECT COUNT(*)
FROM Query3 q2
WHERE q2.ORDERID = q1.ORDERID, 0)
FROM Query3 q1;
我的假设是Hours
是一个整数&我假设COUNT(*)
将返回整数。除以整数的整数将返回一个整数。某些情况下的结果值无法容纳在整数变量中。
例如200/9
因此,使用CDbl
将小时数转换为加倍会强制查询以Double
数据类型返回结果。
这有帮助吗?