oracle查询返回重复数据

时间:2016-01-07 12:22:00

标签: sql oracle ado.net

我已经编写了一个oracle查询来获取数据并且它返回了重复数据,因此我使用 distinct 关键字。但是我收到错误: - ORA-01791:不是SELECTed 表达式

Oracle Query: -

  Select Distinct  pi.first_name || ' ' || pi.last_name initiator
                 , pi.user_id
                 , c.country_name
                 , e.eform_name
                 , w.date_stamp_utc
                 , decode (
                           waRating.attr_value, 4, 'Very Positive',
                                                3, 'Positive',
                                                2, 'Neutral',
                                                1, 'Negative',
                                                0, 'Very Negative', '???'
                          ) rating
                , waEnv.attr_value environment
                , nvl(ua.agent_name, '???') browser
                , nvl(ua.agent_version, '???') version
                , waFeedback.attr_value negative_feedback

            From request_workflow w 
      Inner Join request_workflow_attribute waRating
              On waRating.request_workflow_id = w.row_id
             And waRating.attr_name = 'UserRating'
      Inner Join request_workflow_attribute waEnv
              On waEnv.request_workflow_id = w.row_id
             And waEnv.attr_name = 'Environment'
      Inner Join request_workflow_attribute waBrowser
              On waBrowser.request_workflow_id = w.row_id
             And waBrowser.attr_name = 'UserAgent'
       Left Join request_workflow_attribute waFeedback
              On waFeedback.request_workflow_id = w.row_id
             And waFeedback.attr_name = 'UserFeedback'
      Inner Join person_info pi
              On w.performer_id = pi.row_id
      Inner Join request_common rc
              On w.request_id = rc.row_id
      Inner Join eform e
              On rc.eform_id = e.row_id
       Left Join user_agent ua
              On waBrowser.attr_value = ua.user_agent_raw
      Inner Join country c
              On pi.country_code = c.country_code

           Where w.date_stamp_utc 
         Between '1-dec-2014' And '31-dec-2015'
        Order By waRating.attr_value desc, eform_name;

请建议如何使其发挥作用。

2 个答案:

答案 0 :(得分:1)

错误消息是由order by子句中的字段引起的。它是

order by waRating.attr_value desc, eform_name

在你的select子句中,你有这个:

waEnv.attr_value environment

虽然字段名称相同,但它们来自不同的表。但是,这可能并不重要,因为您还在select子句中使用了别名。在您的订单中使用相同的一个:

order by environment desc, eform_name

答案 1 :(得分:0)

使用group by代替select distinct

select . . .
from . . .
where . . .
group by pi.first_name || ' ' || pi.last_name initiator
     , pi.user_id
     , c.country_name
     , e.eform_name
     , w.date_stamp_utc
     , decode(waRating.attr_value, 4, 'Very Positive',
                                   3, 'Positive',
                                   2, 'Neutral',
                                   1, 'Negative',
                                   0, 'Very Negative', '???')
     , waEnv.attr_value
     , nvl(ua.agent_name, '???')
     , nvl(ua.agent_version, '???')
     , waFeedback.attr_value 
order by waEnv.attr_value, eform_name;