计算一段时间内销售额高于平均销售额的商店

时间:2019-02-16 14:06:43

标签: sql sql-server

我想找到一个商店,他们的产品销售在特定时期内高于平均水平。到目前为止,这是我所做的

SELECT S.NAME, SUM([QTY] * [PRICE]) AS SALES, AVG(SALES) AS AVGSALES
FROM Invoice  INNER JOIN InvDetails ON Invoice.INVOICE_ID=InvDetails.INVOICE_ID
INNER JOIN Product ON InvDetails.PRODUCT_ID=Product.PRODUCT_ID
INNER JOIN Shop  ON Shop.Shop_ID=Invoice.Shop_ID
WHERE  SALES>AVGSALES  AND DATEOFInv BETWEEN '2010-01-01 08:56:01.490' AND '2010-01-06 06:51:31.867'
GROUP BY S.NAME 
ORDER BY SALES DESC

这是我收到的错误

  

信息207,第16级,状态1,第5行   无效的列名“ SALES”。

     

信息207,第16级,状态1,第5行   无效的列名“ AVGSALES”。

     

第207级状态1的1行   无效的列名“ SALES”。

1 个答案:

答案 0 :(得分:0)

请尝试创建一个外部块:

SELECT * FROM
(
    SELECT           
        S.NAME, 
        SUM([QTY] * [PRICE]) AS SALES, 
        AVG([QTY] * [PRICE]) AS AVGSALES
    FROM              
        Invoice  INNER JOIN InvDetails ON 
        Invoice.INVOICE_ID=InvDetails.INVOICE_ID
        INNER JOIN Product ON InvDetails.PRODUCT_ID=Product.PRODUCT_ID
        INNER JOIN Shop  ON Shop.Shop_ID=Invoice.Shop_ID
    WHERE
        DATEOFInv BETWEEN '2010-01-01 08:56:01.490' AND '2010-01-06 06:51:31.867'
    GROUP BY     S.NAME
)x
    WHERE  
    SALES>AVGSALES
ORDER BY     SALES DESC
  

消息207,级别16,状态1,第6行,无效的列名称“ SALES”。