根据条件将Select中的两列合并为一列

时间:2017-12-06 22:41:35

标签: sql sql-server ssms

我有一个module.exports = class MyHelper extends codecept_helper { _before() { let browser = this.helpers['WebDriverIO'].browser; browser.addCommand('jsClick', function(selector, cb) { browser.execute(function(cssSelector) { var clickEvent = document.createEvent("MouseEvent"); clickEvent.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null); document.querySelector(cssSelector).dispatchEvent(clickEvent); }, selector, cb); }); } jsClick(selector) { return this.helpers['WebDriverIO'].browser.jsClick(selector); } },我希望根据以下条件进行合并:如果左边的列为null,则应选择正确的列,但当左列不为空时,应始终选择左

场景

我左边的是我现在拥有的东西,我想把桌子放在右边

AlphabetTable

SELECT

上面左表的SQL

+-------+------+    +---------+
| A     | B    |    | Desired |
+-------+------+    |---------+
| null  | 1    |    | 1       |
| null  | 1    |    | 1       |
|  2    | null |    | 2       |
|  2    | 3    |    | 2       |
|  2    | 5    |    |  2      |
|  null | null |    | null    |
+-------+------+    +---------+

到目前为止我所拥有的是什么

SELECT 
    CAST((select sum(Total) from LettersTable LT
                                WHERE LT.ID = ID
                                AND LT.Count > 5) 
                                AS INT) as A
    CAST((select sum(Total) from LettersTable LT
                                WHERE LT.ID = ID
                                AND LT.Count > 5) 
                                AS INT) as B
From IDTable

4 个答案:

答案 0 :(得分:1)

这里有几个选项..可能

;with mycte as (
select null as a, 1 as b
union all
select null as a, 1 as b
union all
select 2 as a, null as b
union all
select 2 as a, 3 as b
union all
select 2 as a, 5 as b
union all
select null as a, null as b

)

Select 
*,
CONCAT(a,case when b is not null and a is not null then (select null) else b end) case_statement
,COALESCE (a,b)  coalesce_statement
 from mycte

答案 1 :(得分:1)

假设您有A列和B列可用,则可以使用以下其中一项:

SELECT 
CASE WHEN A IS NULL THEN B ELSE A END AS DESIRED_COL    
FROM
YOUR_TABLE;

或者@NetMage建议:

SELECT 
CASE WHEN (A IS NOT NULL OR B IS NOT NULL) THEN COALESCE(A,B) ELSE NULL END AS DESIRED_COL
FROM YOUR_TABLE;

由于如果两列都为NULL,COALESCE将抛出错误,您仍然需要处理该条件的情况。

答案 2 :(得分:1)

您可以使用ISNULL条件实现此目的,如下所示

    SELECT 
        CAST((select sum(Total) from LettersTable LT
                                    WHERE LT.ID = ID
                                    AND LT.Count > 5) 
                                    AS INT) as A,
        CAST((select sum(Total) from LettersTable LT
                                    WHERE LT.ID = ID
                                    AND LT.Count > 5) 
                                    AS INT) as B
    INTO #tmpIDTable
    From IDTable

    SELECT ISNULL(A,B) FROM #tmpIDTable

答案 3 :(得分:0)

试试这个

;with mycte as (
select null as a, 1 as b
union all
select null as a, 1 as b
union all
select 2 as a, null as b
union all
select 2 as a, 3 as b
union all
select 2 as a, 5 as b
union all
select null as a, null as b

)
SELECT ISNULL(a,b) FROM mycte