如何连接第6个表而不重复?

时间:2012-09-27 20:12:31

标签: sql oracle crystal-reports oracle10g

首先为“”全部道歉,这是Crystal Report的一部分。

我有这个查询,它是由其他人编写的,我需要添加另一个表但是如果添加的表有更多的数据行,我不希望重复值。

原始查询

select 
        "t1"."ID",
        "t4"."CURR",
        "t3"."TIME",
        "t4"."CASE",
        "t5"."CASE_TYPE"

from            
                "db"."t1" "t1"
full outer join "db"."t2" "t2" on "t1"."id"="t2"."id"
full outer join "db"."t3" "t3" on "t1"."id"="t3"."id"
full outer join "db"."t4" "t4" on "t1"."curr_name"="t4"."curr_id"
full outer join "db"."t5" "t5" on "t2"."cat"="t5"."cat"

以下是它现在的样子 exp1 http://i48.tinypic.com/23lebye.jpg

我需要加入另一个表格t6 id = t1id,并添加字段new1,new2

但如果t6.new1和new2有更多数据,我希望Current,Time,Case,Cause_Type为null。
例如
img2 http://i47.tinypic.com/jh4oib.jpg

如何在不重复红色和删除值的情况下加入t6?

2 个答案:

答案 0 :(得分:0)

Mogli试试这个: 我猜测t6字段是一个表,根据您在上面显示的内容,根据1个id#保存多个记录。

在CR数据库中,添加表,然后在新表中添加链接连接t6.id到t1.id,左外连接(从t1到t6) 在报表本身使用t1.id创建一个组,然后添加第二个组,其中包含您要从t6使用的字段。

如果您在t6上查找特定记录,请查看该表中是否有一行中包含“当前”ID,并在表之间添加连接(您可以删除之间的连接初始连接t1.id& t6.id)

答案 1 :(得分:0)

SELECT "ID"
,"CURR"
,"TIME"
,"CASE"
,"CASE_TYPE"
,New1
,New2
FROM (
    SELECT "t1"."ID"
    ,"t4"."CURR"
    ,"t3"."TIME"
    ,"t4"."CASE"
    ,"t5"."CASE_TYPE"
    ,SUM("New1") OVER (
        PARTTION BY "t1"."ID" ORDER BY "t1"."ID"
        ) AS New1
    ,SUM("New2") OVER (
        PARTTION BY "t1"."ID" ORDER BY "t1"."ID"
        ) AS New2
    ,ROW_NUMBER() OVER (
        PARTTION BY "t1"."ID" ORDER BY "t1"."ID"
        ) AS RowNum
    FROM "db"."t1" "t1"
    FULL OUTER JOIN "db"."t2" "t2"
        ON "t1"."id" = "t2"."id"
    FULL OUTER JOIN "db"."t3" "t3"
        ON "t1"."id" = "t3"."id"
    FULL OUTER JOIN "db"."t4" "t4"
        ON "t1"."curr_name" = "t4"."curr_id"
    FULL OUTER JOIN "db"."t5" "t5"
        ON "t2"."cat" = "t5"."cat"
    FULL OUTER JOIN "db"."t6" "t6"
        ON "t6"."id" = "t1"."id"
    ) Tb1
WHERE RowNum = 1