Home

Need help with a potential loop (or how would you do this)?

%3CLINGO-SUB%20id%3D%22lingo-sub-995223%22%20slang%3D%22en-US%22%3ENeed%20help%20with%20a%20potential%20loop%20(or%20how%20would%20you%20do%20this)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-995223%22%20slang%3D%22en-US%22%3E%3CP%3EHi%20all%2C%3C%2FP%3E%3CP%3EI'm%20pretty%20sure%20this%20can%20be%20done%20with%20multiple%20LEAD()%2FCASE%20statements%20but%20I'm%20trying%20to%20find%20a%20way%20to%20almost%20automate%20this%20for%20simplicity%20sake%20and%20to%20help%20me%20learn%20something%20new.%3C%2FP%3E%3CP%3EAttached%20is%20some%20example%20data%20that%20is%20in%20table%20%23Test.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F155854i3C90DDCDC9EC6DB7%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_0.png%22%20title%3D%22clipboard_image_0.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EOn%20the%20left%20I%20have%20Keys%20which%20are%20individual%20users.%20I%20think%20what%20I%20need%20to%20do%20is%20a%20loop%20for%20each%20unique%20Key%3B%2093246499%2C%2093246616%2C%2093246697%20and%2093246831.%3C%2FP%3E%3CP%3EEach%20has%20a%20transcode%20associated%20with%20them%3A%3C%2FP%3E%3CP%3ECode%2036%20%3D%20Charge%3C%2FP%3E%3CP%3ECode18%20%3D%20Paid%3C%2FP%3E%3CP%3ECode%2015%20%26amp%3B%2031%20%3D%20Waive%3C%2FP%3E%3CP%3EWhat%20I%20am%20wanting%20is%20for%20each%20instance%20of%20the%2036%20Code%20(Charge)%20to%20look%20at%20the%20next%20row%20(I'm%20currently%20using%20lead()%20for%20the%20Date%20Paid)%20and%20determine%20if%20the%20transcode%20is%20different.%20Here%20are%20the%20different%20scenarios%3A%3C%2FP%3E%3CP%3EIn%20all%20of%20the%20examples%20below%20this%20is%20by%20Key%20if%20the%20Key%20is%20%26lt%3B%26gt%3B%20Row%201%20Key%20then%20it%20stops%20at%20that%20step.%3C%2FP%3E%3CP%3ERow%201%20Code%2036%20Row%202%20Code%2036%20%3D%20Stop%20and%20count%20as%20UnPaid%20(this%20is%20already%20setup)%3C%2FP%3E%3CP%3ERow%201%20Code%2036%20Row%202%20Code%2015%2C%2031%20%3D%20Set%20Waive%20Amount%20to%20the%20Late%20Amount%20(this%20is%20already%20setup).%3C%2FP%3E%3CP%3ERow%201%20Code%2036%20Row%202%20Code%2018%2C%2019%20%3D%20Continue%20searching%20next%20row%20until%20reaching%20Code%2015%2C%2031%20or%2036%20and%20SUM%20the%20Late%20columns.%20IF%20we%20reach%20code%2015%20or%2031%20then%20set%20Waive%20amount%20to%20the%20Late%20amount.%3C%2FP%3E%3CP%3EOnce%20these%20are%20done%20I%20plan%20on%20doing%20SELECT%20*%20FROM%20%23TEST%20WHERE%20Trans_Code%20%3D%2036%20to%20get%20the%20following%20output...%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F155853iFF490FF9F11A9631%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_1.png%22%20title%3D%22clipboard_image_1.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EI'm%20working%20on%20finding%20an%20instance%20where%20the%20Trans_Code%20%3D%2018%2F19%20for%20ROWS%202-3%2B%20but%20again%20it%20would%20be%20the%20same%20as%20above%20but%20the%20PAID%20amount%20would%20instead%20be%20a%20SUM%20of%20all%20those%20Rows.%20Will%20have%20an%20example%20shortly...%3C%2FP%3E%3CP%3EHere's%20a%20better%20example%20of%20what%20I'm%20looking%20for.%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20400px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F155852i1AE446F87C2CA24C%2Fimage-size%2Fmedium%3Fv%3D1.0%26amp%3Bpx%3D400%22%20alt%3D%22clipboard_image_2.png%22%20title%3D%22clipboard_image_2.png%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3CP%3EEnd%20Result%20should%20be%20(note%20I'm%20unsure%20of%20what%20to%20do%20with%20the%20Date%20yet%20so%20you%20can%20ignore%20the%20format%20of%20that)%3A%3C%2FP%3E%3CP%3E%3CSPAN%20class%3D%22lia-inline-image-display-wrapper%20lia-image-align-inline%22%20style%3D%22width%3A%20765px%3B%22%3E%3CIMG%20src%3D%22https%3A%2F%2Fgxcuf89792.i.lithium.com%2Ft5%2Fimage%2Fserverpage%2Fimage-id%2F155855i5B9D03D5521BB86C%2Fimage-size%2Flarge%3Fv%3D1.0%26amp%3Bpx%3D999%22%20alt%3D%22Capture4.PNG%22%20title%3D%22Capture4.PNG%22%20%2F%3E%3C%2FSPAN%3E%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-995330%22%20slang%3D%22en-US%22%3ERe%3A%20Need%20help%20with%20a%20potential%20loop%20(or%20how%20would%20you%20do%20this)%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-995330%22%20slang%3D%22en-US%22%3E%3CP%3EHere's%20something%20I%20came%20up%20with%20so%20far...%20but%20I%20still%20think%20there%20should%20be%20a%20more%20simple%20and%20effective%20means%20of%20doing%20this%20without%20the%20bulk%20of%20code.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-markup%22%3E%3CCODE%3ESELECT%20*%0A%20%20%20%2CCASE%20WHEN%20LEAD(%5BKey%5D%2C%201)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%3D%20%5BKey%5D%20THEN%20%0A%20%20%20%20%20%20CASE%20WHEN%20LEAD(TRANS_CODE%2C%201)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20IN%20('18'%2C%20'19')%20THEN%20%5BAmount%20Paid%5D%20%2B%20LEAD(Charge%2C%201)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%2B%20ISNULL(%0A%20%20%20%20%20CASE%20WHEN%20LEAD(%5BKey%5D%2C%202)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%3D%20%5BKey%5D%20THEN%0A%20%20%20%20%20%20%20CASE%20WHEN%20LEAD(TRANS_CODE%2C%202)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20IN%20('18'%2C%20'19')%20THEN%20LEAD(Charge%2C%202)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%2B%20ISNULL(%0A%20%20%20%20%20%20%20%20%20CASE%20WHEN%20LEAD(%5BKey%5D%2C%203)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%3D%20%5BKey%5D%20THEN%0A%20%20%20%20%20%20%20%20CASE%20WHEN%20LEAD(TRANS_CODE%2C%203)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20IN%20('18'%2C%20'19')%20THEN%20LEAD(Charge%2C%203)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%2B%20ISNULL(%0A%20%20%20%20%20%20%20%20%20%20CASE%20WHEN%20LEAD(%5BKey%5D%2C%204)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20%3D%20%5BKey%5D%20THEN%0A%20%20%20%20%20%20%20%20%20%20%20%20CASE%20WHEN%20LEAD(TRANS_CODE%2C%204)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%20IN%20('18'%2C%20'19')%20THEN%20LEAD(Charge%2C%204)%20OVER(PARTITION%20BY%20%5BKEY%5D%20ORDER%20BY%20%5BDate%5D)%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20ELSE%200%0A%20%20%20%20%20%20%20%20%20%20%20%20%20END%0A%20%20%20%20%20%20%20%20%20%20%20END%2C%200)%0A%20%20%20%20%20%20%20%20%20%20ELSE%200%0A%20%20%20%20%20%20%20%20%20END%0A%20%20%20%20%20%20%20%20%20%20END%2C%200)%0A%20%20%20%20%20%20%20%20%20ELSE%200%0A%20%20%20%20%20%20%20%20END%0A%20%20%20%20%20%20END%2C%200)%0A%20%20%20%20%20ELSE%200%20END%0A%20%20%20%20%20%20ELSE%200%0A%20%20END%0A%20%20FROM%20%23Test%0A%20WHERE%20%5BKey%5D%20%3D%20'93246916'%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Highlighted
KruegerXIII
New Contributor

Hi all,

I'm pretty sure this can be done with multiple LEAD()/CASE statements but I'm trying to find a way to almost automate this for simplicity sake and to help me learn something new.

Attached is some example data that is in table #Test.

clipboard_image_0.png

On the left I have Keys which are individual users. I think what I need to do is a loop for each unique Key; 93246499, 93246616, 93246697 and 93246831.

Each has a transcode associated with them:

Code 36 = Charge

Code18 = Paid

Code 15 & 31 = Waive

What I am wanting is for each instance of the 36 Code (Charge) to look at the next row (I'm currently using lead() for the Date Paid) and determine if the transcode is different. Here are the different scenarios:

In all of the examples below this is by Key if the Key is <> Row 1 Key then it stops at that step.

Row 1 Code 36 Row 2 Code 36 = Stop and count as UnPaid (this is already setup)

Row 1 Code 36 Row 2 Code 15, 31 = Set Waive Amount to the Late Amount (this is already setup).

Row 1 Code 36 Row 2 Code 18, 19 = Continue searching next row until reaching Code 15, 31 or 36 and SUM the Late columns. IF we reach code 15 or 31 then set Waive amount to the Late amount.

Once these are done I plan on doing SELECT * FROM #TEST WHERE Trans_Code = 36 to get the following output...

clipboard_image_1.png

 

I'm working on finding an instance where the Trans_Code = 18/19 for ROWS 2-3+ but again it would be the same as above but the PAID amount would instead be a SUM of all those Rows. Will have an example shortly...

Here's a better example of what I'm looking for.

clipboard_image_2.png

End Result should be (note I'm unsure of what to do with the Date yet so you can ignore the format of that):

Capture4.PNG

1 Reply

Here's something I came up with so far... but I still think there should be a more simple and effective means of doing this without the bulk of code.

 

 

SELECT *
	  ,CASE WHEN LEAD([Key], 1) OVER(PARTITION BY [KEY] ORDER BY [Date]) = [Key] THEN 
		    CASE WHEN LEAD(TRANS_CODE, 1) OVER(PARTITION BY [KEY] ORDER BY [Date]) IN ('18', '19') THEN [Amount Paid] + LEAD(Charge, 1) OVER(PARTITION BY [KEY] ORDER BY [Date]) + ISNULL(
				 CASE WHEN LEAD([Key], 2) OVER(PARTITION BY [KEY] ORDER BY [Date]) = [Key] THEN
					  CASE WHEN LEAD(TRANS_CODE, 2) OVER(PARTITION BY [KEY] ORDER BY [Date]) IN ('18', '19') THEN LEAD(Charge, 2) OVER(PARTITION BY [KEY] ORDER BY [Date]) + ISNULL(
						   CASE WHEN LEAD([Key], 3) OVER(PARTITION BY [KEY] ORDER BY [Date]) = [Key] THEN
								CASE WHEN LEAD(TRANS_CODE, 3) OVER(PARTITION BY [KEY] ORDER BY [Date]) IN ('18', '19') THEN LEAD(Charge, 3) OVER(PARTITION BY [KEY] ORDER BY [Date]) + ISNULL(
									 CASE WHEN LEAD([Key], 4) OVER(PARTITION BY [KEY] ORDER BY [Date]) = [Key] THEN
										  CASE WHEN LEAD(TRANS_CODE, 4) OVER(PARTITION BY [KEY] ORDER BY [Date]) IN ('18', '19') THEN LEAD(Charge, 4) OVER(PARTITION BY [KEY] ORDER BY [Date])
											   ELSE 0
										   END
									  END, 0)
									 ELSE 0
								 END
						    END, 0)
						   ELSE 0
					   END
				  END, 0)
				 ELSE 0 END
		    ELSE 0
		END
  FROM #Test
 WHERE [Key] = '93246916'

 

Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
35 Replies
Extentions Synchronization
ChirmyRam in Discussions on
3 Replies
flashing a white screen while open new tab
Deleted in Discussions on
14 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies