我正在使用以下查询来使用左连接从两个表中获取多个列的总和。但是mysql显示错误“错误代码:1111。无效使用组功能”请指出我在哪里出错?
SELECT url as Domain,
A.advertiserid as advertiserid,
count(A.salesId) as Impressions,
Sum(IF(A.click1datetime IS NULL,0,1)) AS Clicks,
sum(count(B.order_id)) as Sales,
sum(sum(B.order_amount)) as SalesAmount,
FROM ClickRecords A
left join SalesDetail B
on A.salesId= B.salesId
where
A.advertiserid = 52 AND
datetime >= '2012-03-01' AND
datetime < '2012-03-02'
group by advertiserid, Domain
order by impressions desc;
答案 0 :(得分:1)
您不能将聚合函数用作其他聚合函数的参数(例如:sum(count(B.order_id))
)。
答案 1 :(得分:1)
如上所述,您无法嵌套聚合函数 - 即。 sum(sum(value))
无效。
我想你想要:
SELECT url as Domain,
A.advertiserid as advertiserid,
count(distinct A.salesId) as Impressions,
count(distinct case when A.click1datetime IS NOT NULL then A.salesId end)
AS Clicks,
count(B.order_id) as Sales,
sum(B.order_amount) as SalesAmount,
FROM ClickRecords A
left join SalesDetail B on A.salesId= B.salesId
where A.advertiserid = 52 AND
datetime >= '2012-03-01' AND
datetime < '2012-03-02'
group by advertiserid, Domain
order by impressions desc;
请注意,count(distinct ...)
用于ClickRecords值 - sum
或非独特count
根据非空记录的数量加起来值,这些记录可以乘以SalesDetail记录(每个ClickRecord有多个。)