检查给定组的多列中的值

时间:2017-02-10 13:55:30

标签: sql sql-server

我不知道如何在SQL 2008中获得此功能

对于相同的邮政编码,有多个客户端并且具有RelationNo 我试图找到缺少的关系号码 当相同的邮政编码有两个或更多客户编号时,这些客户的任何一个电话号码是相同的 然后相同的交易号将在缺失的地方更新,或者更新为NULL或空白

如果电话号码不存在,则

有人可以建议如何在sql

中做

表:

DECLARE @MASTER TABLE
(
POSTAL VARCHAR (50),
CLIENT VARCHAR (50),
phone1 INT,
phone2 INT,
RelationNo VARCHAR (50)
)

INSERT @MASTER




SELECT 'M169HE',    'E5017766',016187,88888,'E100' UNION ALL
SELECT 'M169HE',    'M0111136', '', 016187, '' UNION ALL
SELECT 'IV11UF',    'E3009949', 50505,  '', 'F200' UNION ALL
SELECT 'KA48HH',    'E3087713', 01290,  '', '' UNION ALL
SELECT 'KA48HH',    'E3017126', 55665,  01290,  'G999' UNION ALL
SELECT 'KA48HH',    'E5005326', '', 55665,  '' UNION ALL
SELECT 'YO72LZ',    'C5640095', '', '', 'T8888' UNION ALL
SELECT 'YO72LZ',    'C5640096', '', '', '' 

输出:

POSTAL| CLIENT  |phone1  |  phone2| RelationNo
M169HE| E5017766|16187   |88888   |E100
M169HE| M0111136|        |16187   |E100
IV11UF| E3009949|   50505|        |F200
KA48HH| E3087713|   1290 |        |G999
KA48HH| E3017126|   55665|1290    |G999
KA48HH| E5005326|        |55665   |G999
YO72LZ| C5640095|        |  T8888 |
YO72LZ| C5640096|        |        |

由于

2 个答案:

答案 0 :(得分:1)

cross apply内使用common table expression查找并更新缺少RelationNo的行的匹配RelationNo

测试设置:http://rextester.com/YARZ79913

;with cte as (
  select o.*, x.FoundRelationNo
    from @MASTER as o
    cross apply (
      select top 1 FoundRelationNo = i.RelationNo 
        from @MASTER  as i
        where i.Postal = o.Postal
          and nullif(i.relationno,'') is not null
          and (
             nullif(i.phone1,0) in (o.phone1, o.phone2)
          or nullif(i.phone2,0) in (o.phone1, o.phone2)
              )
        ) as x
    where nullif(o.relationno,'') is null
     and (o.phone1 > 0 or o.phone2 > 0)
 )
 --select * from cte
 update cte set RelationNo = FoundRelationNo

 select * from @MASTER

结果:

+--------+----------+--------+--------+------------+
| POSTAL |  CLIENT  | phone1 | phone2 | RelationNo |
+--------+----------+--------+--------+------------+
| M169HE | E5017766 |  16187 |  88888 | E100       |
| M169HE | M0111136 |      0 |  16187 | E100       |
| IV11UF | E3009949 |  50505 |      0 | F200       |
| KA48HH | E3087713 |   1290 |      0 | G999       |
| KA48HH | E3017126 |  55665 |   1290 | G999       |
| KA48HH | E5005326 |      0 |  55665 | G999       |
| YO72LZ | C5640095 |      0 |      0 | T8888      |
| YO72LZ | C5640096 |      0 |      0 |            |
+--------+----------+--------+--------+------------+

答案 1 :(得分:1)

这可以通过简单的applyunion all

来完成
DECLARE @t TABLE(POSTAL VARCHAR (50),
                        CLIENT VARCHAR (50),
                        phone1 INT,
                        phone2 INT,
                        RelationNo VARCHAR (50)
                        )

INSERT @t
SELECT 'M169HE',    'E5017766',016187,88888,'E100' UNION ALL
SELECT 'M169HE',    'M0111136', '', 016187, '' UNION ALL
SELECT 'IV11UF',    'E3009949', 50505,  '', 'F200' UNION ALL
SELECT 'KA48HH',    'E3087713', 01290,  '', '' UNION ALL
SELECT 'KA48HH', 'E8888888', 01290, '', 'G888' UNION ALL
SELECT 'KA48HH',    'E3017126', 55665,  01290,  'G999' UNION ALL
SELECT 'KA48HH',    'E5005326', '', 55665,  '' UNION ALL
SELECT 'YO72LZ',    'C5640095', '', '', 'T8888' UNION ALL
SELECT 'YO72LZ',    'C5640096', '', '', '' 

select t.POSTAL
        ,t.CLIENT
        ,t.phone1
        ,t.phone2
        ,tt.RelationNo
from @t t
    outer apply(select top 1 RelationNo
                from @t t2
                where t.POSTAL = t2.POSTAL
                    and t.CLIENT <> t2.CLIENT
                    and (nullif(t.phone1,0) in(t2.phone1,t2.phone2)
                        or nullif(t.phone2,0) in(t2.phone1,t2.phone2)
                        )
                    and t2.RelationNo <> ''
                ) tt
where t.RelationNo = ''

union all

select *
from @t
where RelationNo <> ''
order by 1;

输出:

+--------+----------+--------+--------+------------+
| POSTAL |  CLIENT  | phone1 | phone2 | RelationNo |
+--------+----------+--------+--------+------------+
| IV11UF | E3009949 |  50505 |      0 | F200       |
| KA48HH | E8888888 |   1290 |      0 | G888       |
| KA48HH | E3017126 |  55665 |   1290 | G999       |
| KA48HH | E3087713 |   1290 |      0 | G888       |
| KA48HH | E5005326 |      0 |  55665 | G999       |
| M169HE | M0111136 |      0 |  16187 | E100       |
| M169HE | E5017766 |  16187 |  88888 | E100       |
| YO72LZ | C5640096 |      0 |      0 | NULL       |
| YO72LZ | C5640095 |      0 |      0 | T8888      |
+--------+----------+--------+--------+------------+