Sql server 基于日期的case语句查询

Sql server 基于日期的case语句查询,sql-server,Sql Server,我早些时候发布了这个查询。再次发布更多细节,以帮助更好地了解我的问题 原始数据集 Name currency lcfeerate effectivestartdate Institution1 USD 0.0029 7/9/2009 Institution1 CAD 0.0029 7/9/2009 Institution1 USD 0.0034 4/3/2017 Institution2

我早些时候发布了这个查询。再次发布更多细节,以帮助更好地了解我的问题

原始数据集

Name          currency  lcfeerate   effectivestartdate
Institution1    USD      0.0029      7/9/2009
Institution1    CAD      0.0029      7/9/2009
Institution1    USD      0.0034      4/3/2017
Institution2    CAD      0.0029      7/9/2009
Institution2    USD      0.0029      7/9/2009
Institution3    CAD      0.0029      7/9/2009
Institution3    USD      0.0029      7/9/2009
Institution3    USD      0.0034      4/3/2017
Institution3    CAD      0.0034      4/3/2017
我需要运行查询,例如返回每个机构和相应货币对应的一行。i、 e.机构1将有2行,美元和加元各1行。类似地,机构2和3将各有2行。因此,最终结果是一个包含6行的表。 筛选表的规则是,对于每个机构和货币,根据生效的起始日期选择lcfeerate。 当effectivestartdate位于声明的startdate和enddate之间时,将为该effectivestartdate选择feerate。当声明的startdate和enddate之间没有有效的startdate时,它会检查上一个最大有效的startdate。 下面是所需输出的两个示例。 例1

Start date- 1/1/2017        
End date-   3/31/2017       
Name         currency   lcfeerate   effectivestartdate
Institution1    USD      0.0029        7/9/2009
Institution1    CAD      0.0029        7/9/2009
Institution2    CAD      0.0029        7/9/2009
Institution2    USD      0.0029        7/9/2009
Institution3    CAD      0.0029        7/9/2009
Institution3    USD      0.0029        7/9/2009
由于在宣布的开始日期和结束日期之间没有有效的起始日期,它选择下一个可用的有效起始日期2009年7月9日,并为每个机构和货币美元和加元提供与这些日期相对应的lcfeerate

例2

Start date- 4/1/2017        
End date-   5/31/2017       
Name         currency   lcfeerate   effectivestartdate
Institution1    CAD       0.0029      7/9/2009
Institution1    USD       0.0034      4/3/2017
Institution2    CAD       0.0029      7/9/2009
Institution2    USD       0.0029      7/9/2009
Institution3    USD       0.0034      4/3/2017
Institution3    CAD       0.0034      4/3/2017
在这种情况下,由于3号机构2017年4月3日的生效开始日期介于宣布的开始日期和结束日期之间,因此它提供了新的LCFEERATE。对于机构1,美元汇率在声明日期之间具有有效起始日期,因此提供了一行,而其余3行在声明日期之间没有有效起始日期,因此选择先前的有效起始日期来提供汇率

根据effectivestartdate对表进行排序并按名称和货币分组以获得最高值非常容易,但我不确定如何编写此查询

我尝试过的查询如下:

declare @startdate as datetime = '1-Jan-2017';
declare @enddate as datetime = '31-Mar-2017';

select bankname, lcfeerate
        ,case when effectivestartdate between @startdate and @enddate then lcfeerate 
           when effectivestartdate not between @startdate and @enddate
        then (select *
        from (
              select *, row_number()
              over (partition by name, currency order by effectivestartdate desc) as seqnum
                from table1
             ) t1
        where seqnum = 1)end as lcfeerate
     from table1
我得到以下错误:

选择时,只能在选择列表中指定一个表达式 子查询不随EXISTS一起引入


你很接近,但是你的排名错了。第一个排名标准应该是日期是否在日期范围内,第二个是降序的日期

select name, currency, lcfeerate, effectivestartdate
from
(
  select 
    name, currency, lcfeerate, effectivestartdate, 
    row_number() over (partition by name, currency order by
          case when effectivestartdate between @startdate and @enddate the 1 else 2 end,
          effectivestartdate desc) as rn
  from table1
) ranked;

你很接近,但是你的排名错了。第一个排名标准应该是日期是否在日期范围内,第二个是降序的日期

select name, currency, lcfeerate, effectivestartdate
from
(
  select 
    name, currency, lcfeerate, effectivestartdate, 
    row_number() over (partition by name, currency order by
          case when effectivestartdate between @startdate and @enddate the 1 else 2 end,
          effectivestartdate desc) as rn
  from table1
) ranked;
我希望这对你有帮助


希望对您有所帮助。

请输入格式代码。很难阅读。问题是这
然后(选择*
)。你只能返回一个值,因为它将进入一列。也许你需要将零件移动到“外部应用”并将其选择为多列?请格式化代码。这很难阅读。问题是这
然后(选择*
。您只能返回一个值,因为它将进入一列。您可能需要将零件移动到“外部应用”并将其选择到多个列中?