按表达式错误分组

时间:2014-04-21 17:07:55

标签: oracle group-by sqlplus

有关GROUP BY查询的问题。这是我使用的以下语句,因此我可以创建查询:

SQL> SELECT invoice_num, technician_num, service_advisor, invoice_date_in, customer_id
2 FROM invoices_TP
3 GROUP BY invoice_date_in;

这是我每次尝试实现它时都会遇到的错误:

SELECT invoice_num, technician_num, service_advisor, invoice_date_in, customer_id
*
ERROR at line 1:
ORA-00979: not a GROUP BY expression

我检查了表,所有SELECT语句都是根据表包含的列。

以下是已创建表和信息的其余语句:

CREATE TABLE parts_TP
(part_num NUMBER (8),
 part_unit_price NUMBER (6,2)
     CONSTRAINT parts_TP_prt_unit_price_nn NOT NULL, 
 part_description VARCHAR2 (40)
     CONSTRAINT parts_TP_prt_desc_nn NOT NULL,
 CONSTRAINT parts_TP_prt_num_pk PRIMARY KEY (part_num));

CREATE TABLE vehicles_TP
(car_vin VARCHAR2 (17),
 car_year CHAR (8)
     CONSTRAINT veh_TP_car_year_nn NOT NULL,
 car_manufacturer VARCHAR2 (32)
     CONSTRAINT veh_TP_car_man_nn NOT NULL,
  car_color VARCHAR2 (15)
     CONSTRAINT veh_TP_car_col_nn NOT NULL,
 car_engine_type VARCHAR2 (32)
     CONSTRAINT veh_TP_car_eng_nn NOT NULL, 
 CONSTRAINT veh_TP_car_vin_pk PRIMARY KEY (car_vin));

CREATE TABLE customers_TP
(customer_id NUMBER (6),
 customer_last_name VARCHAR2 (15)
     CONSTRAINT cust_TP_cust_lname_nn NOT NULL,
 customer_first_name VARCHAR2 (10)
     CONSTRAINT cust_TP_cust_fname_nn NOT NULL,
 customer_street VARCHAR2 (18)
     CONSTRAINT cust_TP_cust_street_nn NOT NULL,
 customer_city VARCHAR2 (15)
     CONSTRAINT cust_TP_cust_city_nn NOT NULL,
 customer_state CHAR (2)
     CONSTRAINT cust_TP_cust_state_nn NOT NULL,
 customer_zip_code CHAR (5)
     CONSTRAINT cust_TP_cust_zip_nn NOT NULL,
 customer_phone_num CHAR (12)
     CONSTRAINT cust_TP_cust_phone_nn NOT NULL,
 customer_alt_phone CHAR (12),
 customer_license_num varchar2 (8)
     CONSTRAINT cust_TP_cust_licnum_nn NOT NULL,
 CONSTRAINT cust_TP_cust_id_pk PRIMARY KEY (customer_id));

CREATE TABLE technicians_TP
(technician_num NUMBER (2),
 technician_last_name VARCHAR2 (10)
     CONSTRAINT tech_TP_tech_lname_nn NOT NULL,
 technician_first_name VARCHAR2 (8)
     CONSTRAINT tech_TP_tech_fname_nn NOT NULL,
 CONSTRAINT tech_TP_tech_num_pk PRIMARY KEY (technician_num));

CREATE TABLE ownerships_TP
(customer_id NUMBER (6),
 car_vin VARCHAR2 (17)
     CONSTRAINT owner_TP_car_vin_not_null NOT NULL,
 CONSTRAINT owner_TP_cust_id_prime_key PRIMARY KEY (customer_id, car_vin),
 CONSTRAINT owner_TP_cust_num_fk FOREIGN KEY (customer_id)
     REFERENCES customers_TP (customer_id),
  CONSTRAINT ownerships_TP_car_vin_fk FOREIGN KEY (car_vin)
     REFERENCES vehicles_TP (car_vin));

CREATE TABLE invoices_TP
(invoice_num NUMBER (6),
 customer_id NUMBER (6)
     CONSTRAINT inv_TP_cust_nn NOT NULL, 
car_vin VARCHAR2 (17)
     CONSTRAINT inv_TP_car_vin_nn NOT NULL,     
 invoice_date_in DATE
     CONSTRAINT inv_TP_inv_date_in_nn NOT NULL,
 invoice_date_out DATE 
     CONSTRAINT inv_TP_inv_date_out_nn NOT NULL,
 invoice_sales_tax NUMBER (5,2)
     CONSTRAINT inv_TP_inv_sls_tax_nn NOT NULL,
 invoice_mileage_in Number (6)
     CONSTRAINT inv_TP_inv_mile_nn NOT NULL,  
 technician_num NUMBER (2)
     CONSTRAINT inv_TP_tech_num_nn NOT NULL,
 service_advisor VARCHAR2 (15)
     CONSTRAINT inv_TP_serv_nn NOT NULL,
 CONSTRAINT inv_TP_inv_num_pk PRIMARY KEY (invoice_num),   
 CONSTRAINT inv_TP_car_vin_fk FOREIGN KEY (car_vin)
     REFERENCES vehicles_TP (car_vin),
 CONSTRAINT inv_TP_inv_car_vin_fk FOREIGN KEY (customer_id)
     REFERENCES customers_TP (customer_id),
 CONSTRAINT inv_TP_tech_num_fk FOREIGN KEY (technician_num)
     REFERENCES technicians_TP (technician_num));

CREATE TABLE parts_sold_TP
(invoice_num NUMBER (6),
 part_num NUMBER (8)
      CONSTRAINT prt_num_TP_not_null NOT NULL,
 part_unit_price NUMBER (6,2)
     CONSTRAINT prt_unit_price_not_null NOT NULL,
 part_qty_sold CHAR (3)
     CONSTRAINT prt_qty_sold_TP_not_null NOT NULL,
 CONSTRAINT prt_sls_TP_inv_num_prime_key PRIMARY KEY (invoice_num, part_num),  
 CONSTRAINT prt_sls_TP_inv_num_fk FOREIGN KEY (invoice_num)
     REFERENCES invoices_TP (invoice_num),
 CONSTRAINT prt_sls_TP_part_num_fk FOREIGN KEY (part_num)
     REFERENCES parts_TP (part_num));

/* rem this sequence is used to increase with every new customer */
CREATE SEQUENCE cust_id_seq
 INCREMENT BY 1
 START WITH 367909
 NOCYCLE
 NOCACHE;
/* this sequence is used to increase with every new invoice */
CREATE SEQUENCE invoice_num_seq
 INCREMENT BY 1
 START WITH 160000
 NOCYCLE
 NOCACHE;

任何帮助都会感激不尽。

谢谢!

1 个答案:

答案 0 :(得分:1)

当您使用group by表达式时,您选择的列可以是group by或colregate函数(如min,max,count等)。

数据示例:

NAME    LAST_NAME    AMOUNT   
----    ---------    ------
Bob     Marley       100
Freddy  Mercury      120
Bob     Marley       150

你想输出类似的东西:

NAME    LAST_NAME    AMOUNT   
----    ---------    ------
Bob     Marley       250
Freddy  Mercury      120

如果你这样做

select name, last_name, sum(amount)
from my_table
group by name

我将收到您的错误,因为不清楚如何处理Bob的last_name。 有2条记录,我们只需要显示1,DB应该如何选择?

这是两个可行的解决方案。

  1. 在group by中使用last_name:

    select name, last_name, sum(amount)
    from my_table
    group by name, last_name
    
  2. 在last_name:

    上使用agregate功能
    select name, max(last_name), sum(amount)
    from my_table
    group by name