2

我在 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 Difference>0 //增加了多少
  2. COUNT IF Difference<0 //减少了多少
  3. COUNT IF Difference=0 //有多少相同
  4. COUNT IF Difference="" //有多少不可用 --Difference 为空。

演示:http ://sqlfiddle.com/#!3/b6f37/29

4

2 回答 2

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, )”来减去日期。无论如何,可能有其他方法可以计算您想要的,但这回答了您的具体问题。

于 2012-08-22T02:56:37.190 回答
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,以及一些扩展的测试数据。

于 2012-08-22T04:03:37.083 回答