平均计数错误结果

时间:2019-01-23 15:23:56

标签: sql sql-server

我需要得到所有维修得不到平均值的控制台中的BrandType,未维修的控制台类型也必须计入该平均值。

所以我从 console table

我将其加入此数据库中的Items tableartikel表)。

然后我将这些项目加入Repairs table中,因为我还需要尚未修复的控制台类型,不确定这是否正确。

现在,为了获得每种控制台类型的维修量,我对Repairs table(如图中的repaired_items_id)中的repareerd_artikel_id进行了计数,并按相同的方式将其分组列,然后取该计数的平均值。

这是我的语法,我也尝试了不同的组合方式,但结果总是错误的。

select merk,type from console c  join  artikel a on
a.CONSOLE_ID=c.CONSOLE_ID left join  REPARATIE r on
REPAREERD_ARTIKEL_ID=a.ARTIKEL_ID  group by MERK,TYPE

HAVING  (select avg(A.rcount) from  (select
count(repareerd_artikel_id) AS rcount from REPARATIE group by
REPAREERD_ARTIKEL_ID) A) < (select avg(A.rcount) from  (select
count(repareerd_artikel_id) AS rcount from REPARATIE group by
REPAREERD_ARTIKEL_ID) A)

然后我也尝试从计数开始。

HAVING  count(repareerd_artikel_id)<  (select avg(A.rcount) from 
(select count(repareerd_artikel_id) AS rcount from REPARATIE group by
REPAREERD_ARTIKEL_ID) A)

我不知道现在该怎么办,因此我们将不胜感激。

2 个答案:

答案 0 :(得分:0)

使用查询(并且不检查图像,因为从SO导航到图像很麻烦),我认为您可以执行以下操作,将问题分为三个阶段:

计算每个控制台的维修次数,包括零;

计算所有控制台的平均维修次数;

列出所有低于平均水平的维修。

WITH AllRepairs AS (
    SELECT 
        merk,
        [type],
        ISNULL(COUNT(r.repareerd_artikel_id), 0) AS repairs
    FROM 
        console c  
        INNER JOIN artikel a ON a.CONSOLE_ID = c.CONSOLE_ID 
        LEFT JOIN REPARATIE r ON r.REPAREERD_ARTIKEL_ID = a.ARTIKEL_ID  
    GROUP BY 
        merk,
        [type]),
AverageRepairs AS (
    SELECT
        AVG(repairs) AS average_repairs
    FROM
        AllRepairs)
SELECT
    a.*
FROM
    AllRepairs a 
    CROSS JOIN AverageRepairs ar
WHERE
    a.repairs < ar.average_repairs
ORDER BY
    a.repairs;

您可能要担心将整数与小数进行比较?例如,如果您的平均维修次数为2.9,则只有2以下的维修才算为低于平均维修。我想那可能就是您想要的?

答案 1 :(得分:0)

我查看您的信息的方式看起来像您的项目表中的项目没有任何维修。因此,我为一项添加了2个维修,并为另一项添加了1个维修。在我的查询中,我得出所有11个Merk / Types的平均值为1.5。然后,我将其与每个维修的次数进行比较。结果中包含2次修理的记录。

Create Table #console
(
Console_Id Int,
Merk Varchar(25),
Type VarChar(25),
Kleur VarChar(10),
Jaar_Uitgave Int,
Maat VarChar(10)
)
Insert Into #console Values
(1,'Sony 1','PS4 Slim','Wit',2016,'Slim'),
(2,'Microsoft','XBox','Beige',2004,'Port'),
(3,'Microsoft','XBox 360','Zwart',2011,'Pro'),
(4,'Microsoft','XBox One','Wit',2014,'Pro'),
(5,'Microsoft','XBox One X','Wit',2017,'Pro'),
(6,'Nintendo','NES Classic Edition','Wit',2016,'XL'),
(7,'Nintendo','Switch','Wit',2017,'XL'),
(8,'Nintendo','WII','Wit',2011,'Slim'),
(9,'Nintendo','WII Mini','Wit',2015,'XL'),
(10,'Nintendo','WII U','Wit',2013,'Slim'),
(11,'Sony','PS3','Wit',2013,'Port')

Create Table #Items
(
Artikel_ID Int,
BarCode VarChar(20),
Prijs Float,
Prijs_Per_D Float,
Spel_Of_Console VarChar(25),
Spel_ID Int,
Console_Id Int
)
Insert Into #Items Values
(301,'10000008',300.00,11.00,'Console',Null,3),
(302,'10000017',400.00,15.00,'Console',Null,4),
(303,'10000026',270.00,9.00,'Console',Null,9),
(304,'10000035',200.00,5.00,'Console',Null,6),
(305,'10000044',200.00,5.00,'Console',Null,11),
(306,'10000053',300.00,11.00,'Console',Null,12),
(307,'10000023',60.00,2.00,'Spel',15,Null),
(308,'10000242',36.00,2.00,'Spel',16,Null),
(309,'10000278',35.00,2.00,'Spel',21,Null),
(310,'10000107',66.00,4.00,'Spel',36,Null),
(311,'10000215',45.00,3.00,'Spel',40,Null)

Create Table #Repairs
(
Medewerker_Id Int,
Repareerd_Artikel_Id Int,
Schadenummer Int,
Huurovereenkomst_Id Int,
datum_Gereed DateTime,
Kosten Float,
Reparatiestatus VarChar(25)
)
Insert Into #Repairs Values
(1,259,7,12,'2017-08-03 00:00:00',112.00,'GEREED'),
(2,260,9,14,'2016-09-29 00:00:00',84.00,'GEREED'),
(3,288,19,28,'2017-04-09 00:00:00',96.00,'GEREED'),
(4,292,21,30,'2018-01-27 00:00:00',110.00,'GEREED'),
(5,283,16,24,'2015-12-29 00:00:00',103.00,'GEREED'),
(6,245,1,2,'2017-01-31 00:00:00',160.00,'GEREED'),
(7,245,2,3,'2018-01-18 00:00:00',120.00,'GEREED'),
(8,275,11,19,'2016-04-15 00:00:00',75.00,'GEREED'),
(9,276,12,20,'2015-08-25 00:00:00',174.00,'GEREED'),
(10,283,15,23,'2014-06-10 00:00:00',74.00,'GEREED'),
(11,297,21,34,'2014-07-17 00:00:00',96.00,'GEREED')

Insert Into #Repairs Values
(14,305,21,34,'2014-07-25 00:00:00',96.00,'GEREED'),
(12,301,21,34,'2014-07-17 00:00:00',96.00,'GEREED'),
(13,301,21,34,'2014-07-25 00:00:00',96.00,'GEREED')

查询

;With cte As
(
   select 
      c.Merk, c.Type,
      Count(r.REPAREERD_ARTIKEL_ID) As cnt
   from 
      #console c Left join 
      #Items a on a.CONSOLE_ID=c.CONSOLE_ID left join 
      #Repairs r on r.REPAREERD_ARTIKEL_ID=a.ARTIKEL_ID 
   group by 
      c.merk, c.type
)
Select 
   *, 
   (Select Count(*) As totrecs From cte) As cntRecs ,
   (Select avg(Cast(cte.cnt As Float)) As avgrecs From cte Where cte.cnt > 0) as avgrecs
From 
   cte
Where cte.cnt > (Select avg(Cast(cte.cnt As Float)) As avgrecs From cte Where cte.cnt > 0)

结果:

Merk        Type        cnt cntRecs avgrecs
Microsoft   XBox 360    2       11  1.5