aspvbnetnerd
Programmer
I have done an application that the users scan the FirstSerialNumber and LastSerialNumber I have to keep track of the LastSerialNumber is comes in order with the FirstSerialNumber that is scanned.
If the LastSerialNumber is 99926 Then the FirstSerialNumber should be 99927.
After the number 99999 it will begin with 00001
This is my SQL Query. And it will return
99767 43 and I want to return 99767 82
My question is this is it possible to use Where clause in Case Statement? Something like this.
If the LastSerialNumber is 99926 Then the FirstSerialNumber should be 99927.
After the number 99999 it will begin with 00001
Code:
[b]AssignmentID BuntchID FirstSerialNumber LastSerialNumber ScanningStationID[/b]
26 1 99768 99926 2
26 2 99927 43 2
26 3 44 [COLOR=red] 82 [/color] 2
This is my SQL Query. And it will return
99767 43 and I want to return 99767 82
Code:
[COLOR=blue]DECLARE[/color] @RetLastSerialNumber [COLOR=blue]AS[/color] [COLOR=blue]INT[/color]
[COLOR=blue]DECLARE[/color] @RetLastSerialNumberTemp [COLOR=blue]AS[/color] [COLOR=blue]INT[/color]
[COLOR=blue]SELECT[/color] @RetLastSerialNumber =
[COLOR=blue]CASE[/color]
[COLOR=blue]WHEN[/color] [COLOR=#FF00FF]MAX[/color](B.LASTSERIALNUMBER) - [COLOR=#FF00FF]MIN[/color](B.LASTSERIALNUMBER) > 5000 [COLOR=blue]THEN[/color] [COLOR=#FF00FF]MIN[/color](B.LASTSERIALNUMBER)
[COLOR=blue]ELSE[/color] [COLOR=#FF00FF]ISNULL[/color]([COLOR=#FF00FF]MAX[/color](B.LASTSERIALNUMBER), 0)
[COLOR=blue]END[/color],
@RetLastSerialNumberTemp =
[COLOR=blue]CASE[/color]
[COLOR=blue]WHEN[/color] [COLOR=#FF00FF]MAX[/color](TB.LASTSERIALNUMBER) - [COLOR=#FF00FF]MIN[/color](TB.LASTSERIALNUMBER) > 5000 [COLOR=blue]THEN[/color] [COLOR=#FF00FF]MIN[/color](TB.LASTSERIALNUMBER)
[COLOR=blue]ELSE[/color] [COLOR=#FF00FF]ISNULL[/color]([COLOR=#FF00FF]MAX[/color](TB.LASTSERIALNUMBER), 0)
[COLOR=blue]END[/color]
[COLOR=blue]FROM[/color] ASSIGNMENT A
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] CARDBOARD CB [COLOR=blue]ON[/color] CB.ASSIGNMENTID = A.ASSIGNMENTID
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] BUNTCH B [COLOR=blue]ON[/color] CB.CARDBOARDID = B.CARDBOARDID AND B.SCANNINGSTATIONID = 2
AND B.CARDBOARDID = ([COLOR=blue]SELECT[/color] [COLOR=#FF00FF]MAX[/color](CCBB.CARDBOARDID)
[COLOR=blue]FROM[/color] ASSIGNMENT AA
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] CARDBOARD CCBB [COLOR=blue]ON[/color] CCBB.ASSIGNMENTID = AA.ASSIGNMENTID
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] BUNTCH BB [COLOR=blue]ON[/color] BB.CARDBOARDID = CCBB.CARDBOARDID AND BB.SCANNINGSTATIONID = 2
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] Temp_Buntch TTBB [COLOR=blue]ON[/color] TTBB.ASSIGNMENTID = AA.ASSIGNMENTID AND TTBB.SCANNINGSTATIONID = 2
[COLOR=blue]WHERE[/color] AA.ASSIGNMENTID = 26)
[COLOR=#FF00FF]LEFT[/color] [COLOR=blue]JOIN[/color] Temp_Buntch TB [COLOR=blue]ON[/color] TB.ASSIGNMENTID = A.ASSIGNMENTID AND TB.SCANNINGSTATIONID = 2
[COLOR=blue]WHERE[/color] A.ASSIGNMENTID = 26
[COLOR=blue]select[/color] @RetLastSerialNumber, @RetLastSerialNumberTemp
My question is this is it possible to use Where clause in Case Statement? Something like this.
Code:
[COLOR=blue]WHEN[/color] [COLOR=#FF00FF]MAX[/color](TB.LASTSERIALNUMBER) - [COLOR=#FF00FF]MIN[/color](TB.LASTSERIALNUMBER) > 5000 [COLOR=blue]THEN[/color] [COLOR=#FF00FF]MAX[/color](TB.LASTSERIALNUMBER) [COLOR=blue]WHERE[/color] TB.LASTSERIALNUMBER < 9000