我正在使用 Squirrel SQL,数据库在 Sybase 的 SQL Anywhere 12 中。我正在尝试构建一个报告,从多个不同的表中提取数据并将它们呈现在一个 1 x n 表中 - 不是最佳实践,但我唯一的选择能想到。
但这就是问题所在。我正在尝试使用 parta.Name = partb.Name 的简单 INNER JOIN 将 A 部分和 B 部分连接在一起。A 部分和 B 部分单独运行良好,但一旦 JOIN 发生,wham,聚合函数的无效使用。经过测试,我发现A部分是问题所在。代码如下 - 我取出了这组代码下面的所有内容,但它仍然以无效的聚合使用打击我。我不知道我做错了什么,特别是因为 A 部分和 B 部分都是独立工作的!有人有想法吗?
SELECT * FROM
(
SELECT
'Company A' AS MSPName
,
-- COUNT ORDERS MADE BY MSP COMPANIES AFTER THEIR ENROLL DATES -- COUNT ORDERS MADE BY MSP COMPANIES AFTER THEIR ENROLL DATES -- COUNT ORDERS MADE BY MSP COMPANIES AFTER THEIR ENROLL DATES
(
SELECT
COUNT(DISTINCT order_id) AS CountOrdersAfterEnroll
FROM
reporting.msp msp
INNER JOIN
iquser.dm_order_d orders
ON
msp.ClientID = orders.client_id
WHERE
-- right company
msp.ClientName /*TARGETING*/ LIKE 'Company A%'
AND
-- right time
((date_filled IS NOT NULL AND date_filled >= msp.EnrollDate)
-- filled it
OR
-- we didn't fill it
(date_filled IS NULL AND order_create_date >= msp.EnrollDate))
AND
order_status_id IN (2, 5, 8, 9, 10, 12)
-- these are the only ones we care about
) AS CountOrdersAfterEnroll
-- a lot of other similar code
(
SELECT
CASE
WHEN CountFilledOrdersAfterEnroll = 0
THEN NULL
ELSE
SUM(CASE
WHEN field_value = 'More than 20'
THEN 21.000
ELSE
CAST(field_value AS DECIMAL(12, 4)) --12 digits long, 4 behind the decimal mark. I'm not sure how this handles mantissa length retention, though.
END) * 1.0 -- for floats, JIC the DECIMAL cast didn't work out?
/ --DIVIDE BY
CountFilledOrdersAfterEnroll
END
FROM
-- Q&A
iquser.custom_field CF
LEFT JOIN
--answers
iquser.custom_field_value CFV
ON
CF.custom_field_id = CFV.custom_field_id
-- order details for these Q&A
LEFT JOIN
iquser.dm_order_d orders
ON
CFV.order_id = orders.order_id
-- now Q&A from ONLY the MSPs
-- right company
INNER JOIN
reporting.msp msp
ON
msp.ClientID = CF.client_id
WHERE
-- right company
msp.ClientName /*TARGETING*/ LIKE 'Company A%'
AND
-- right time
((date_filled IS NULL AND order_create_date >= msp.EnrollDate)
-- we didn't fill it
OR
-- we did fill it
(date_filled IS NOT NULL and date_filled >= msp.EnrollDate))
AND
CFV.field_value != 'zShared Services Only - SSNA'
AND
CF.field_name /*QUESTION TARGETING*/ LIKE 'How many candidates did we extend offers to after the client had selected%'
) AS CandidatesExtendedPerFilled
) parta
LEFT JOIN
(
SELECT
'Company A' AS MSPName
,
--other code of PartB
) partb
ON
parta.MSPName = partb.MSPName