簡體   English   中英

如何編寫一個SQL查詢以查找超過平均值的百分比?

[英]How to write a SQL query for finding more than percent average?

我有一個包含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之間獲得total_sales。

您是否需要以下內容:

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');

希望它的幫助

這對我來說還不是很清楚,但也許是這樣嗎?

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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM