2个不同的列,6个总显示列,跨4个表

时间:2011-05-11 21:52:47

标签: sql oracle distinct

我正在尝试在Oracle中运行查询,我希望在2列(positionat.task_number上进行明确的选择)但总体而言我还想显示另外4列。

我理解Oracle只在所有使用的列上使用distinct,而不仅仅是order子句。如果我删除了APP_GRPag.app_group_cd的资格,那么我的查询会给出我想要的输出,但我还需要显示这些列。

以下是我的选择今天如何看,但它没有给我我想要的东西。

select distinct
(select cv.display from code_value cv where cv.code_value = ag.position_cd) AS POSITION, AG.POSITION_CD,
(select cv.display from code_value cv where cv.code_value = ag.app_group_cd) AS APP_GRP, ag.app_group_cd, 
at.task_number, at.description, Users=count(ag.position_cd)
from
application_group ag, application_task at, task_access ta, code_value cv
plan cv
where cv.code_set= 78
and cv.active_ind = 1
and cv.begin_effective_dt_tm <= SYSDATE
and cv.end_effective_dt_tm >= SYSDATE
join ag
where ag.position_cd = cv.code_value
join ta
where ta.app_group_cd = ag.app_group_cd
join at
where at.task_number = ta.task_number
and at.task_number in (1, 2, 3, 4, 5, 6)
and at.active_ind = 1
join d
group by
ag.position_cd, ag.app_group_cd, at.task_number, at.description
having
COUNT(ag.position_cd)>0
order by position, at.task_number



已更新

我的查询返回类似这样的内容......

POSITION -----的 APP_GRP -----的 TASK_NUMBER
zzzDBA ------------- DBA --------------------- 1
zzzDBA ------------- DBA --------------------- 4
zzzDBA ------------- DBA --------------------- 3
zzzDBA ------------- DBA --------------------- 5
zzzDBA ------------- DBA --------------------- 2
zzzDBA ------------- DBA -------------------- 3
zzzDBA ------------- DBA -------------------- 4
zzzDBA ------------- DBA -------------------- 5

最终我想到达我只返回的地方:

POSITION -----的 APP_GRP -----的 TASK_NUMBER
zzzDBA ------------- DBA --------------------- 1
zzzDBA ------------- DBA --------------------- 4
zzzDBA ------------- DBA --------------------- 3
zzzDBA ------------- DBA --------------------- 5
zzzDBA ------------- DBA --------------------- 2
&#34;去除&#34; TASK_NUMBER IN(3,4,5)的重复行

2 个答案:

答案 0 :(得分:0)

如果您没有为这两个字段获取不同的值,则表示您在APP_GRPag.app_group_cd字段中返回了多个值。

如果你对这些字段应用类似MAX()的东西并从GROUP BY语句中删除ag.app_group_cd,你可能会得到不同的值,但你实际上隐藏了这个组合产生{{{{ 1}}和APP_GRP

如果这没有意义,那么查看示例查询结果会更容易看到您希望看到的内容以及您的查询返回的内容。

答案 1 :(得分:0)

这个查询似乎可以解决问题......

SELECT      tn.Position,
tn.Task_Number,
tn.TotalPosCd,
maxapp.MaxAppGroupCD,
ta.Description,
appgrp.display AS APP_GRP

FROM
        (SELECT     cv.display AS Position,
                    at.Task_Number,
                    COUNT(ag.position_code) AS TotalPosCd
        FROM  APPLICATION_TASK AT ,
                    TASK_ACCESS TA ,
                    APPLICATION_GROUP AG ,
                    CODE_VALUE CV
        WHERE AG.POSITION_CD  =    CV.CODE_VALUE 
                    AND TA.APP_GROUP_CD = AG.APP_GROUP_CD 
                    AND AT.TASK_NUMBER =  TA.TASK_NUMBER 
                    AND CV.CODE_SET =  88
                    AND CV.ACTIVE_IND =  1
                    AND CV.BEGIN_EFFECTIVE_DT_TM <= SYSDATE
                    AND CV.END_EFFECTIVE_DT_TM >=  SYSDATE
                    AND AT.TASK_NUMBER IN (1,2,3,4,5,6)
                    AND AT.ACTIVE_IND = 1 
        GROUP BY    cv.display,
                          at.Task_Number
        HAVING      COUNT(ag.Position_code) > 0
        ) tn,
        (SELECT ta.Task_Number,
                    MAX(ag.App_Group_CD) AS MaxAppGroupCD
        FROM  TASK_ACCESS ta,
                    APPLICATION_TASK at,
                    APPLICATION_GROUP ag
        WHERE tn.Task_number = ta.Task_number
                    AND ta.Task_Number = at.Task_number
                    AND ta.App_Group_CD = ag.App_Group_CD
        GROUP BY ta.Task_number
        ) maxapp,
        TASK_ACCESS ta,
        CODE_VALUE appgrp

WHERE tn.Task_number = maxapp.Task_Number
        AND maxapp.MaxAppGroupCD = ta.app_group_cd
        AND tn.Task_Number = ta.Task_Number
        AND maxapp.MaxAppGroupCD = appgrp.Code_Value