2012-12-15 23 views
0

确定每个成员的团队字段中'a'的发生次数和日期发生次数(作为一个组)是必需的。的数据样本是如下:分区号

**member & date**|     **Team** 
a010112|         a 
a010112|         b 
a010112|         c 
a010112|         d 
b010112|         b 
b010112|         b 
c010112|         a 
c010112|         b 
c010112|         c 

期望的结果如下:

**member & date|     **team**|  **Occurrence of 'a' per member & date** 
a010112|         a|    yes 
a010112|         b|    yes 
a010112|         c|    yes 
a010112|         d|    yes 
b010112|         b|    no 
b010112|         b|    no 
c010112|         a|    yes 
c010112|         b|    yes 
c010112|         c|    yes 

所计算的场 - 的“一个”每构件&日期分组的出现是要计算的字段从数据库查询通过case语句/分区/ over。能否为所需的语法提供一些帮助?

谢谢你,

+2

请发表您的尝试。 –

+0

团队='a'的情况下,然后'是'结束(按会员和日期划分)否则结束。 – mlee

回答

1

试试这个

Declare @t Table ([Member and Date] Varchar(50), Team Varchar(10)) 
Insert Into @t Values 
    ('a010112','a'),('a010112','b'),('a010112','c'),('a010112','d'), 
    ('b010112','b'),('b010112','b'), 
    ('c010112','a'),('c010112','b'),('c010112','c') 

SELECT 
    x.[Member and Date] 
    ,x.[Team] 
    ,[Occurrence of 'a' per member & date] = CASE WHEN y.[Member and Date] IS NULL THEN 'No' ELSE 'Yes' END 

From @t x 
Left Join(Select Distinct [Member and Date] 
    From @t Where Team = 'a')y 
On x.[Member and Date] = y.[Member and Date] 

enter image description here