有一张有四列的表格:客户端、城市、邮政编码和街道。对于每个客户端,我想要计算唯一地址的数量。不幸的是,在一些列中可能有空:城市、邮政编码或街道。我必须忽略他们时,比较计数不同。因此,这不能通过简单的分组和计数不同来解决。
例如,
'client1', 'city1', 'postcode1', 'street1'
'client1', 'city1', 'postcode1', null
'client1', 'city1', null, 'street1'
'client1', null, null, 'street2'
'client1', 'city2', null, 'street1'
'client1', 'city2', null, 'street2'对于我的任务,唯一的地址应该是(编辑的)
'client1', 'city1', 'postcode1', 'street1'
'client1', 'city2', null, 'street1'
'client1', 'city2', null, 'street2'(答案是client1的三个唯一地址),
但是对于标准区分子句,这些都是唯一的,例如行。
'client1', 'city1', 'postcode1', 'street1'
'client1', 'city1', 'postcode1', null
'client1', 'city1', null, 'street1'被视为不同,而对于我的任务,这些并没有不同,我想把它们算作1。
评论后的编辑:如果我们有
'client1', null, null, 'street3'然后,这是一个唯一的地址(因为没有其他地址与'street3'),应该计算在内。
发布于 2020-06-25 12:29:13
我在PL/SQL中解决了我的问题。代码很长,所以如果有人感兴趣的话,我只会给出一个想法的概要。
中元组覆盖的元组。
我认为这个解决办法没有反例。
发布于 2020-06-24 16:08:37
您可以按以下方式使用min分析函数:
Select distinct t.client,
t.city,
Coalesce(t.postcode,Min(t.postcode) over (partition by t.client, t.city)) as postcode,
Coalesce(t.street,Min(t.street) over (partition by t.client, t.city)) as street
From your table
Where city is not null;-更新
我能想到自己的解决方案,看看它是否对你有用。
Select distinct a.client,
Coalesce(a.city, b.city) as city,
Coalesce(a.postcode, b.postcode) as postcode,
Coalesce(a.street, b.street) as street
From your_table a left join your_table b
On a.client = b.client
And (a.city = b.city or (a.city is null or b.city is null))
And (a.postcode = b.postcode or (a.postcode is null or b.postcode is null))
And (a.street = b.street or (a.street is null or b.street is null))
And a.rowid <> b.rowidhttps://stackoverflow.com/questions/62558979
复制相似问题