需要在算术列上的SQL中使用分组依据

时间:2018-07-02 06:49:53

标签: sql oracle group-by

我想在MEDICINE_NAME列的以下查询中使用分组依据,但遇到错误,请任何人帮助我如何使用它。

SELECT m.doc_date "Date of Purchase",
         t.cost_unit "Purchase On",
         t.price_unit "Sold On",
         t.price_unit - t.cost_unit "Margin",
         CONCAT (
            ROUND ( ( (t.price_unit - t.cost_unit) * 100 / (t.cost_unit + 1)),
                   2),
            '%')
            "Profit",
         med.medicine_name,
         med.manufacture,
         v.vendor_name
    FROM rrmaster m,
         rrtran t,
         medicine med,
         vendor v
   WHERE     m.doc_no = t.doc_no
         AND m.doc_type = t.doc_type
         AND m.storecode = t.storecode
         AND med.medicine_code = t.id_no
         AND m.doc_date LIKE '%17'
         AND m.source = v.code
GROUP BY med.medicine_name;

错误

  

ORA-00979:不是GROUP BY表达式   00979. 00000-“不是GROUP BY表达式”   *原因:
  *行动:   行错误:1列:8

问候 索邦

1 个答案:

答案 0 :(得分:1)

请尝试以下操作,当使用aggregate functions时,所有非聚合列都必须包含在GROUP BY子句中

SELECT doc_date "Date of Purchase",
       cost_unit "Purchase On",
       price_unit "Sold On",
       price_unit - cost_unit "Margin",
       profit,
       medicine_name,
       manufacture,
       vendor_name
  FROM (  SELECT m.doc_date,
                 t.cost_unit,
                 t.price_unit,
                 t.price_unit,
                 t.cost_unit,
                 CONCAT (
                    ROUND (
                       ( (t.price_unit - t.cost_unit) * 100 / (t.cost_unit + 1)),
                       2),
                    '%')
                    "Profit",
                 med.medicine_name,
                 med.manufacture,
                 v.vendor_name
            FROM rrmaster m,
                 rrtran t,
                 medicine med,
                 vendor v
           WHERE     m.doc_no = t.doc_no
                 AND m.doc_type = t.doc_type
                 AND m.storecode = t.storecode
                 AND med.medicine_code = t.id_no
                 AND m.doc_date LIKE '%17'
                 AND m.source = v.code
        GROUP BY m.doc_date,
                 t.cost_unit,
                 t.price_unit,
                 t.price_unit,
                 t.cost_unit,
                 med.medicine_name,
                 med.manufacture,
                 v.vendor_name)