• sushantkatte (8/2/2015)


    I was not able to understand how the Case statement is working,particularly the THEN part (THEN 1,THEN 2,THEN 2)

    CASE WHEN alert_msg ='Meter' THEN 1

    WHEN alert_msg = 'Tamper' THEN 2

    WHEN alert_msg = 'Reverse' THEN 2

    I am a newbie in SQL but i have basic knowledge and understanding. i was applying for entry level and internship positions where i would get to learn in detail.

    and as far as getting the job goes. i haven't heard back from them yet:-P

    The code actually contained the following CASE statement.

    CASE WHEN alert_msg ='Meter' THEN 1

    WHEN alert_msg = 'Tamper' THEN 2

    WHEN alert_msg = 'Reverse' THEN 3

    END

    CASE is conditional code that operates as a single operand. In this case, it says that if the alert_msg column contains 'Meter', then set the result of the CASE to 1. It does similar with 2 and 3. Since the CASE statement is in the ORDER BY, the resulting 1, 2, or 3 for each row is used to sort the data.

    Good luck on your internship. I guess I don't understand why they would ask such an intermediate question to someone applying for and entry level position.

    Shifting gears, a bit... if you intend to make a career in the world of SQL Server or front-end programming with good knowledge of SQL Server programming, I strongly recommend you buy a copy of the SQL Server Developer Edition (usually < $60USD) for yourself. Think of it as a text-book on steroids. It's the "Enterprise Edition" with special licensing for learners/developers and works on your desktop/laptop so that you don't have to have a Windows Server setup. Also lookup and then download and install "Books Online", which is the "help" system for SQL Server. That information is sometimes easy to find via Google but you may like the way the information is organized in Books Online better. It also contains some tutorial information.

    As a study guide for a beginner, you might try the following link with the understanding that they use ANSI SQL there. SQL Server uses parts of the ANSI Standard but there's a whole lot of extra stuff in SQL Server that work really well.

    http://www.w3schools.com/sql/default.asp

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.

    Change is inevitable... Change for the better is not.


    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)