如何将空数据连接到SQL Server

时间:2016-04-02 13:39:19

标签: sql-server sql-server-2012

我有一张桌子:

declare @Fromdate date;
declare @ToDate date;
set @Fromdate='2016-01-01'
set @ToDate='2016-01-11'

SELECT 
    MTime, CDate, MNAME, 
    tblCowMilk.MemNo,
    ISNULL(Liters, 0.0) as Litres, 
    ISNULL(FAT, 0.0) as FAT, 
    ISNULL(SNF, 0.0) as SNF,
    ISNULL(MRate, 0.0) as MRate, 
    ISNULL(Amount, 0.0) as Amount,
    SUM(Liters) as Total_Litres,
    SUM(Amount) as Total_Amount 
FROM 
    tblCowMilk 
INNER JOIN 
    TBLMEMBERS ON tblCowMilk.MemNo = TBLMEMBERS.MNO 
WHERE
    CDate between @FromDate and @ToDate 
    AND MType = 'C' 
    AND MemNo = 52
GROUP BY 
    MTime, CDate, MNAME, tblCowMilk.MemNo, Liters, FAT, SNF, Amount, MRate   

Table 1 Result

查询#2:

SELECT DISTINCT 
    MTime, CDATE, 
    NULL AS MNAME, NULL AS MemNo,
    NULL as Litres, NULL as FAT,
    NULL AS SNF, NULL as MRate,
    NULL as Amount, NULL as Total_Litres,
    NULL as Total_Amount 
FROM 
    tblCowMilk 
--INNER JOIN TBLMEMBERS ON tblCowMilk.MemNo = TBLMEMBERS.MNO 
WHERE
    CDate between @FromDate and @ToDate 
    AND MType = 'C' 
GROUP BY 
    cdATE, MTIME

Table 2 Result

我想加入这两个表并获得以下结果:

  • 如果table1有行值,它将替换Table2 null值
  • 否则返回空行值
  • 我想要第二个表格格式的结果(11天是固定的)。如果记录在第一个表格中,那么以下面的方式替换相应的位置第二个表格:

    E 2016-01-01 SHANMUGAM 52 1.900 3.0 7.8 23.04 43.78 1.900 43.78 E 2016-01-02 NULL NULL NULL NULL NULL NULL NULL NULL NULL E 2016-01-03 SHANMUGAM 52 3.000 4.0 8.5 27.15 81.45 3.000 81.45 E 2016-01-04 SHANMUGAM 52 4.000 3.9 8.4 26.70 106.80 4.000 106.80 E 2016-01-05 SHANMUGAM 52 4.300 4.4 8.2 27.37 117.69 4.300 117.69 E 2016-01-06 NULL NULL NULL NULL NULL NULL NULL NULL NULL E 2016-01-07 SHANMUGAM 52 7.000 4.1 8.2 26.70 186.90 7.000 186.90 E 2016-01-08 SHANMUGAM 52 3.000 4.1 8.2 26.70 80.10 3.000 80.10 E 2016-01-09 SHANMUGAM 52 3.000 4.0 8.0 26.03 78.09 3.000 78.09 E 2016-01-10 SHANMUGAM 52 3.000 3.9 8.1 26.03 78.09 3.000 78.09 E 2016-01-11 SHANMUGAM 52 24.000 8.0 8.0 34.99 839.76 24.000 839.76 M 2016-01-01 NULL NULL NULL NULL NULL NULL NULL NULL NULL M 2016-01-02 SHANMUGAM 52 7.000 4.1 8.2 26.70 186.90 7.000 186.90 M 2016-01-03 SHANMUGAM 52 4.000 4.0 8.2 26.48 105.92 4.000 105.92 M 2016-01-04 NULL NULL NULL NULL NULL NULL NULL NULL NULL M 2016-01-05 SHANMUGAM 52 1.000 8.0 8.0 34.99 34.99 1.000 34.99 M 2016-01-06 SHANMUGAM 52 28.000 4.1 8.0 26.25 735.00 28.000 735.00 M 2016-01-07 NULL NULL NULL NULL NULL NULL NULL NULL NULL M 2016-01-08 NULL NULL NULL NULL NULL NULL NULL NULL NULL M 2016-01-09 SHANMUGAM 52 31.000 4.0 8.0 26.03 806.93 31.000 806.93 M 2016-01-10 SHANMUGAM 52 3.000 3.3 7.8 23.71 71.13 3.000 71.13 M 2016-01-11 SHANMUGAM 52 1.000 7.0 7.8 32.00 32.00 1.000 32.00

