MSSQL查询错误

时间:2015-08-18 13:37:15

标签: sql-server sql-server-2008 stored-procedures

我想访问两个表中的数据,其中每小时都有数据。我正在使用联合查询来处理此问题,如下所示

( SELECT T.unit1,t.CurrentDate,t.CurrentTime,loc,Unit2,value,Description FROM fore T  WHERE   loc=@loc AND CurrentDateTIME =@c0 AND quan='CO'  
AND NOT EXISTS( SELECT 1 FROM audit WHERE  startdate<=CAST(@c0dt AS DATE) AND StartTime<=CAST(@c0dt AS TIME) AND (ENDDATE >= CAST(@c0dt AS DATE) OR EndDate IS NULL )
AND (ENDTIME>=CAST(@c0dt AS TIME) OR enDtime IS NULL ) AND loc=@loc AND unit1='CO')

UNION 

SELECT TOP 1 T.unit1,t.CurrentDate,t.CurrentTime,loc,'' as Unit2 ,CAST(VALUE AS SMALLINT) value ,Description FROM  audit T WHERE startdate<=CAST(getdate() AS DATE) 
AND StartTime <= CAST(CURRENT_TIMESTAMP AS TIME)AND (enddate>=getdate()  OR ENDDATE IS NOT NULL )   AND (endTime>=CAST(GETDATE() AS TIME) OR enDtime IS NULL) 
AND loc=@loc AND unit1='CO')

我正在访问的表格如下:

表审核:

id  currentdate     currentdatetime             currenttime     loc     unit1   value   Desc    startdate   startdatetime           starttime   enddate     enddate     enddatetime 
1   2015-08-19      2015-08-19 18:00:06.000     18:00:00        B1      C0      40.0000         2015-08-19  2015-08-19 18:00:00.000 18:00:00    2015-08-18  18:00:00    2015-08-18 18:00:00.000

表格前言:

currentdate     currenttime     loc     unit1   unit2   concern     value   description currentdatetime
2015-08-19      15:00:00        B1      C0      m       .1700       5       Good        2015-08-19 15:00:00.000
2015-08-19      16:00:00        B1      C0      m       14.7000     35      Good        2015-08-19 16:00:00.000
2015-08-19      17:00:00        B1      C0      m       6.5600      7       Good        2015-08-19 17:00:00.000
2015-08-19      19:00:00        B1      C0      m       19.6000     20      Good        2015-08-19 19:00:00.000

上面两个表的描述如下表所示:Fore包含来自每小时转储的数据,而table:audit包含用户修改日期,如果数据不可用于该小时用户手动输入数据。 / p>

我想编写一个联合,它应该从前端得到c0dt @c0dt datetime的数据。所以问题是当我运行上面的查询时,我没有从审计表中获取数据,该数据是在那个小时手动输入的。

2 个答案:

答案 0 :(得分:1)

只是好奇这是否有效:我以为我记得在某个地方读过带有TOP的联盟在MSSQL中运作不好......我试图找到这篇文章。认为它与顶级工作所需的顺序有关,但是顺序只能在两个数据集的并集上定义......绕过它的方法是使用CTE或内联视图...这是为什么我问这个CTE示例是否有效。

WITH A AS ( SELECT 
    T.unit1
    ,t.CurrentDate
    ,t.CurrentTime
    ,loc
    ,Unit2
    ,value
    ,Description 
  FROM fore T  
  WHERE   loc=@loc 
  AND     CurrentDateTIME =@c0 
  AND quan='CO'  
  AND NOT EXISTS(   SELECT 1 
                    FROM audit 
                    WHERE  startdate<=CAST(@c0dt AS DATE) 
                    AND StartTime<=CAST(@c0dt AS TIME) 
                    AND (ENDDATE >= CAST(@c0dt AS DATE) 
                         OR EndDate IS NULL )
                    AND (ENDTIME>=CAST(@c0dt AS TIME) 
                         OR enDtime IS NULL ) 
                    AND loc=@loc 
                    AND unit1='CO'),
B as (SELECT TOP 1 
    T.unit1
    ,t.CurrentDate
    ,t.CurrentTime
    ,loc
    ,'' as Unit2 
    ,CAST(VALUE AS SMALLINT) value 
    ,Description 
  FROM  audit T 
  WHERE startdate<=CAST(getdate() AS DATE) 
  AND StartTime <= CAST(CURRENT_TIMESTAMP AS TIME)
  AND (enddate>=getdate()  
       OR ENDDATE IS NOT NULL ) 
  AND (endTime>=CAST(GETDATE() AS TIME) 
       OR enDtime IS NULL) 
  AND loc=@loc 
  AND unit1='CO'))
SELECT * from A
UNION ALL
SELECT * FROM B;

答案 1 :(得分:0)

有了这些数据和查询,我认为你不会得到任何结果。在startdate之前你有一个enddate。

PS:你有一个特殊的设计,冗余地具有相同的日期,时间和日期时间字段。