This article is half-done without your Comment! *** Please share your thoughts via Comment ***
Check the below input data and expected output to skip the zeros before the non-zero number.
I asked this question many times in an interview. Look like very simple, but not a single DB Developer gave me correct answer till date.
Input data:
1 2 3 4 5 6 7 8 9 10 11 |
num ------ 0 0 0 0 1 2 0 3 4 |
Expected Output:
1 2 3 4 5 6 7 |
num ------ 1 2 0 3 4 |
Create a sample table with data:
1 2 3 4 5 6 7 8 9 10 11 |
CREATE TABLE tbl_ids ( num int ) GO INSERT INTO tbl_ids VALUES (0),(0),(0) ,(0),(1),(2) ,(0),(3),(4) GO |
Solution:
1 2 3 4 5 6 |
SELECT num FROM tbl_ids WHERE %%PhysLoc%% >= ( SELECT MIN(%%PhysLoc%%) FROM tbl_ids WHERE num > 0 ) |
Please try the different solution for this puzzle and share it via comment...