2 个答案:

答案 0 :(得分:1)

表2查询:

SELECT DISTINCT MTime,CDATE,NULL AS MNAME,NULL AS MemNo,NULL as Litres
     , NULL as FAT,NULL AS SNF,NULL as MRate, NULL  as Amount
     , NULL as Total_Litres, NULL as Total_Amount 
FROM tblCowMilk 
LEFT JOIN TBLMEMBERS 
   ON tblCowMilk.MemNo = TBLMEMBERS.MNO 
  and MType='C' 
where CDate between @FromDate and @ToDate 
GROUP BY cdATE,MTIME

现在强制系统选择系列中的第一个非空值,只需对要使用的值使用coalesce()

所以也许...... coalesce(tblmembers.litres, tblCowMil.litres,0)就是一个例子。如果members.litres不是空的,请使用它,否则使用tblcowmilk.litres如果它不为空,否则放0

我会让你们完成所有这些操作,因为我不确定每张桌子中哪些匹配。

当您想要一个表中的所有结果而只需要与另一个表匹配时,您需要使用OUTER连接。

  • LEFT JOIN - 仅包括第一个表中的记录和第二个表中匹配的记录。
  • RIGHT JOIN - 仅包括第二张桌子上的那些记录,只包括与第一张记录匹配的记录。
  • FULL OUTER - 即使找不到匹配项,也包括所有表中的所有记录。

如果你考虑数据库引擎在幕后做什么,那么将标准移动到连接的需要是有意义的。

首先,引擎必须生成组合记录集

所以,如果我们有表

A      B
ID     AID Property
1      1   A  
2      2   B
3

我们使用左连接

连接这些表
Select * from A LEFT JOIN B on A.ID = B.AID 

AID  BAID   Property
1    1      A
2    2      B
3

现在,如果我们对财产实施限制..

Select * from A LEFT JOIN B on A.ID = B.AID  where B.Property = 'A'

AID  BAID   Property
1    1      A

记录3去了哪里?这是因为NULL既不是=' A'也不等于' A'它是NULL。所以这实际上和内连接一样。

因此,为了获得记录,我们必须将外侧表上的限制(匹配的那些记录)移动到连接标准,以便在连接的同时应用限制。操作顺序是进行连接然后是where子句。

Select * from A LEFT JOIN B on A.ID = B.AID  and B.Property = 'A'

AID  BAID   Property
1    1      A
3  

答案 1 :(得分:0)

.cheap-watcher {  //this is main container properties in which everything is injected
    position: fixed;
    top: 0px;
    right: 0px;
    width: 360px;
    height: 100%;
    max-width: 360px;
    max-height: 100%;
    min-height: 100%;
    background-color: #f1f3f4;
    float: right;

    .header {
        height: 7.57%;
        background-color: #00a8e8;
    }

    .price_found {
        padding-top: 16px;
        height: 10.169%;
        background-color: #fff;
        text-align: center;
    }

    .settings {
        height: 4.971%;
        width: 100%;
        display: inline-block;
    }

     .main_container {
        width: 360px;
        background-color: #fff;

        .container {
            width: 360px;
            height: 57.856%;
        }
    }

    .total_select {        
        height: 7.57%;
        width: 100%;
        background-color: #fff;
        text-align: center;
        border-top: solid;
        border-top-color: #e8e8e8;
        border-top-width: 2px;
    }

    .menu_footer {
        height: 11.864%;
        width: 100%;
    }
}