简体   繁体   English

为什么我不认为case语句返回NULL值?

[英]Why is my case statement returning a NULL value when I don't think it should?

Question is at the bottom but in short, the table I'm creating is giving my Null values for the field Division even when the links match my case expression and I cant seem to find why. 问题在底部,但总之,即使链接与我的case表达式匹配,而且我似乎找不到原因,我正在创建的表仍在为字段Division赋予Null值。

I am creating a table to assign divisions according to a link url within the program: 我正在创建一个表来根据程序中的链接URL分配分区:

create table set_divisions as
  select distinct campaign, offer,
   case
   when upper(link) like '%NORTH%' then 'NORTH'
   when upper(link) like '%SOUTH%' then 'SOUTH'
   when upper(link) like '%EAST%'  then 'EAST'
   when upper(link) like '%WEST%'  then 'WEST'
   ELSE NULL
   END AS DIVISION
   FROM TABLE A;

I then get a lot of NULL's in Division so I'm trying to see what links I'm missing as they all should have a value in Division. 然后,我在Division中得到了很多NULL,因此我试图查看缺少的链接,因为它们都应该在Division中有价值。 Each campaign does have multiple links some not associated with north, south, east or west but every campaign should also contain one of these links from the case statement. 每个战役确实具有多个链接,有些链接与北,南,东或西不相关,但是每个战役也应包含case语句中的这些链接之一。

I then run the following: 然后运行以下命令:

select distinct link
from  A, set_divisions S
where A.campaigncode = S.campaigncode
and   A.offercode    = S.offercode
and (
     upper(link) like '%NORTH%'
     or upper(link) like '%SOUTH%'
     or upper(link) like '%EAST%'
     or upper(link) like '%WEST%');

The results come back with links that have NORTH , SOUTH , EAST AND WEST in them. 结果回来与具有链接的NORTHSOUTHEASTWEST在其中。

So my question is what am I doing wrong in my case expression when creating my table, that is causing it to miss these links? 所以我的问题是在创建表时我的case表达式做错了什么,导致它丢失了这些链接?

examples of links being missed are(I only used south and east but there are similar links for all four): 缺少链接的示例是(我只使用了南方和东方,但所有四个链接都相似):

http://product.south.com/information/preference
http://sports.east.com/unsubscribe/?email=%%Linkurl9%%

Thanks and this is my first post so I apologize ahead of time if I missed something or my code looks crappy lol. 谢谢,这是我的第一篇文章,所以如果我错过了一些事情或者我的代码看起来很糟糕,我会提前道歉。

Reighlo 雷洛

In the second query, your WHERE clause specifies that link must contain NORTH, SOUTH, EAST, or WEST. 在第二个查询中,您的WHERE子句指定链接必须包含NORTH,SOUTH,EAST或WEST。 Your first query does not contain a similar WHERE clause. 您的第一个查询不包含类似的WHERE子句。

You can run this to find links not containing NORTH, SOUTH, EAST or WEST to troubleshoot: 您可以运行此命令以查找不包含NORTH,SOUTH,EAST或WEST的链接以进行故障排除:

select distinct link
from  A, set_divisions S
where A.campaigncode = S.campaigncode
and   A.offercode    = S.offercode
and upper(link) not like '%NORTH%'
and upper(link) not like '%SOUTH%'
and upper(link) not like '%EAST%'
and upper(link) not like '%WEST%');

