SQL-根据日期和批号选择列

时间:2013-06-13 17:51:29

标签: sql sql-server select case

我很接近,现在我遇到了这段代码的问题。它说我需要在某处添加存在?谢谢你的帮助!

SELECT (SELECT UA#SacmiDataLog_2013.NS_Inlet_T
                  , UA#SacmiDataLog_2013.NSFlowRate
                  , UA#SacmiDataLog_2013.NSHeaterTank
                  , UA#SacmiDataLog_2013.NS_Outlet_T
                  , UA#SacmiDataLog_2013.NSTrPrAvg
                  , UA#SacmiDataLog_2013.NSPump1
                  , UA#SacmiDataLog_2013.NSPump2
                  , UA#SacmiDataLog_2013.NS_Visc
                  , UA#SacmiDataLog_2013.NS_Burner_B
             WHERE
               UA#SacmiDataLog_2013.NSHeatBatch = 'G23169') AS expr1
          , (SELECT UA#SacmiDataLog_2013.OS_Inlet_T
                  , UA#SacmiDataLog_2013.OS_Outlet_T
                  , UA#SacmiDataLog_2013.OS_Burner_B
                  , UA#SacmiDataLog_2013.OSViscosity
                  , UA#SacmiDataLog_2013.OSHeaterTank
                  , UA#SacmiDataLog_2013.OSPump1
                  , UA#SacmiDataLog_2013.OSPump2
                  , UA#SacmiDataLog_2013.OSTrPrAvg
             WHERE
               UA#SacmiDataLog_2013.OSHeatBatch = 'G23169') AS expr2

这   dbo.UA#SacmiDataLog_2013

2 个答案:

答案 0 :(得分:0)

你是否尝试过这种方式:

where (NSHeatBatch= 'G22166' or OSHeatBatch= 'G22166') 
and RunDate between '1/14/13' and '1/16/13'

很多时候,你的大括号是定义查询这么重要的事情。让我们这样试试......!

答案 1 :(得分:0)

然后,Do喜欢这个,我希望这会有所帮助:

SELECT  * FROM( 
    SELECT NSField1 as DataColumnNameSame1,NSField2 as DataColumnNameSame2 FROM SAMETABLE AS NS WHERE NS.BATCH='111111' AND RUNDATE BETWEEN '1/14/13' AND '1/16/13' 

UNION ALL

    SELECT OSField1 as DataColumnNameSame1,OSField2 as DataColumnNameSame2 FROM SAMETABLE AS NS WHERE OS.BATCH='111111' AND RUNDATE BETWEEN '1/14/13' AND '1/16/13' 

) AS DATA 
    --WHERE CRITERIA IF NECESSARY IN THIS LEVEL TOO, ONCE AGAIN YOU CAN FILTER FROM HERE ALSO IF NEEDED. HOPE THIS HELPS.