我有一个包含customer_id,customer_name,month_day,month,total_sales的表。数据类似于以下方式
customer_id customer_name month_day month total_sales
1 ravi 2 Feb 479892.901
2 arun 7 Aug 889390.709
3 goutham 6 Sep 791831.561
4 naveen 8 Sep 797413.558
此处total_sales的平均值为739632.18,其5%为36981.6,总数为776613.78。我想在9月份将这个总数介于776613.78和739632.18之间。
答案 0 :(得分:0)
你需要这样的东西:
SELECT customer_id FROM a
WHERE month_day = 6 and month = 'Apr' --Date and Month filter
and total_sales > (SELECT (AVG(total_sales)*1.05) FROM a WHERE month_day = 6 and month = 'Apr');
我有点困惑,为什么当这是销售表时你没有相同的customer_id
这是一个查询,以防这是customer_id
不是pk
SELECT customer_id FROM a
WHERE month_day = 6 and month = 'Apr'
GROUP BY customer_id
HAVING SUM(total_sales) > (SELECT (AVG(total_sales)*1.05) FROM a WHERE month_day = 6 and month = 'Apr');
希望得到帮助
答案 1 :(得分:0)
这对我来说不是很清楚,但也许这个?
SELECT *
FROM sales,
(SELECT avg(total_sales) AS avgval
FROM sales
WHERE month = 'Sep') tabavg
WHERE month = 'Sep'
AND day = '1'
AND total_sales > tabavg.avgval
AND total_sales < tabavg.avgval * 1.05;