Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从SQL查询中删除以下数据中的交换列?_Sql_Postgresql_Swap - Fatal编程技术网

如何从SQL查询中删除以下数据中的交换列?

如何从SQL查询中删除以下数据中的交换列?,sql,postgresql,swap,Sql,Postgresql,Swap,我使用下面的SQL查询获取数据 select * from (select c.cdr_id as cdr_id, (select cdr_id from coms_trnsfrmd.cdr_legacy where cgs_cdr_id = k.assc_cdr_id) as associated_cdr_id from cgs_postgres.kl_cgs_ass

我使用下面的SQL查询获取数据

select * 
from 
    (select 
         c.cdr_id as cdr_id, 
         (select cdr_id 
          from coms_trnsfrmd.cdr_legacy 
          where cgs_cdr_id = k.assc_cdr_id) as associated_cdr_id 
     from 
         cgs_postgres.kl_cgs_associated_cdrs k 
     inner join 
         coms_trnsfrmd.cdr_legacy c on k.cdr_id = c.cgs_cdr_id )temp 
where 
    temp.associated_cdr_id is not null 
order by 
    temp.cdr_id
使用上面的sql查询,数据如下所示

CDR_ID          ASSOCIATED_CDR_ID
123                 456
456                 123
123                 178
178                 123
156                 169
198                 456
456                 198
案例1:如果记录看起来像交换

CDR_ID    ASSOCIATED_CDR_ID
 123             456
 456             123
我不需要两个都填充,我需要第一个或第二个记录

案例2:如果没有交换记录,如

CDR_ID    ASSOCIATED_CDR_ID
156            169 
我需要将其直接填充到目标中。

尝试以下操作:

with your_table
as (
    select *
    from (
        select c.cdr_id as cdr_id,
            (
                select cdr_id
                from coms_trnsfrmd.cdr_legacy
                where cgs_cdr_id = k.assc_cdr_id
                ) as associated_cdr_id
        from cgs_postgres.kl_cgs_associated_cdrs k
        inner join coms_trnsfrmd.cdr_legacy c on k.cdr_id = c.cgs_cdr_id
        ) temp
    where temp.associated_cdr_id is not null
    )
----- The above is your original query. Actual solution is below.---- 


select *
from your_table
where CDR_ID <= ASSOCIATED_CDR_ID

union all

select *
from your_table t
where CDR_ID > ASSOCIATED_CDR_ID
    and not exists (
        select 1
        from your_table t2
        where t.CDR_ID = t2.ASSOCIATED_CDR_ID
            and t2.CDR_ID = t.ASSOCIATED_CDR_ID
        )
“不存在”确保没有两行具有可交换的值。

请尝试以下操作:

with your_table
as (
    select *
    from (
        select c.cdr_id as cdr_id,
            (
                select cdr_id
                from coms_trnsfrmd.cdr_legacy
                where cgs_cdr_id = k.assc_cdr_id
                ) as associated_cdr_id
        from cgs_postgres.kl_cgs_associated_cdrs k
        inner join coms_trnsfrmd.cdr_legacy c on k.cdr_id = c.cgs_cdr_id
        ) temp
    where temp.associated_cdr_id is not null
    )
----- The above is your original query. Actual solution is below.---- 


select *
from your_table
where CDR_ID <= ASSOCIATED_CDR_ID

union all

select *
from your_table t
where CDR_ID > ASSOCIATED_CDR_ID
    and not exists (
        select 1
        from your_table t2
        where t.CDR_ID = t2.ASSOCIATED_CDR_ID
            and t2.CDR_ID = t.ASSOCIATED_CDR_ID
        )

“不存在”可确保没有两行具有可交换的值。

CDR ASSC_CDR_ID 918771 918775 918771 918779 918773 918785 918775 918771 918776 918784 918777 918785 918779 918771 918779 918779 918784 918782 918782 918782 918782 918782 918782 918782 918782 918784 918776 918784 918784 918785 918785 918785 918785 918785 918785 918785以上是从查询中获取的原始数据。如果你检查上面的数据,有交换数据,我不想要两条记录,我只想要一条记录,第一条或第二条。是的,它是正确的。我只需要那些数据。我在pgadmin中使用了相同的查询,我没有得到这些数据。CDR助理CDR ID 918771 918775 918771 918779 918773 918785 918775 918771 918776 918784 918777 918785 918779 918771 918779 918779 918779 918771 918779 918784 918782 918782 918782 918782 918782 918782 918782 918787 918784 918784 918784 918784 918785 918785 918773 918785 918785 918785 918785 918785 918785 918785 918785 918785 918785 918788 918785 918785 918785 918785 918785 918785 918785 918785 918785 918785 918785 918785 918785 9187查询如果你检查上面的数据,有交换数据,我不想要两条记录,我只想要一条记录,第一条或第二条。是的,它是正确的。我只需要那些数据。我在我的pgadmin中使用了相同的查询,我没有得到那个数据。