SQL如果单元格为空,请查看相邻单元格的值,搜索该列以进行匹配,并使用相应的值填充空白单元格

时间:2018-04-04 21:33:37

标签: sql

不确定这是否可行,但这是我正在尝试做的事情。我有一张这样的桌子:

URL         | URL_NAME
www.foo.com | ad_Blue_Heron_Resort
www.foo.com |

我将始终有两个URL行,但只有一个URL NAME。我想用相应的URL NAME填充空白。鉴于上述情况,是否可以使用查询空白的查询,当找到空白时,查找相邻的URL“www.foo.com”然后搜索URL列以查找匹配项,然后将URL NAME复制到空白处细胞

我在想这样的事情,但我无法绕过它:

SELECT
CASE WHEN URL_NAME IS NULL
  search URL column for a match for www.foo.com, and get URL_NAME from that row
END as URL_NAME

显然这不是真正的SQL,但我不知道我可以使用哪种功能......

更新我正在使用Marketing Cloud SQL,因此并非所有TSQL功能都可用。这就是我到目前为止所看起来应该有效,但事实并非如此。这是基于发布的第一个答案。

select LinkContent, CASE WHEN (LinkName = '' OR LinkName IS NULL) THEN
(SELECT max(LinkName) from [Link Ranking] n2 where n2.LinkContent = n.LinkContent)
END as LinkName
FROM [Link Ranking] n

我似乎无法将表格别名两次......

更新 好的,这最终为我工作,但它也给了我太多的结果,所以这是我需要调查的其他东西

SELECT n.LinkContent,
CASE WHEN LEN(n.LinkName)>0 THEN
    n.LinkName
ELSE
    n2.LinkName
END as LinkName
FROM [Newsletter Link Ranking Past month] n with (NOLOCK)
JOIN (
    SELECT LinkContent, LinkName
    FROM [Newsletter Link Ranking Past month] with (NOLOCK)
    WHERE LinkName IS NOT NULL
) n2 
ON n.LinkContent = n2.LinkContent

3 个答案:

答案 0 :(得分:2)

这是一种基于集合的方法。我做了以下假设:

  • 对于列URL中的每个不同值,URL_Name中的值将是相同的值或NULL
  • “无值”表示NULL。如果没有,请查看以下评论​​,了解是否需要检查空字符串。

代码:

--  Based on the assumptions, this gets the URL_Name for each URL
SELECT URL, max(URL_Name)
 from MyTable
 where URL_Name is not null  --  or, where URL_Name <> ''


--  Next make that a CTE--essentially, a temporary table that can be referenced
--  in the subsequent statement
WITH cteNames as
 (
    SELECT URL, max(URL_Name)  URL_Name
     from MyTable
     where URL_Name is not null  --  or, where URL_Name <> ''
 )
UPDATE MyTable
 set URL_Name = cte.URL_Name
 from MyTable mt
  inner join cteNames cte
   on cte.URL = mt.URL
 where mt.URL_Name is null  --  or, where URL_Name = ''

(警告:我没有针对表测试此代码,因此可能需要进行一些调整。)

- 编辑--------------

--  Here's the same written using subqueries
UPDATE MyTable
 set URL_Name = maxURL.URL_Name
 from MyTable mt
  inner join (
              select URL, max(URL_Name)  URL_Name
               from MyTable
               where URL_Name is not null  --  or, where URL_Name <> ''
             )  maxURL
   on maxURL.URL = mt.URL
 where mt.URL_Name is null  --  or, where URL_Name = ''

答案 1 :(得分:1)

您可以使用窗口函数填充查询中的空白:

select t.*,
       coalesce(url, max(url_name) over (partition by url)) as new_url_name
from t;

在更新中,您可以执行此操作。可在任何数据库中使用的一种方法是:

update t
    set url_name = (select max(url_name) from t t2 where t2.url = t.url)
    where url_name is null;

答案 2 :(得分:0)

这就是诀窍

SELECT n.LinkContent,
    CASE WHEN LEN(n.LinkName)>0 THEN
        n.LinkName
    ELSE
        n2.LinkName
    END as LinkName
    FROM [Newsletter Link Ranking Past month] n with (NOLOCK)
    JOIN (
        SELECT LinkContent, LinkName
        FROM [Newsletter Link Ranking Past month] with (NOLOCK)
        WHERE LinkName IS NOT NULL
    ) n2 
    ON n.LinkContent = n2.LinkContent