查询1:
select B.TimeSpan as ReportDate,(C.InFlux) InFlux,(C.OutFlux) OutFlux,(C.InFlux+C.OutFlux) TotalFlux
    from NRC_TimeSpan as B ,NRC_TransactionMinuteTotal as C
    where B.TimeSpan*=C.TransactionDateTime and C.TransactionDateTime>='2009-1-1 00:00' and C.TransactionDateTime<'2009-1-1 00:05' and TimeSpan>='2009-1-1 00:00' and TimeSpan<'2009-1-1 00:05' and B.SpanType=1 order by B.TimeSpan
结果正常:
2009-01-01 00:00:00.000 NULL NULL NULL
2009-01-01 00:01:00.000 NULL NULL NULL
2009-01-01 00:02:00.000 NULL NULL NULL
2009-01-01 00:03:00.000 0 1 1
2009-01-01 00:04:00.000 NULL NULL NULL
查询2(将1改写为join):
select B.TimeSpan as ReportDate,(C.InFlux) InFlux,(C.OutFlux) OutFlux,(C.InFlux+C.OutFlux) TotalFlux
    from NRC_TimeSpan as B left join NRC_TransactionMinuteTotal as C
    on B.TimeSpan=C.TransactionDateTime  where C.TransactionDateTime>='2009-1-1 00:00' and C.TransactionDateTime<'2009-1-1 00:05'  and TimeSpan>='2009-1-1 00:00' and TimeSpan<'2009-1-1 00:05' and B.SpanType=1 order by B.TimeSpan
结果异常:
2009-01-01 00:03:00.000 0 1 1
两个表的数据如下:
NRC_TimeSpan:
select * from NRC_TimeSpan B where SpanType=1 and TimeSpan>='2009-1-1 00:00' and TimeSpan<'2009-1-1 00:05'
1091372 2009 1 2009-01-01 00:00:00.000
1091373 2009 1 2009-01-01 00:01:00.000
1091374 2009 1 2009-01-01 00:02:00.000
1091375 2009 1 2009-01-01 00:03:00.000
1091376 2009 1 2009-01-01 00:04:00.000
NRC_TransactionMinuteTotal:
select * from NRC_TransactionMinuteTotal C where C.TransactionDateTime>='2009-1-1 00:00' and C.TransactionDateTime<'2009-1-1 00:05'
168488 2 NULL 3 中胜 NULL 0 1 .00 2009-01-01 00:03:00.000 2009-01-01 00:00:00.000实际上,将left join换成right join,join结果没变化.问题出在哪呢?

解决方案 »

  1.   

    select B.TimeSpan as ReportDate,(C.InFlux) InFlux,(C.OutFlux) OutFlux,(C.InFlux+C.OutFlux) TotalFlux 
        from NRC_TimeSpan as B left join NRC_TransactionMinuteTotal as C 
        on B.TimeSpan=C.TransactionDateTime and C.TransactionDateTime>='2009-1-1 00:00' and C.TransactionDateTime <'2009-1-1 00:05' 
     where  TimeSpan>='2009-1-1 00:00' and TimeSpan <'2009-1-1 00:05' and B.SpanType=1 order by B.TimeSpan 
      

  2.   

    要把C表的条件全部写道ON部分,否则自动变成INNER JOIN