问题描述
所以我有 2 个表:Clients_Test 和 Vacations_Test。我需要创建一个有 2 个维度的仓库: d1_month 和d2_destination。应该包含这2个维度字段的主表是事实表。此外,事实还需要多2个字段:每个客户的目的地总数+总价格。 问题是在我将数据插入事实表后,按行分组失败了,我不知道为什么。尝试使用其他字段对它们进行分组,但它仍然让我退缩。
CREATE TABLE CLIENTS_TEST(
IDclient NUMBER(7) PRIMARY KEY,name_client VARCHAR2(40),adress VARCHAR2(50),city VARCHAR2(30),county VARCHAR2(20));
CREATE TABLE VACATIONS_TEST(
number_contract NUMBER(6) PRIMARY KEY,IDclient NUMBER(7) REFERENCES CLIENTI_TEST(IDclient),date_contract DATE,destination VARCHAR2(40),price NUMBER(10));
INSERT INTO CLIENTS_TEST VALUES (1,'name1','adr1','Timisoara','Timis');
INSERT INTO CLIENTS_TEST VALUES (2,'name2','adr2','arad','arad');
INSERT INTO CLIENTS_TEST VALUES (3,'name3','adr3','Cluj','Cluj');
INSERT INTO CLIENTS_TEST VALUES (4,'name4','adr4','arad');
INSERT INTO CLIENTS_TEST VALUES (5,'name5','adr5','Timis');
INSERT INTO CLIENTS_TEST VALUES (6,'name6','adr6','Cluj');
INSERT INTO VACATIONS_TEST VALUES (11,2,TO_DATE('2/05/2020','dd/mm/yyyy'),'dest2',4000);
INSERT INTO VACATIONS_TEST VALUES (12,3,TO_DATE('4/05/2020','dest5',4000);
INSERT INTO VACATIONS_TEST VALUES (13,6,TO_DATE('8/05/2020','dest3',3000);
INSERT INTO VACATIONS_TEST VALUES (14,1,TO_DATE('10/05/2020','dest4',3000);
INSERT INTO VACATIONS_TEST VALUES (15,TO_DATE('12/05/2020','dest1',2000);
INSERT INTO VACATIONS_TEST VALUES (16,5,TO_DATE('15/05/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (17,TO_DATE('18/05/2020',5000);
INSERT INTO VACATIONS_TEST VALUES (18,TO_DATE('21/05/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (19,TO_DATE('24/05/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (20,4,TO_DATE('27/05/2020',6000);
INSERT INTO VACATIONS_TEST VALUES (21,TO_DATE('3/06/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (22,TO_DATE('6/06/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (23,TO_DATE('7/06/2020',6000);
INSERT INTO VACATIONS_TEST VALUES (24,TO_DATE('9/06/2020',5000);
INSERT INTO VACATIONS_TEST VALUES (25,TO_DATE('11/06/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (26,TO_DATE('14/06/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (27,TO_DATE('17/06/2020',6000);
INSERT INTO VACATIONS_TEST VALUES (28,TO_DATE('19/06/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (29,TO_DATE('21/06/2020',2000);
INSERT INTO VACATIONS_TEST VALUES (30,TO_DATE('27/06/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (31,TO_DATE('1/07/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (32,TO_DATE('4/07/2020',2000);
INSERT INTO VACATIONS_TEST VALUES (33,TO_DATE('6/07/2020',2000);
INSERT INTO VACATIONS_TEST VALUES (34,TO_DATE('10/07/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (35,TO_DATE('12/07/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (36,TO_DATE('15/07/2020',3000);
INSERT INTO VACATIONS_TEST VALUES (37,TO_DATE('22/07/2020',2000);
INSERT INTO VACATIONS_TEST VALUES (38,TO_DATE('24/07/2020',4000);
INSERT INTO VACATIONS_TEST VALUES (39,TO_DATE('27/07/2020',2000);
INSERT INTO VACATIONS_TEST VALUES (40,TO_DATE('29/07/2020',4000);
Create table d1_month (month_contract number(2) primary key);
Insert into d1_month select distinct extract(month from data_contract) from vacations_test;
Create table d2_destination(destination varchar2(40) Primary key);
insert into d2_destination select distinct destination from vacations_test;
Create table fact (month_contract number(2) references d1_month(month_contract),destination varchar2(40) references d2_destination(destination),nr_vacations number(10),total_price number(20),Primary key(nr_vacations,total_price));
Insert into fact
select extract(month from VACATIONS_TEST.date_contract),VACATIONS_TEST.destination,count(VACATIONS_TEST.IDclient),sum(VACATIONS_TEST.price)
from vacations_test,clients_test
WHERE VACATIONS_TEST.IDclient=CLIENTS_TEST.IDclient
group by VACATIONS_TEST.destination,extract(month from VACATIONS_TEST.date_contract); // error
解决方法
不是 GROUP BY
失败,而是主键被违反的事实:
SQL> insert into fact (month_contract,destination,nr_vacations,total_price)
2 select
3 extract(month from vacations_test.date_contract),4 vacations_test.destination,5 count(vacations_test.idclient),6 sum(vacations_test.price)
7 from vacations_test,clients_test
8 where vacations_test.idclient = clients_test.idclient
9 group by vacations_test.destination,10 extract(month from vacations_test.date_contract)
11 ;
insert into fact (month_contract,total_price)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C007160) violated
SQL>
怎么办?
- 如果
fact
表的主键设置不正确,请更改它 - 如果主键正常但数据违反它,则确保查询返回两个主键列的唯一组合:
[nr_vacations,total_price]
- 怎么样?也许通过在查询中包含
where
子句,或者...谁知道?我没有,但你应该。
- 怎么样?也许通过在查询中包含