SQL:如何为单个isull场景在列中创建2个单独的值?

时间:2018-06-20 04:04:47

标签: sql sql-server

我正在测试如下所示的SQL命令:

with 
AllEvents AS 
           (
             SELECT E.EventName 
               FROM dbo.tblEvtDur E 
           GROUP BY E.EventName
           ), 
  EventDurations AS 
                   (
                    SELECT macstring, E.EventName, SUM(DATEDIFF(SECOND, E.StartDT, E.EndDT)) 
                        AS DIFF 
                      FROM dbo.tblEvtDur E 
                Inner Join 
                          (
                           Select Distinct SUBSTRING(MacNm , 1,3) as MacString, MacID                 
                             From dbo.tblMachine)M on E.MacID = M.MacID  
                            Where E.DayID BETWEEN '20180501' 
                              And '20180531' 
                         Group By macstring, E.EventName
                           )

          Select MacString, AE.EventName, ISNULL(ED.DIFF,0) as DIFF 
            From AllEvents AE 
       Left Join EventDurations ED 
              On AE.EventName = ED.EventName 
        Order By EventName, MacString 

我得到的结果可以在这里看到:

MacString   EventName   DIFF
NULL        BLK         0
COR         MDL         359880
FRA         MDL         1805253
COR         MTL         903783
FRA         MTL         324831
COR         OFF         6862901
FRA         OFF         9905131
COR         RPR         2986153
FRA         RPR         5799318
COR         RUN         7509506
FRA         RUN         11250157

为空,因为在DayID范围内未使用EventName-BLK。现在,我要使它成为MacString在存在NULL值时将为“ COR”和“ FRA”显示2行。

像这样:

MacString   EventName   DIFF
NULL        BLK         0
|
| (become)
v
COR         BLK         0
FRA         BLK         0

如果检测到NULL,我尝试使用ISNULL方法定义值:

--modification in last row
   Select isnull(MacString, 'COR') 
       As MacString, AE.EventName, ISNULL(ED.DIFF,0) 
       As DIFF 
     From AllEvents AE 
Left Join EventDurations ED 
       On AE.EventName = ED.EventName 
 Order By EventName, macstring 

但是它似乎只允许一个输入。有什么方法可以使用ISNULL在MacString列中创建带有“ COR”和“ FRA”的两行吗?

1 个答案:

答案 0 :(得分:1)

尝试针对LEFT JOIN的硬编码列表对NULL值进行MacString

;with AllEvents AS 
(
    SELECT 
        E.EventName 
    FROM 
        dbo.tblEvtDur E 
    GROUP BY 
        E.EventName
), 
EventDurations AS 
(
    SELECT 
        macstring, 
        E.EventName, 
        SUM(DATEDIFF(SECOND, E.StartDT, E.EndDT)) AS DIFF 
    FROM 
        dbo.tblEvtDur E 
        Inner Join 
        (
            Select Distinct 
                SUBSTRING(MacNm , 1,3) as MacString, 
                MacID                 
            From 
                dbo.tblMachine
        )M on E.MacID = M.MacID  
    Where 
        E.DayID BETWEEN '20180501' And '20180531' 
    Group By 
        macstring, 
        E.EventName
),
NullMacStrings AS
(
    SELECT
        MacString = 'COR'
    UNION ALL
    SELECT
        MacString = 'FRA'
)
Select 
    MacString = ISNULL(ED.MacString, NM.MacString),
    AE.EventName, 
    ISNULL(ED.DIFF,0) as DIFF 
From 
    AllEvents AE 
    Left Join EventDurations ED On AE.EventName = ED.EventName
    LEFT JOIN NullMacStrings AS NM ON ED.MacString IS NULL
Order By 
    EventName, 
    MacString