我有一个包含一些许可证的表,对于不同的许可证,存在基于网络的记录 例如:许可证12345具有网络记录(' 101',' 102',' 108') 现在我想从网络中找出(' 101'' 102'' 108',' 113',&#39 ; 114'),我的许可证丢失了所有网络,之后我们需要将现有许可证的数据复制到所有五个网络的丢失许可证中吗?
我已经在使用脚本了: 看看
select license, count(*) data, network nt1 from x_pns_address
where network in ('101','102', '108', '113', '114')
and license in ('110438','150275')
group by license, network
into temp sherin_1;
select distinct license, '101' netw from x_pns_address
where license in ('110438','150275')
into temp table1;
insert into table1 select distinct license,
'102' netw from x_pns_address
where license in ('110438','150275');
insert into table1 select distinct license,
'108' netw from x_pns_address
where license in ('110438','150275');
insert into table1 select distinct license,
'113' netw from x_pns_address
where license in ('110438','150275');
insert into table1 select distinct license,
'114' netw from x_pns_address
where license in ('110438','150275');
select distinct a.nt1,a.license,
b.netw from sherin_1 a, table1 b
where a.license = b.license
and a.nt1 <> b.netw
;
这就是我的意思:
nt1 license netw
101 150275 102
101 150275 108
101 150275 113
101 150275 114
102 110438 101
102 110438 108
102 110438 113
102 110438 114
102 150275 101
102 150275 108
102 150275 113
102 150275 114
108 110438 101
108 110438 102
108 110438 113
108 110438 114
108 150275 101
108 150275 102
108 150275 113
108 150275 114
113 110438 101
113 110438 102
113 110438 108
113 110438 114
113 150275 101
113 150275 102
113 150275 108
113 150275 114
114 110438 101
114 110438 102
114 110438 108
114 110438 113
答案 0 :(得分:0)
对于SQL Server,您可以使用:
我用虚拟数据创建表。
CREATE TABLE #x_pns_address (
network int,
license int
)
INSERT INTO #x_pns_address VALUES
(101,12345),
(102,12345),
(108,12345),
(101,54321),
(102,54321),
(108,54321),
(113,54321),
(114,54321),
(101,98765),
(108,98765)
正如您所看到,许可54321
拥有全部5个网络,而12345
,98765
获得了2和3。
然后我们运行:
INSERT INTO #x_pns_address
SELECT p1.license,
n.network
FROM (
SELECT license --Here we get all licenses that missed any of the
FROM #x_pns_address --given numbers
WHERE network IN (101,102,108,113,114)
GROUP BY license
HAVING COUNT(network) < 5
) as p1
CROSS JOIN ( --Cartesian join with license numbers we need
SELECT * FROM (VALUES (101),(102),(108),(113),(114)) as t(network)
) as n
LEFT JOIN #x_pns_address x --and here we get only those that are not in main table
ON x.network = n.network and x.license = p1.license
WHERE x.license IS NULL
在#x_pns_address
我们为许可12345
,98765
添加5行缺少网络:
license network
12345 113
12345 114
98765 102
98765 113
98765 114