值范围中的Vlookup值

时间:2018-07-31 14:49:52

标签: sql domo

我正在尝试在行(表2)的行范围内查找值(表1的rdate),如果可以在表2(具有一组特定的条件)中找到该值,则返回“是” < / p>

表1-按日期销售

rdate           strcompanynodename          Sum of MnyDollar Cost         
    --------------------------------------------------------------
7/15/2017         Chadwick Supply Co.                5100
8/1/2017          Northeastern Milling               5600
6/3/2017          Ford Paper                         25320
12/1/2017         Northeastern Milling               6600
1/11/2010         Chadwick Supply Co.                300
7/4/2017          Ford Paper                         2

表2-受影响的日期

rdate start         rdate end                 strcompanynodename  
    --------------------------------------------------------------
7/1/2017          7/31/2018                   Chadwick Supply Co.   
7/2/2017          8/2/2018                    Northeastern Milling 
2/1/2016          3/31/2016                   Chadwick Supply Co.   
7/3/2017          8/3/2018                    Ford Paper    
3/2/2012         8/2/2013                    Northeastern Milling  

当strcompanynodename与表2的值匹配并且表1的rdate介于表2的rdate开始和rdate结束之间(当公司匹配时),我想按表1的strcompany名称对mnydollar成本进行求和。 / p>

更新为预期结果

strcompanynodename          match? (between dates)                sum of mnydollarcost
--------------------------------------------------------------
Chadwick Supply Co.            yes                             5100
Northeastern Milling           yes                             5600
Ford                           yes                             2
Chadwick Supply Co.            no                              300
Northeastern Milling           no                              6600
Ford                           no                              25320 

2 个答案:

答案 0 :(得分:0)

您可以尝试以下方法:

    SELECT T1.strcompanynodename, T1.Mydollar, t1.rdate, t2.rdatestart, t2.rdateEnd,
case when t1.rdate between t2.rDateStart and t2.rdateEnd then 'Yes' 
Else 'No' end AS Flag
FROM SalesbyDate as T1
LEFT  JOIN ImpactedDates as T2 ON T1.strcompanynodename = T2.strcompanynodename 
AND T1.rDate BETWEEN T2.rDateStart AND T2.rDateEnd
Order by Flag desc

答案 1 :(得分:0)

使用以下查询,您应该能够获得预期的结果

select distinct t1.strcompanynodename , t1.dollar, case when t1.rdate between t2.start and t2.end then "Yes" else "No" end as flag from
table1 as t1 left outer join table2 as t2
on t1.strcompanynodename = t2.strcompanynodename
order by flag

说明: case语句用于创建是或否标志。 此处的关键是使用LEFT OUTER JOIN(在Google上了解有关联接的信息)。为了避免重复记录,我添加了一个独特的表,该表将删除由于表2中的多个条目而导致的重复,因为表1是源

Screenshot