So the table that I was running this for had tens of millions of rows and as mentioned in the comments above the case statement was bringing back Null due to many rows that yielded that result even if my link url was in there. 因此,我为其运行的表具有数千万行,并且正如上面的注释中所提到的,由于即使存在我的链接URL,仍有许多行产生了该结果,因此case语句将Null带回。 I did not want to exclude all that resulted in NULL because there are some programs that truly should be null. 我不想排除所有导致NULL的情况,因为有些程序确实应该为null。 I found that by bringing in all rows with their urls and grouping them, then selecting distinct values from that subset with my case statement included, then selecting again and running the case statement for all the remaining where division is still null did the trick. 我发现通过引入所有带有其url的行并将它们分组,然后从包含我的case语句的子集中选择不同的值,然后再次选择并为除法仍然为null的所有其余部分运行case语句,就可以了。 However, this did bring back duplicates so I had to use rownumber() to bring in everything I wanted. 但是,这确实带来了重复项,因此我不得不使用rownumber()来引入我想要的所有内容。 Basically to get rid of any NULL's where a true division already existed. 基本上是为了消除已经存在真除法的所有NULL。 There were some duplicates with different divisions but I needed to bring those in because that's another issue that has to be looked at. 有一些重复的内容,但有不同的部门,但是我需要带进去,因为这是另一个必须要解决的问题。 Here is my code and please feel free to let me know if there was an easier way to do this. 这是我的代码,如果有更简单的方法,请随时告诉我。 Not sure if it matters but my table ran in about 5 minutes. 不确定是否重要,但是我的桌子在大约5分钟内就跑完了。 Sorry if there are any typos, I had to recreate from my code to the fields and values here because our client is real big on privacy issues and although this would not put anything out there, it's just a headache I'd rather not deal with. 抱歉,如果有任何错别字,我不得不从我的代码重新创建到此处的字段和值,因为我们的客户在隐私问题上非常重要,尽管这不会给我带来任何麻烦,但我宁愿不去处理。

create table set_divisions as       
SELECT DISTINCT CAMPAIGNCODE, OFFERCODE, DIV        
FROM
(
    SELECT DISTINCT CAMPAIGN, OFFER, DIVISION
    ROWNUMBER() OVER
    (PARTITION BY CAMPAIGNCODE, OFFERCODE, ORDER BY DIVISION) AS RN
    FROM (SELECT DISTINCT CAMPAIGN, OFFER, DIVISION)
                case
                when upper(URL) like '%NORTH%'
                AND DIVISION IS NULL THEN 'NORTH'
                when upper(URL) like '%SOUTH%' 
                AND DIVISION IS NULL then 'SOUTH'
                when upper(URL) like '%EAST%'  
                AND DIVISION IS NULL then 'EAST'
                when upper(URL) like '%WEST%'  
                AND DIVISION IS NULL then 'WEST'
                ELSE DIVISION
                END AS DIV      
    FROM
    (
        SELECT DISTINCT CAMPAIGN, OFFER, DIVISION,
        CASE WHEN DIVISION IS NULL THEN LINK_URL
        ELSE NULL 
        END AS URL          
        FROM 
            (
                select campaign, offer, linkurl
                case
                when upper(link) like '%NORTH%' then 'NORTH'
                when upper(link) like '%SOUTH%' then 'SOUTH'
                when upper(link) like '%EAST%'  then 'EAST'
                when upper(link) like '%WEST%'  then 'WEST'
                ELSE NULL
                END AS DIVISION
                FROM TABLE A
                group by campaign, offer, linkurl
                case
                when upper(link) like '%NORTH%' then 'NORTH'
                when upper(link) like '%SOUTH%' then 'SOUTH'
                when upper(link) like '%EAST%'  then 'EAST'
                when upper(link) like '%WEST%'  then 'WEST'
            )               
    )                       
)   
    WHERE RN = 1
    OR (RN >1
        AND DIV IS NOT NULL);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当对SQL UPDATE语句使用CASE时,为什么将NULL值输入数据库? - Why is the value NULL entered into my database when I use CASE for my SQL UPDATE statement? 如果语句返回true而它应该是假的..我想 - If statement returning true while it should be false.. i think 为什么MySQL CASE语句返回NULL列? - Why is MySQL CASE statement returning NULL columns? 当 Archived_Date 为 NULL 并在 Age 列中返回 NULL 值时未计算 Statement Age 的情况 - Case When Statement Age not calculating when Archived_Date is NULL and returning NULL value in Age column 如果不为空,则返回 if 语句的值 - Return the value of if statement in case when it's not null 输入值为NULL时,多个CASE语句不起作用 - Multiple CASE statements don't work when input value(s) are NULL 为什么即使我的专栏中的 email adrees 没有值为 0,我的查询也会得到结果 - Why I am getting results in my query even when I don't have value 0 for email adrees in my column 如果我不知道字段名称,例如在选择查询中仅使用*,是否可以替换null - Is it possible to replace null in case when I don't know field name like simply using * in select query 为什么我使用case语句在第二个计数器中获得Null值 - Why i get Null values in my second counter using case statement 为什么这个 CASE GREATEST 返回 NULL? - Why is this CASE GREATEST returning NULL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM