在嵌套的sql查询中计算if函数

时间:2012-08-22 02:32:11

标签: asp.net sql sql-server-2008

我在sql server 2005中使用表

进行了以下查询
select t1.id, CONVERT(VARCHAR,t1.dt,103) date_1, CONVERT(VARCHAR,t2.dt,103) date_2, t1.hotel,
    t1.price price_1, t2.price price_2, t2.price - t1.price difference, ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
from test t1
inner join
(
    select *
    from test
) t2
    on t1.hotel = t2.hotel
    and t1.dt < t2.dt and t2.dt=(SELECT MAX(dt) from TEST) and t1.dt=(SELECT MAX(dt-1) from TEST)

我想在此查询中使用count if函数。基于差异列。所以我可以数“增加多少,减少多少,多少相同,多少不可用”

  1. COUNT IF差异&gt; 0 //增加了多少
  2. COUNT IF差异&lt; 0 //减少了多少
  3. COUNT IF差异= 0 //多少相同
  4. COUNT IF差异=“”//多少个不可用 - 差异为空。
  5. DEMO:http://sqlfiddle.com/#!3/b6f37/29

2 个答案:

答案 0 :(得分:1)

此方法使用您的查询,然后只汇总结果:

with t as (
    select t1.id, CONVERT(VARCHAR,t1.dt,103) as date_1,
            CONVERT(VARCHAR,t2.dt,103) as date_2,
           t1.hotel,
           t1.price as price_1, t2.price as price_2,
           t2.price - t1.price as difference,
           ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
    from test t1 join
         test t2
    on t1.hotel = t2.hotel and
       t1.dt < t2.dt and
       t2.dt=(SELECT MAX(dt) from TEST) and
       t1.dt=(SELECT MAX(dt-1) from TEST)
)
select sum(case when diff_percentage > 0.0 then 1 else 0 end) as numIncrease,
       sum(case when diff_percentage < 0.0 then 1 else 0 end) as numDecrease,
       sum(case when diff_percentage = 0.0 then 1 else 0 end) as numSame,
       sum(case when diff_percentage is NULL then 1 else 0 end) as numBlank
from t

我不确定“dt - 1”是什么意思。对于SQL Server中的日期/日期时间值,通常使用“dateadd(day,-1,)”来减去日期。在任何情况下,可能还有其他方法来计算您想要的内容,但这可以回答您的具体问题。

答案 1 :(得分:1)

如果我想要您现有的查询结果,我会将查询重写为:

select t1.id, 
       CONVERT(VARCHAR,t1.dt,103) date_1, 
       CONVERT(VARCHAR,t2.dt,103) date_2, 
       t1.hotel,
       t1.price price_1, 
       t2.price price_2, 
       t2.price - t1.price difference, 
       ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
  from test t1
  join (select max(dt) maxDt from test) d
    on t1.dt = d.maxDt-1
  join test t2
    on t2.hotel = t1.hotel
   and t2.dt = d.maxDt

扩展原始查询以包含缺少行的酒店:

select t1.id, 
       CONVERT(VARCHAR,t1.dt,103) date_1, 
       CONVERT(VARCHAR,t2.dt,103) date_2, 
       h.hotel,
       t1.price price_1, 
       t2.price price_2, 
       t2.price - t1.price difference, 
       ((t2.price - t1.price)/t1.price)*100 as Diff_percentage
  from (select distinct hotel from test) h
  cross join (select max(dt) maxDt from test) d
  left join test t1
    on t1.hotel = h.hotel
   and t1.dt = d.maxDt-1
  left join test t2
    on t2.hotel = h.hotel
   and t2.dt = d.maxDt

使用规范化的HOTEL表(每个酒店1行)来替换SELECT DISTINCT子查询,上述查询效率会更高。

要获得您要求的结果,我会使用:

select count(case when (t2.price-t1.price) < 0 then 1 end) decrease_count,
       count(case when (t2.price-t1.price) > 0 then 1 end) increase_count,
       count(case when (t2.price-t1.price) = 0 then 1 end) same_count,
       count(distinct t1.hotel) - count(case when (t2.price-t1.price) is not null then 1 end) unavailable_count
  from test t1
  left join (select max(dt) maxDt from test) d
    on t1.dt = d.maxDt-1
  left join test t2
    on t2.hotel = t1.hotel
   and t2.dt = d.maxDt

对于规范化的HOTEL表,上述内容将更有效。我将重新构建查询更像前一个:从HOTEL交叉连接到MAX日期查询,然后外部连接到TEST表两次以获取2个日期的数据。然后可以更直接地测量不可用计数,计算差值计算为NULL的行数。

以下是所有查询的SQL Fiddle以及一些扩展测试数据。