选择具有不同查询的相同列(where子句)

时间:2014-05-26 14:39:38

标签: tsql union

我试图在同一个查询中第二次选择相同的列但是具有其他条件(其中) 列@columnname应选择为max,而lo.PlantID ='' @ deviceid''而作为AVG而lo.PlantID!='' @ deviceid''

所以我的查询看起来像这样:

  DECLARE @sqlCommand nvarchar(max) ='SELECT ''@sitename'',''@devicename'',''@devicetypename'',''@location'',''@deviceSerial'',
  ''@lcname'', CAST(MAX(@columnname) AS FLOAT), 0,0
  FROM [DBLocation@location].[dbo].['+ @TableName +'] AS lo
  Where lo.PlantID = ''@deviceid'' AND lo.TIMESTAMP < DATEADD(MINUTE, -60, CURRENT_TIMESTAMP);'

 SET @sqlCommand = REPLACE(@sqlCommand,'@deviceid',@DeviceID);
 SET @sqlCommand = REPLACE(@sqlCommand,'@deviceSerial',@DeviceSerial);
 SET @sqlCommand = REPLACE(@sqlCommand,'@lcname',@LCName);
 SET @sqlCommand = REPLACE(@sqlCommand,'@columnname',@columnName);
 SET @sqlCommand = REPLACE(@sqlCommand,'@sitename',@sitename);
 SET @sqlCommand = REPLACE(@sqlCommand,'@devicename',@Devicename);
 SET @sqlCommand = REPLACE(@sqlCommand,'@location',@location);
 SET @sqlCommand = REPLACE(@sqlCommand,'@devicetypename',@Devicetypename);

 PRINT @sqlCommand;

 INSERT INTO tempdb.dbo.tempresult
 EXEC sys.sp_executesql @sqlCommand; 

我正在尝试这样的事情

 Select * FROM (
 SELECT ''@sitename'',''@devicename'',''@devicetypename'',''@location'',''@deviceSerial'',
''@lcname'', CAST(MAX(@columnname) AS FLOAT), 0
FROM [DBLocation@location].[dbo].['+ @TableName +'] AS lo
Where lo.PlantID = ''@deviceid'' AND lo.TIMESTAMP < DATEADD(MINUTE, -60, CURRENT_TIMESTAMP)

Union ALL

  SELECT CAST(AVG(@columnname) AS FLOAT)
 FROM [DBLocation@location].[dbo].['+ @TableName +'] AS loc
 Where loc.PlantID != ''@deviceid'' AND loc.TIMESTAMP < DATEADD(MINUTE, -60, CURRENT_TIMESTAMP))
 InnerQuery'
 ...

如果有人能指出正确的分数,这将是不错的。 感谢

2 个答案:

答案 0 :(得分:0)

您可以使用case语句根据PlantID在MAX和AVG之间进行选择。

 Select * FROM (
 SELECT ''@sitename'',''@devicename'',''@devicetypename'',''@location'',''@deviceSerial'',
 ''@lcname'', 

 CASE WHEN lo.PlantId = ''@deviceid'' then CAST(MAX(@columnname) AS FLOAT)
     ELSE CAST(AVG(@columnname) AS FLOAT) end

, 0
FROM [DBLocation@location].[dbo].['+ @TableName +'] AS lo
Where lo.TIMESTAMP < DATEADD(MINUTE, -60, CURRENT_TIMESTAMP)

答案 1 :(得分:0)

如果有人有兴趣,我已经解决了这个问题

DECLARE @sqlCommand nvarchar(max) ='
with cte as
(select ''@DeviceId'' as PlantId, AVG(@columnname) as AverageValue FROM [DBLocation@location].
[dbo].['+ @TableName +'] where PlantID <> ''@DeviceID'' AND TIMESTAMP < DATEADD(MINUTE, -60,
CURRENT_TIMESTAMP) )
SELECT ''@sitename'',''@devicename'',''@devicetypename'',''@location'',''@deviceSerial'', 
''@lcname'', MAX(@columnname), 0,MAX(cte.AverageValue),0FROM [DBLocation@location].[dbo].['+ 
@TableName +'] AS lo
INNER JOIN cte ON lo.PlantId = cte.PlantId
Where lo.TIMESTAMP < DATEADD(MINUTE, -60, CURRENT_TIMESTAMP)
group by lo.PlantID;'

SET @sqlCommand = REPLACE(@sqlCommand,'@deviceid',@DeviceID);
SET @sqlCommand = REPLACE(@sqlCommand,'@deviceSerial',@DeviceSerial);
SET @sqlCommand = REPLACE(@sqlCommand,'@lcname',@LCName);
SET @sqlCommand = REPLACE(@sqlCommand,'@columnname',@columnName);
SET @sqlCommand = REPLACE(@sqlCommand,'@sitename',@sitename);
SET @sqlCommand = REPLACE(@sqlCommand,'@devicename',@Devicename);
SET @sqlCommand = REPLACE(@sqlCommand,'@location',@location);
SET @sqlCommand = REPLACE(@sqlCommand,'@devicetypename',@Devicetypename);

PRINT @sqlCommand;

INSERT INTO tempdb.dbo.tempresult
EXEC sys.sp_executesql @sqlCommand;