Monday, February 16, 2015

How to find the Second value (could be largest or lowest) from a table.?


I have One table with StatusIDHeld in "UserStatusHistory"Table with this column userid,StatusIDHeld and statusChangedOn. One customer could be multiple StatusIDHeld . I am finding out just previous one StatusIDHeld that mean the second StatusIDHeld which just before he had hold.

To be able to get 1., 2. or n-th record depending on sort order, use ROW_NUMBER() function.
                        SELECT StatusIDHeld 
   FROM
   (
    SELECT StatusIDHeld, ROW_NUMBER () OVER(order by statusChangedOn DESC) as RowNo
    FROM UserStatusHistory where userid= 1037146 
   ) AS t
   where t.RowNo = 2


Happy Query .....

SQL SERVER – Simple Example of Cursor with update table

A cursor is a set of rows together with a pointer that identifies a current row.

DECLARE cursor_activatedOnEmpty CURSOR FOR SELECT userid, statusid,RegisteredOn, activatedOn, RealActivationDate FROM UserProfile where statusID=1330 and  activatedOn is null; 
DECLARE @userid INT;
DECLARE @statusid INT;
DECLARE @RegisteredOn Date;
DECLARE @activatedOn Date;
DECLARE @RealActivationDate Date;
DECLARE @PreviousStatusID INT;
OPEN cursor_activatedOnEmpty;
FETCH NEXT FROM cursor_activatedOnEmpty INTO @userid, @statusid,@RegisteredOn, @activatedOn,@RealActivationDate;
WHILE @@FETCH_STATUS = 0  
BEGIN    
 
 SET @PreviousStatusID = (select top 2 StatusIDHeld from dbo.UserStatusHistory where userid=@userid order by statusChangedOn DESC)  
       if (@PreviousStatusID=1325)
       update UserProfile set activatedOn=@RegisteredOn where userid=@userid

       FETCH NEXT FROM cursor_activatedOnEmpty INTO @userid, @statusid,@RegisteredOn, @activatedOn,@RealActivationDate;
END;
CLOSE cursor_activatedOnEmpty;
DEALLOCATE cursor_activatedOnEmpty;

Happy Cursor!!

Sunday, February 8, 2015

Complex Json deserialization C# (class is not supported for deserialization of an array)


I am trying to deserialization array bellow this way.
This is my json file.
[
   [
      {
         "name":"Riaz Kabir",
         "url":"https://recruit.theladders.com/resumeviewer?jobseekerId=01-sid-BDLBKUPMIL6HGZ22MOAJJIWYGE",
         "summary":"ASP.NET MVC Developer Hewlett-Packard (HP) (1/2013-Present) Location: Schenectady, NY Compensation: $50k+ Previous Titles/Companies: 2016 ►",
         "role":"ASP.NET MVC Developer at Hewlett-Packard (HP)",
         "compensation":"$50k+",
         "education":"BS, Computer Science and Engineering, Asian University of Bangladesh",
         "expertise":"Databases , IT Consulting , Software Development , Front End Development",
         "years":"Less than 5",
         "relocation":"Schenectady, NY Within 1000 miles of 12305 Would need to relocate here",
         "resume":"0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAP",
         "resumeExtension":"doc",
         "resumeMimeType":"application/msword"
      }
   ]
]

Bellow my candidate Resume class
public class CandidateResume
        {

            public string name { get; set; }
            public string url { get; set; }
            public string summary { get; set; }
            public string role { get; set; }
            public string compensation { get; set; }
            public string education { get; set; }
            public string expertise { get; set; }
            public string years { get; set; }
            public string relocation { get; set; }
            public string resume { get; set; }
            public string resumeExtension { get; set; }
            public string resumeMimeType { get; set; }

        }

I have bind this "WebScrappingCandidate" class this bellow way.
                JavaScriptSerializer jsSerializer = new JavaScriptSerializer() { MaxJsonLength = 86753090 };
                List> myobj = jsSerializer.Deserialize>>(description);

                foreach (List listCandidateResume in myobj)
                {
                    foreach (CandidateResume candidateResume in listCandidateResume)
                    {
                        WebScrappingCandidate webScrappingCandidate = new WebScrappingCandidate();
                        webScrappingCandidate.FullName = candidateResume.name;
                        webScrappingCandidate.ResumeUrl = candidateResume.url;
                        webScrappingCandidate.Summary = candidateResume.summary;
                        webScrappingCandidate.PositionName = candidateResume.role;
                        webScrappingCandidate.Compensation = candidateResume.compensation;
                        webScrappingCandidate.Education = candidateResume.education;
                        webScrappingCandidate.Skills = candidateResume.expertise;
                        webScrappingCandidate.YearsOfExperience = candidateResume.years;
                        webScrappingCandidate.Relocation = candidateResume.relocation;                        
                        webScrappingCandidate.ResumeText = candidateResume.resume;                                                                  
                    }                                       
                }   

Happy Programming :)