簡體   English   中英

選擇總和不同的列並按另一列分組

[英]select sum different columns grouped by another column

我一直在嘗試計算每個“ 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
;

用這個:

 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;

您不需要子查詢,只需按一個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;

假設每個bestselling_id只有一個名稱,則可以將p.naam包括在selectgroup by

暫無
暫無

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

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