如何在mssql 2008中的case语句中使用子查询

时间:2012-07-25 14:22:26

标签: sql sql-server sql-server-2008 subquery case

我需要使用带有子查询的select case语句将else语句更改为单个查询...

 if((select COUNT(*) from pseb.dbo.meterattributedetails where meterid=@meterid)=0)
  select @emf=EMF from pseb.dbo.METERMASTER where MeterID=@meterid
  else  if((select COUNT(*) from pseb.dbo.meterattributedetails where meterid=@meterid and dateadd(day,1,@fromdate)<DateTime)>0) 
    select top 1 @emf=oldvalue from pseb.dbo.meterattributedetails  where MeterID=@meterid and dateadd(day,1,@fromdate)<datetime order by DateTime 
     else 
  select top 1 @emf=newvalue from pseb.dbo.meterattributedetails  where meterid=@meterid and DateTime<@fromdate order by DateTime desc

meterattribute表结构如下:

AttributeID AttributeName   Oldvalue    newvalue    DateTime    meterid
1           EMF         2.00000         4.00000        2012-07-05   4756
1           EMF         4.00000         6.00000        2012-07-10   4756
1           EMF         6.00000         8.00000        2012-07-15   4756
1           EMF         8.00000        10.00000        2012-07-25   4756

在该表中,datetime指的是emf更改日期,如果我的输入是'2012-07-17'意味着我必须获取该特定日期的新值,,,在7月15日至25日期间,emf值为8.00000 。


我使用http://www.dpriver.com/pp/sqlformat.htm - 迈克尔

格式化了您的代码
IF( (SELECT Count(*) 
     FROM   pseb.dbo.meterattributedetails 
     WHERE  meterid = @meterid) = 0 ) 
  SELECT @emf = emf 
  FROM   pseb.dbo.metermaster 
  WHERE  meterid = @meterid 
ELSE IF( (SELECT Count(*) 
     FROM   pseb.dbo.meterattributedetails 
     WHERE  meterid = @meterid 
            AND Dateadd(day, 1, @fromdate) < datetime) > 0 ) 
  SELECT TOP 1 @emf = oldvalue 
  FROM   pseb.dbo.meterattributedetails 
  WHERE  meterid = @meterid 
         AND Dateadd(day, 1, @fromdate) < datetime 
  ORDER  BY datetime 
ELSE 
  SELECT TOP 1 @emf = newvalue 
  FROM   pseb.dbo.meterattributedetails 
  WHERE  meterid = @meterid 
         AND datetime < @fromdate 
  ORDER  BY datetime DESC 

2 个答案:

答案 0 :(得分:7)

@Michael - 感谢http://www.dpriver.com/pp/sqlformat.htm的链接。

试试这个(当然未经测试)

SELECT @emf = CASE 
            WHEN ( (SELECT Count(*) 
                    FROM   pseb.dbo.meterattributedetails 
                    WHERE  meterid = @meterid) = 0 ) THEN 
            (SELECT emf 
             FROM   pseb.dbo.metermaster 
             WHERE  meterid = @meterid) 
            WHEN ( (SELECT Count(*) 
                    FROM   pseb.dbo.meterattributedetails 
                    WHERE  meterid = @meterid 
                           AND Dateadd(day, 1, @fromdate) < datetime) > 0 ) 
          THEN ( 
            SELECT 
            TOP 1 oldvalue 
            FROM 
          pseb.dbo.meterattributedetails 
            WHERE 
          meterid = @meterid 
          AND Dateadd(day, 1, @fromdate) < datetime 
            ORDER 
          BY datetime) 
            ELSE (SELECT TOP 1 newvalue 
                  FROM   pseb.dbo.meterattributedetails 
                  WHERE  meterid = @meterid 
                         AND datetime < @fromdate 
                  ORDER  BY datetime DESC) 
          END 

@Saranya:如果这不起作用,请在http://sqlfiddle.com/上设置测试用例

祝你好运!

答案 1 :(得分:0)

没有尝试语法检查,我认为你正在寻找这样的东西:

select @emf = case
  when not exists ( select 42 from pseb.dbo.meterattributedetails where meterid = @meterid )
    then ( select emf from pseb.dbo.metermaster where meterid = @meterid )
  when exists ( select 42 from pseb.dbo.meterattributedetails where meterid = @meterid and
    Dateadd(day, 1, @fromdate) < datetime )
    then ( select top 1 oldvalue from pseb.dbo.meterattributedetails where meterid = @meterid and
      Dateadd(day, 1, @fromdate) < datetime order by datetime )
  else ( select 1 newvalue from pseb.dbo.meterattributedetails where meterid = @meterid and
    datetime < @fromdate order by datetime desc )
  end

编辑:一般情况下,如果您要获得一个可以检查零的计数,则可以使用性能更佳的EXISTS替换它。