计算和创建上个月的列。循环?

时间:2013-07-02 16:48:53

标签: sql-server loops

我是MS SQL Server的新手(我一直都在使用SAS)。

我有一个程序,用于识别在特定月份未见的儿童 - 下面的标准是粗体。当我运行程序时,这是结果的一个例子:

5月份未见过的儿童:

Person_ID Child_Name  Case_ID   Stage_ID  Unit  Worker_Name
37938354  Chrismary   25367048  28714173  P5G   Edith
37938357  Michael     25367048  28714173  P5G   Edith
29174158  Bryan       22229090  24456882  P5J   Elizabeth
22426348  Juan        24621782  27258653  P5C   Dalbin
37046338  Nathaniel   25154727  28232513  P5A   Vontaisha
38593370  Natalia     25154727  28232513  P5A   Vontaisha
36266293  Luzarely    25020393  27989836  P5C   Dalbin
35745702  Taneesa     24716363  27417204  P5E   Kathleen
28044496  Braulio     24926432  27909182  P5E   Elizabeth

我需要做的是根据我运行程序的月份添加5个额外的列,这将指示过去5个月是否看到孩子的值为“是”,“否”和“ N / A”。 N / A是指孩子未包含在特定月份的数据集中。

我需要的例子:

Person_ID Child_Name  Case_ID   Stage_ID  Unit  Worker_Name  Seen_April Seen_March 
37938354  Chrismary   25367048  28714173  P5G   Edith          YES         YES    
37938357  Michael     25367048  28714173  P5G   Edith          NO          YES
29174158  Bryan       22229090  24456882  P5J   Elizabeth      NO          NO
22426348  Juan        24621782  27258653  P5C   Dalbin         YES         YES
37046338  Nathaniel   25154727  28232513  P5A   Vontaisha      N/A         N/A
38593370  Natalia     25154727  28232513  P5A   Vontaisha      YES         YES
36266293  Luzarely    25020393  27989836  P5C   Dalbin         YES         YES
35745702  Taneesa     24716363  27417204  P5E   Kathleen       YES         NO
28044496  Braulio     24926432  27909182  P5E   Elizabeth      YES         No

...并继续在2月,1月,12月

在SAS中,我可以使用Do循环来解决这个问题,但是不知道使用MS SQL Sever做到这一点。

另外,您会注意到我必须硬代码编码日期Criterion。在SAS中,我可以使用宏变量来选择日期示例:%let BegDate ='2013-05-01'和%Let EndDate ='2013-05-31'。是否有可能在MS SQL服务器中创建宏变量?

以下是该计划:

SELECT 
distinct
a.Person_ID,
a.Child_Name,
b.Case_ID,
b.Stage_ID, 
b.Unit, 
b.Worker_Name
from 
(select
distinct 
Person_ID,
Child_Name
FROM gw_dw.dbo.DimContacts_Child  
where Unit in ('P5C','P4C','P5L','P5D','P5F','P5G','P5E','P5A','P5K','P5J') 
and (**Contact_Date >= '2013-05-01' AND Contact_Date <='2013-05-31**')
group by Person_ID,Child_Name
having sum(case when (Contact_Method='Face To Face') AND
                     (Contact_Result <> 'Attempted') AND
                     (Participant='Yes')
                then 1 else 0 end) = 0 ) as A 
inner join
(Select distinct --Addtional Query Beacuse there multipal units assigned to a child*/
Person_ID,
Case_ID,
Stage_ID, 
Unit, 
Worker_Name
from gw_dw.dbo.DimContacts_Child
where Unit in ('P5C','P4C','P5L','P5D','P5F','P5G','P5E','P5A','P5K','P5J') 
and (**Contact_Date >= '2013-05-01' AND Contact_Date <='2013-05-31'**)
group by Worker_Name,Unit,Person_ID,Case_ID,Stage_ID
having sum(case when (Contact_Method='Face To Face') AND
                     (Contact_Result <> 'Attempted') AND
                     (Participant='Yes')
                then 1 else 0 end) = 0  ) as B 
on  A.Person_ID = B.Person_ID   
order by a.Child_Name;

感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

这可以在SQL中完成。这是一个包含2列的简单示例:

ID DateSeen
1  1/1/13
1  2/1/13
2  3/1/13
3  1/1/13
3  1/1/13
3  4/1/13

如果我理解你的问题,你想要将这个问题折叠为每个ID 1行,并将DateSeen“交叉标记”为12列。试试这个:

Select ID, YEAR(Dateseen) as Seen_Year,
    sum(case when MONTH(Dateseen)=1 then 1 else 0 end) as Seen_Jan,
    sum(case when MONTH(Dateseen)=2 then 1 else 0 end) as Seen_Feb,
    sum(case when MONTH(Dateseen)=3 then 1 else 0 end) as Seen_Mar,
    sum(case when MONTH(Dateseen)=4 then 1 else 0 end) as Seen_Apr
from @tmp
group by id,YEAR(Dateseen)

这会给你(对不起间距):

ID  Seen_Year   Seen_Jan    Seen_Feb    Seen_Mar    Seen_Apr
1   2013    1   1   0   0
2   2013    0   0   1   0
3   2013    2   0   0   1