选择按另一列

时间:2016-10-14 10:10:23

标签: sql oracle group-by

我一直在尝试计算每个" BESTELLING_ID"的总价。但我不知道从哪里开始。对bestelling_id进行分组似乎不起作用。在这里,您可以看到查询的结果,我想要计算每个" BESTELLING_ID"的计算值。我一直试图看看其他问题,但我似乎无法将答案与我的问题联系起来。我想用这个图表显示每个订单的个人订单价值。提前谢谢。

BESTELLING_ID   NAAM    ((P.PRIJS)*(R5.GEWICHT/100))
1              ui                    .25
1              pindakaas              1.7
22             Broccoli               1
22             Rijst                  1
select   b.bestelling_id ,p.naam, ((p.prijs)* (r5.gewicht/100)) 
   from  relation_6 r6
     join bestellingregel br on br.dieet_dieet_id= r6.dieet_id
     join bestelling b on br.bestelling_id=b.bestelling_id
     join relation_5 r5 on r5.recept_recept_id= r6.recept_id
     join product p on p.product_id = r5.product_product_id
   where p.winkelbeheer_winkelbeheer_id=2
   order by b.datum
;

2 个答案:

答案 0 :(得分:1)

使用此:

 select   
      a.col1 as BESTELLING_ID,sum(a.col3) as Total_Price
 from (      
  SELECT b.bestelling_id col1, p.naam  col2, ( (p.prijs) * (r5.gewicht / 100))  col3
    FROM relation_6 r6
         JOIN bestellingregel br ON br.dieet_dieet_id = r6.dieet_id
         JOIN bestelling b ON br.bestelling_id = b.bestelling_id
         JOIN relation_5 r5 ON r5.recept_recept_id = r6.recept_id
         JOIN product p ON p.product_id = r5.product_product_id
   WHERE p.winkelbeheer_winkelbeheer_id = 2
ORDER BY b.datum ) a
group by a.col1;

答案 1 :(得分:0)

您不需要子查询,只需group by

select b.bestelling_id, sum((p.prijs)* (r5.gewicht/100)) 
from relation_6 r6 join
     bestellingregel br
     on br.dieet_dieet_id = r6.dieet_id join
     bestelling b
     on br.bestelling_id = b.bestelling_id join
     relation_5 r5
     on r5.recept_recept_id = r6.recept_id join
     product p
     on p.product_id = r5.product_product_id
where p.winkelbeheer_winkelbeheer_id = 2
group by b.bestelling_id;

您可以在p.naamselect中加入group by,假设每bestselling_id只有一个名称。