分区/结束

时间:2012-12-15 04:13:09

标签: tsql

需要识别每个成员的团队字段中的'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

计算字段 - 每个成员发生'a'和&日期分组是来自数据仓库查询的计算字段,通过case语句/分区by / over。是否可以为所需的语法提供一些帮助?

谢谢,

1 个答案:

答案 0 :(得分: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