谢谢,我在这个论坛上得到了慷慨的人们的大力帮助。我是Oracle SQL的初学者。
我在两个VIEW之间有一对多的关系,即PERSON_VIEW和PHONE_VIEW。在帮助下,我得到一个查询case /何时让手机工作,但当我将它添加回我的主查询时,它影响了我与TASK_VIEW的其他联接。
我想我需要将其添加为内部查询,所以我得到了一个结果。我在我的查询中添加了以下内容。我在TOAD中运行了查询,它给了我一个错误,“ORA-00979:不是GROUP BY表达式”,并突出显示了查询中的所有PERSON_ID。如果我采取以下左连接,它运行正常,不输出电话号码。任何帮助表示赞赏。
我还注意到,由于我带回了3个电话号码(小区,家庭和商业),因此从TASK返回时会导致重复。使用Task Inner Join, 我得到了像
这样的结果在: 1 John M. Doe ToDo1:ToDO1,ToDo2:ToDo2
在: 1 John M. Doe ToDo1:ToDO1,ToDo1:ToDO1,ToDo1:ToDO1,ToDo2:ToDo2,ToDo2:ToDo2,ToDo2:ToDo2
我从3个case语句中取出了最大值并运行了查询。但它为每个电话号码返回一个单独的行,如此
1 John Doe 999-999-9999 1 John Doe 888-888-8888 1 John Doe 222-222-2222
我该如何解决这个问题?
LEFT JOIN (SELECT PERSON_ID, PHONE_TP_SHORT_DESC,
max(case when PHONE_TYPE = 'HOME' then PHONE_NUMBER end) as HOME_PHONE,
max(case when PHONE_TYPE = 'BUSINESS' then PHONE_NUMBER end) as BUSINESS_PHONE,
max(case when PHONE_TYPE = 'CELL' then PHONE_NUMBER end) as CELL_PHONE
FROM PHONE_VIEW) PHONE
ON PERSON.PERSON_ID = PHONE.PERSON_ID AND PHONE.PHONE_TYPE IN ('HOME','BUSINESS','CELL')
这是我正在尝试调试的整个查询
SELECT PERSON.PERSON_ID,
PERSON.FIRST_NAME,
PERSON.MIDDLE_NAME,
PERSON.LAST_NAME,
PERSON.USER_NAME,
PHONE.HOME_PHONE,
PHONE.BUSINESS_PHONE,
PHONE.CELL_PHONE,
LISTAGG(case
when SKILLS.SKILL_SHORT_DESC = 'ToDo1' then 'ToDo1:ToDo1'
when SKILLS.SKILL_SHORT_DESC = 'ToDo2' then 'ToDo2:ToDo2'
when SKILLS.SKILL_SHORT_DESC = 'ToDo3' then 'ToDo3:ToDo3'
when SKILLS.SKILL_SHORT_DESC = 'ToDo4' then 'ToDo4:ToDo4'
when SKILLS.SKILL_SHORT_DESC = 'ToDo5' then 'ToDo5:ToDo5'
when SKILLS.SKILL_SHORT_DESC = 'ToDo6' then 'ToDo6:ToDo6'
else ''
end, ',')
WITHIN GROUP (ORDER BY TASK.TASK_DESC ) AS TASK
FROM PERSON_VIEW PERSON
LEFT JOIN (SELECT PERSON_ID, PHONE_TP_SHORT_DESC,
max(case when PHONE_TYPE = 'HOME' then PHONE_NUMBER end) as HOME_PHONE,
max(case when PHONE_TYPE = 'BUSINESS' then PHONE_NUMBER end) as BUSINESS_PHONE,
max(case when PHONE_TYPE = 'CELL' then PHONE_NUMBER end) as CELL_PHONE
FROM PHONE_VIEW) PHONE
ON PERSON.PERSON_ID = PHONE.PERSON_ID AND PHONE.PHONE_TYPE IN ('HOME','BUSINESS','CELL')
LEFT JOIN (SELECT DISTINCT PERSON_ID, TASK_DESC FROM TASK_VIEW) SKILLS
ON SKILLS.PERSON_ID = PERSON.PERSON_ID
GROUP BY
PERSON.PERSON_ID,
PERSON.FIRST_NAME,
PERSON.MIDDLE_NAME,
PERSON.LAST_NAME,
PERSON.USER_NAME,
PHONE.HOME_PHONE,
PHONE.BUSINESS_PHONE,
PHONE.CELL_PHONE
答案 0 :(得分:0)
我确实发现您的查询存在一个问题。您对以下子查询有一个LEFT JOIN
:
(SELECT PERSON_ID,
PHONE_TP_SHORT_DESC,
max(case when PHONE_TYPE = 'HOME' then PHONE_NUMBER end) as HOME_PHONE,
max(case when PHONE_TYPE = 'BUSINESS' then PHONE_NUMBER end) as BUSINESS_PHONE,
max(case when PHONE_TYPE = 'CELL' then PHONE_NUMBER end) as CELL_PHONE
FROM PHONE_VIEW) PHONE
但是,在ON
条件下,您会引用名为PHONE_TYPE
的列:
ON PERSON.PERSON_ID = PHONE.PERSON_ID AND
PHONE.PHONE_TYPE IN ('HOME','BUSINESS','CELL')
没有名为PHONE_TYPE
的列可用,因此我预计会出现错误。有时,您从数据库(或编译器)获得的错误消息可能会产生误导,因为它并不总是揭示问题的根本原因。
另一个可能的错误原因是您正在使用视图,它们的行为与表格不同。