如何在DB2中用NULL替换NULL?

时间:2018-05-18 15:33:32

标签: sql replace null db2

我试图在“Antaltillgängligacpråk”列中将NULL替换为整数值1(如果在嵌套的sql-statment中完成,则为0)。有些行得到正确的值,但是som得到NULL。我想这是因为我的嵌套sql语句只返回匹配我的where子句的内容。有一些bokid女巫没有来自xpath-expression的任何Språk。

  • 我尝试过使用SELECT COALESCE(COUNT(Språk)+1,1)AS“Antal tillgängligsspråk“,但那没用。
  • 我也试过SELECT IFNULL(COUNT(Språk)+1,1)AS“Antal tillgängligsspråk“,但这也不起作用。
  • 我也试过更改嵌套中的where子句 sql-statment使用FULL JOIN女巫也没有用。

现在我没有想法,有人可以帮助我吗?

SELECT DISTINCT Title AS "Titel", 
    OriginalLanguage AS "Orginalspråk", 
    Genre AS "Genre", 
    (SELECT COUNT(Id)
    FROM Edition
    WHERE Edition.Book = Book.Id) AS "Antal upplagor",
    (SELECT COUNT(Språk)+1 AS "Antal tillgängliga språk"
    FROM (SELECT Book.Id AS bokid, Språk
        FROM Edition, XMLTABLE('$TRANSLATIONS//Translation/@Language' 
            COLUMNS Språk VARCHAR(20) PATH '.'), Book
        WHERE Edition.Book = Book.Id
        GROUP BY Språk, Book.Id)
    WHERE bokid = Book.Id
    GROUP BY bokid),
    (SELECT COUNT(Author)
    FROM Authorship
    WHERE Authorship.Book = Book.Id) AS "Antal författare",
    (SELECT MIN(Year)
    FROM Edition
    WHERE Edition.Book = Book.Id) AS "År första upplaga"
FROM Book

+1是计算所有可用的书籍语言,所有翻译+原始语言。

3 个答案:

答案 0 :(得分:1)

将您的COALESCE置于子选择之外

SELECT DISTINCT Title AS "Titel", 
    OriginalLanguage AS "Orginalspråk", 
    Genre AS "Genre", 
    COALESCE((SELECT COUNT(Id)
    FROM Edition
    WHERE Edition.Book = Book.Id),1) AS "Antal upplagor",
    COALESCE((SELECT COUNT(Språk)+1 AS "Antal tillgängliga språk"
    FROM (SELECT Book.Id AS bokid, Språk
        FROM Edition, XMLTABLE('$TRANSLATIONS//Translation/@Language' 
            COLUMNS Språk VARCHAR(20) PATH '.'), Book
        WHERE Edition.Book = Book.Id
        GROUP BY Språk, Book.Id)
    WHERE bokid = Book.Id
    GROUP BY bokid),1),
    COALESCE((SELECT COUNT(Author)
    FROM Authorship
    WHERE Authorship.Book = Book.Id),1) AS "Antal författare",
    COALESCE((SELECT MIN(Year)
    FROM Edition
    WHERE Edition.Book = Book.Id),1) AS "År första upplaga"
FROM Book

答案 1 :(得分:0)

查询也可以这样写。在某种程度上,你更喜欢做事情,但通常相关的子选择不容易调试,并且通常不像加入单独的选择一样清晰。

-R

答案 2 :(得分:0)

就个人而言,我会将查询重构为(UNTESTED)

之类的内容

我宁愿为内存中的所有记录获取一次数据,而不是为每一行运行查询。在表格规模增长之前,绩效差异无关紧要;虽然Språk一代可能会导致性能降级的通知尽快实现。

SELECT DISTINCT Title AS "Titel", 
    OriginalLanguage AS "Orginalspråk", 
    Genre AS "Genre", 
    "Antal upplagor",
    coalesce("Antal tillgängliga språk",0) AS "Antal tillgängliga språk",
    coalesce("Antal författare", 0) AS "Antal författare",
    coalesce("År första upplaga",0) AS "År första upplaga"
FROM Book
LEFT JOIN (SELECT COUNT(Språk)+1 AS "Antal tillgängliga språk", bokid
           FROM (SELECT Book.Id AS bokid, Språk
                 FROM Edition
                    , XMLTABLE('$TRANSLATIONS//Translation/@Language' COLUMNS Språk VARCHAR(20) PATH '.')
                 INNER JOIN Book
                    ON Edition.Book = Book.Id
                 GROUP BY Språk, Book.Id)
           GROUP BY bokid)  BE
  ON BE.bokid = Book.Id
LEFT JOIN  (SELECT Book, COUNT(Author) AS "Antal författare"
            FROM Authorship
            GROUP BY Book)  A
  ON A.Book = Book.Id
LEFT JOIN  (SELECT Book, MIN(Year) AS "År första upplaga", Count(ID) AS "Antal upplagor"
            FROM Edition
            GROUP BY Book) E
  ON E.Book = Book.Id