聚合可能不会出现在WHERE子句中

时间:2015-04-20 19:24:38

标签: sql sql-server-2008

 Select cusip
 from mhfspric
 where 
 PriceDate = case (Month(max(PriceDate)))
        when 1 
        Then '12-31-2014'
        when 2
        Then '12-31-2014'
        when 3
        then '12-31-2014'
        ELSE Null
    END

 group by cusip

我需要一个案例陈述来获取不同月份pricedate的季度结束日期。


select distinct c1 as Cusip,
       c2 as PriceDate,
       c3 as PriceToDate,
       c4 as PriceSource
from 
(select c8 as c1,
       c11 as c2,
       c10 as c3,
       c9 as c4,
       c7 as c5
from 
(select T1."Cusip" as c6,
       MAX(T1."PriceSource") as c7

      from "mhfspric" T1
where T1.PriceDate = (select case (Month(max(T2.PriceDate)))
        when 1 
        Then '12-31-2015'
        when 2
        Then '12-31-2015'
        when 3
        then '12-31-2015'
        when 4
        then '03-31-2015'
        when 5
        then '03-31-2015'
        when 6
        then '03-31-2015'
        when 7
        then '06-30-2015'
        when 8
        then '06-30-2015'
        when 9
        then '06-30-2015'
        when 10
        then '09-30-2015'
        when 11
        then '09-30-2015'
        when 12
        then '09-30-2015'
        ELSE Null
    END
    from mhfspric T2)and T1."PriceSource" <> 91
group by T1."Cusip"

) D3,
(select T1."Cusip" as c8,
       T1."PriceSource" as c9,
       T1."PriceToDate" as c10,
       T1."PriceDate" as c11
from "mhfspric" T1
where T1.PriceDate = (select case (Month(max(T2.PriceDate)))
        when 1 
        Then '12-31-2014'
        when 2
        Then '12-31-2014'
        when 3
        then '12-31-2014'
        when 4
        then '03-31-2015'
        when 5
        then '03-31-2015'
        when 6
        then '03-31-2015'
        when 7
        then '06-30-2015'
        when 8
        then '06-30-2015'
        when 9
        then '06-30-2015'
        when 10
        then '09-30-2015'
        when 11
        then '09-30-2015'
        when 12
        then '09-30-2015'
        ELSE Null
    END
    from mhfspric T2)and T1."PriceSource" <> 91
) D2
where ((c8 = c6) or ((c8 IS NULL) and (c6 IS NULL)))
) D1
where (c4 = c5)
)

这是整个sql。我需要获得最高价格日期,如果它在一年的前3个月内下降,那么是否已经到了上一季度结束。例如,如果最大定价是04-13-2015,那么定价应该是03-31-2015。如何使这个代码通用而不是每年的硬编码。

请帮忙。谢谢

1 个答案:

答案 0 :(得分:0)

这是一个SQLFiddle:http://sqlfiddle.com/#!6/57f6f/33

设置数据:

create table mhfspric (
  cusip numeric,
  PriceDate date,
  PriceSource numeric
  );

/* has max value in first quarter */
insert into mhfspric values (1,'2015-01-10',90);
insert into mhfspric values (1,'2015-01-12',90);

/* has max value in second quarter */
insert into mhfspric values (2,'2015-02-13',90);
insert into mhfspric values (2,'2015-04-02',90);

/* has max value in third quarter */
insert into mhfspric values (3,'2015-08-12',90);
insert into mhfspric values (3,'2015-09-05',90);

/* has max value in fourth quarter */
insert into mhfspric values (4,'2015-11-12',90);
insert into mhfspric values (4,'2015-12-23',90);

查询:

select cusip
, max(PriceDate) as maxPriceDate
, convert(date, 
    DATEADD(s,-1,
      DATEADD(mm, 3*(month(max(PriceDate)) / 4), 
        DATEADD(yy, DATEDIFF(yy,0,max(PriceDate)), 0)
      )
    )
  ) as quarterEndDate
from mhfspric
group by cusip;

以下是查询功能的细分。

DATEADD(yy, DATEDIFF(yy,0,max(PriceDate)), 0)

查询的上述部分是获取给定max(PriceDate)的一年中的第一天。例如,它将返回2015-01-01。

DATEADD(mm, 3*(month(max(PriceDate)) / 4),...

如果我们取最大值(PriceDate)并除以4,我们将得到0到3之间的数字。然后我们将这个月数增加3倍到2015-01-01,所以我们最终得到了2015- 01-01(已添加0个月),2015-04-01(已添加3个月),2015-07-01(已添加6个月),2015-10-01(已添加9个月)。

DATEADD(s,-1,...

现在我们取上述结果并减去1秒,将每个值更改为上个月(2015-01-01更改为2014年12月,2015-04-01更改为2015年3月等)。

最后我们在上面运行convert(date,...)来删除时间部分。

结果如下:

cusip   maxPriceDate    quarterEndDate
1       2015-01-12      2014-12-31
2       2015-04-02      2015-03-31
3       2015-09-05      2015-06-30
4       2015-12-23      2015-09-30