找到项目列表中的平均值

时间:2018-02-13 20:17:51

标签: sql sql-server database

我试图找到平均价格,然后输出低于每个型号的平均价格的汽车。请有人帮助我,我真的很挣扎。我的主表carversions包含主要细节。

我已经成功获得了一款车型低于平均价格的数据,但我如何处理多种型号(可能没有硬编码)?这是我用于一个模型的查询

SELECT
    carversion.DateAdded,
    article.id 'ArticleId',
    carversion.VERSION 'Version',
    Article.DaysOld,
    carmake.name 'Make',
    carmodel.name 'Model',
    carversion.YEAR 'Year',
    carversion.Mileage,
    ('£' + LTRIM(STR(carversion.Price))) AS 'Price',
    carversion.FuelType,
    carversion.SellerType,
    carversion.Title,
    carversion.Description,
    carversion.Location,
    Article.Link,
    article.Thumbnail
FROM
    carversion
    JOIN Article ON Article.Id = carversion.ArticleId
    JOIN carmodel ON carmodel.id = article.CarModelId
    JOIN carmake ON carmake.id = carmodel.carmakeid
WHERE
    carmodel.name = 'a4'
    AND mileage < 60000
    AND price < (
        SELECT
            AVG(av.Price) AS 'Avg Price'
        FROM
            carversion av
            JOIN Article a ON a.Id = av.ArticleId
            JOIN carmodel mo ON mo.id = a.CarModelId
            JOIN carmake ma ON ma.id = mo.carmakeid
        WHERE
            mo.name = 'a4'
            AND mileage < 60000
    )
ORDER BY
    Article.daysold ASC,
    carversion.dateadded DESC,
    carversion.YEAR DESC,
    carversion.Price ASC,
    carversion.mileage ASC,
    carversion.VERSION DESC;

请有人帮我做多个模特。你可以看到我刚刚在&#39; a4&#39;模型,但我怎么能做多个?

汽车制造表

Make_ID|Name
    1  Audi
    2 BMW
    3 Vauxhall

汽车模型表

    ID|Name|Make_ID
    1  A4        1
    2 320si     2
    3 Astra     3

由于

2 个答案:

答案 0 :(得分:0)

我相信你需要这些方面的东西

SELECT  *
FROM carversion
JOIN Article ON Article.Id = carversion.ArticleId
JOIN carmodel ON carmodel.id = article.CarModelId
JOIN carmake ON carmake.id = carmodel.carmakeid 
JOIN
(
    SELECT  mo.name, 
            AVG(av.Price) AS AvgPrice
    FROM carversion av
    JOIN Article a ON a.Id = av.ArticleId
    JOIN carmodel mo ON mo.id = a.CarModelId
    JOIN carmake ma ON ma.id = mo.carmakeid
    WHERE mileage < 60000
    GROUP BY mo.name
) t ON carmodel.name = t.name and
       carversion.price < t.AvgPrice 
WHERE mileage < 60000

使用CTE可以避免代码重复

WITH CTEcars AS
(
    SELECT  * -- here you may need to list your columns explicitely, since there can be duplicit names
    FROM carversion
    JOIN Article ON Article.Id = carversion.ArticleId
    JOIN carmodel ON carmodel.id = article.CarModelId
    JOIN carmake ON carmake.id = carmodel.carmakeid     
    WHERE mileage < 60000
)
SELECT  *
FROM CTEcars c
JOIN
(
    SELECT name, AVG(price) AS AvgPrice
    FROM CTEcars
    GROUP BY mo.name
) t ON c.name = t.name and
       c.price < t.AvgPrice

答案 1 :(得分:0)

你可以试试这个

WITH Car_CTE (DateAdded, ArticleId, Version, DaysOld,Make,Model, [Year],Mileage, Price,
                FuelType, SellerType,Title, Description, Location, Link, Thumbnail )  
AS  
(  

   SELECT
    carversion.DateAdded,
    article.id 'ArticleId',
    carversion.VERSION 'Version',
    Article.DaysOld,
    carmake.name 'Make',
    carmodel.name 'Model',
    carversion.YEAR 'Year',
    carversion.Mileage,
    ('£' + LTRIM(STR(carversion.Price))) AS 'Price',
    carversion.FuelType,
    carversion.SellerType,
    carversion.Title,
    carversion.Description,
    carversion.Location,
    Article.Link,
    article.Thumbnail
FROM
    carversion
    JOIN Article ON Article.Id = carversion.ArticleId
    JOIN carmodel ON carmodel.id = article.CarModelId
    JOIN carmake ON carmake.id = carmodel.carmakeid
WHERE

    AND mileage < 60000
) ,
AveData AS(

        SELECT
            Model,
            AVG(Price) AS 'AvgPrice'
        FROM Car_CTE
        GROUP BY Model
) 

SELECT
    ct.*
FROM Car_CTE  AS ct INNER JOIN AveData  AS ad ON ad.Model= ct.Model
WHERE ct.Price < ad.AvgPrice