我正在使用3个表,其中包含我的查询所需的内容。我需要返回给定销售日期范围内的年龄组,并显示每个年龄组的利润。这是我正在返回错误的查询:
#1241 - 操作数应包含1列
我似乎无法绕过这一个。
SELECT DealInfo.DeliveryDate, Coalesce(SUM(Commissions.GrossProfit),0) as GP, CASE
WHEN age < 16 THEN 'Under 16'
WHEN age BETWEEN 16 and 20 THEN '16 - 20'
WHEN age BETWEEN 21 and 25 THEN '21 - 25'
WHEN age BETWEEN 26 and 30 THEN '26 - 30'
WHEN age BETWEEN 31 and 35 THEN '31 - 35'
WHEN age BETWEEN 36 and 40 THEN '36 - 40'
WHEN age BETWEEN 41 and 45 THEN '41 - 45'
WHEN age BETWEEN 46 and 50 THEN '46 - 50'
WHEN age BETWEEN 51 and 55 THEN '51 - 55'
WHEN age > 55 THEN 'Over 55'
WHEN age IS NULL THEN 'NULL' END as age_range,
COUNT(*) AS RC FROM (SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age
FROM customers WHERE Customers.dob<>'0000-00-00' and Customers.server_id = '0000001412') as derived
GROUP BY age_range
ORDER BY age_range ASC
from DealInfo
LEFT JOIN Commissions on Commissions.Deal_ID=DealInfo.Deal_Number and Commissions.server_id=DealInfo.server_id
WHERE DealInfo.server_id = '0000001412' and DealInfo.Status=2 and Coalesce(Commissions.GrossProfit,0)>0 and
dealinfo.DeliveryDate BETWEEN '2017-01-01' AND '2017-12-31'
答案 0 :(得分:1)
SELECT DealInfo.DeliveryDate,
Coalesce(SUM(Commissions.GrossProfit),0) as GP,
(SELECT CASE WHEN age < 16 THEN 'Under 16'
WHEN age BETWEEN 16 and 20 THEN '16 - 20'
WHEN age BETWEEN 21 and 25 THEN '21 - 25'
WHEN age BETWEEN 26 and 30 THEN '26 - 30'
WHEN age BETWEEN 31 and 35 THEN '31 - 35'
WHEN age BETWEEN 36 and 40 THEN '36 - 40'
WHEN age BETWEEN 41 and 45 THEN '41 - 45'
WHEN age BETWEEN 46 and 50 THEN '46 - 50'
WHEN age BETWEEN 51 and 55 THEN '51 - 55'
WHEN age > 55 THEN 'Over 55'
WHEN age IS NULL THEN 'NULL' END as age_range,
COUNT(*) AS RC
FROM (SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age
FROM customers
WHERE Customers.dob<>'0000-00-00'
and Customers.server_id = '0000001412') as derived
GROUP BY age_range
ORDER BY age_range ASC)
from DealInfo
LEFT JOIN Commissions
on Commissions.Deal_ID = DealInfo.Deal_Number
and Commissions.server_id = DealInfo.server_id
WHERE DealInfo.server_id = '0000001412'
and DealInfo.Status=2
and Coalesce(Commissions.GrossProfit,0) > 0
您的标量子查询:
(SELECT CASE WHEN age < 16 THEN 'Under 16'
WHEN age BETWEEN 16 and 20 THEN '16 - 20'
WHEN age BETWEEN 21 and 25 THEN '21 - 25'
WHEN age BETWEEN 26 and 30 THEN '26 - 30'
WHEN age BETWEEN 31 and 35 THEN '31 - 35'
WHEN age BETWEEN 36 and 40 THEN '36 - 40'
WHEN age BETWEEN 41 and 45 THEN '41 - 45'
WHEN age BETWEEN 46 and 50 THEN '46 - 50'
WHEN age BETWEEN 51 and 55 THEN '51 - 55'
WHEN age > 55 THEN 'Over 55'
WHEN age IS NULL THEN 'NULL' END as age_range,
COUNT(*) AS RC
FROM (SELECT TIMESTAMPDIFF(YEAR, dob, CURDATE()) AS age
FROM customers
WHERE Customers.dob<>'0000-00-00'
and Customers.server_id = '0000001412') as derived
GROUP BY age_range
ORDER BY age_range ASC)
返回2列,它应该只返回一列。