Quantcast
Channel: ฟอรัม Getting started with SQL Server
Viewing all articles
Browse latest Browse all 8428

Calculating and Creating Columns for Previous Months. Do Loops?

$
0
0

I am very new to MS SQL Server (I have always been using SAS to pull).

I have program which identifies children which where not seen in a particular month - the criterion is in bold below. When I run the program this is a example of the result:

Children which were not seen in the month of May:

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

What I need to do is add 5 additional month columns based on the month which I run the program for which will indicate if the child was not seen the last 5 month with the values 'YES', 'NO', and 'N/A'. N/A is when the child was not included in the dataset for the particular month.

Example of what I need:

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

...and continues for Feb, Jan, Dec.

In SAS I could figure this out using a Do Loop but don't know hoe to do that using MS SQL Sever.

Also you'll notice I've had to hard code code the date Criterion. In SAS I can use a Macro variable to select the dates Example: %let BegDate= '2013-05-01' and %Let EndDate='2013-05-31'. Is there a may to make macro variables in MS SQL server?

Here is the program:

SELECTdistinct
a.Person_ID,
a.Child_Name,
b.Case_ID,
b.Stage_ID,
b.Unit,
b.Worker_Namefrom(selectdistinct
Person_ID,
Child_NameFROM 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**')groupby Person_ID,Child_Namehaving sum(casewhen(Contact_Method='Face To Face')AND(Contact_Result <>'Attempted')AND(Participant='Yes')then1else0end)=0)as A innerjoin(Selectdistinct--Addtional Query Beacuse there multipal units assigned to a child*/
Person_ID,
Case_ID,
Stage_ID,
Unit,
Worker_Namefrom gw_dw.dbo.DimContacts_Childwhere Unit in('P5C','P4C','P5L','P5D','P5F','P5G','P5E','P5A','P5K','P5J')and(**Contact_Date >='2013-05-01'AND Contact_Date <='2013-05-31'**)groupby Worker_Name,Unit,Person_ID,Case_ID,Stage_IDhaving sum(casewhen(Contact_Method='Face To Face')AND(Contact_Result <>'Attempted')AND(Participant='Yes')then1else0end)=0)as B on  A.Person_ID = B.Person_ID   orderby a.Child_Name;

Viewing all articles
Browse latest Browse all 8428

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>