SlNo in Select Query

  • I have the following Table : Emp

    EmpIDDeptEmpName
    1prdnravi
    2prdnrani
    3prdnraja
    4ITpathy
    5ITmahi
    6ITLatha
    7AdminNeela
    8AdminRaju
    9AdminRamu

     

    I want a select query to form the following based on Dept.

    SnoIDDeptEmpName
    11prdnravi
    22prdnrani
    33prdnraja
    14ITpathy
    25ITmahi
    36ITLatha
    17AdminNeela
    28AdminRaju
    39AdminRamu

     

    Thanks,

    R.Prakash


    Kindest Regards,

    R

  • select EmpID - MinEmp + 1 as Sno, EmpID, e.Dept, EmpName

    from Emp e left join (select Dept, min(EmpID) as MinEmp from Emp group by Dept) g

    on e.Dept = g.Dept

  • or, if EmpID not consecutive

    SELECT (SELECT COUNT(*) FROM [Emp] b WHERE b.Dept = a.Dept AND b.EmpID <= a.EmpID)

    ,a.EmpID, a.Dept, a.EmpName

    FROM [Emp] a

    ORDER BY a.Dept ASC, a.EmpID ASC

    Far away is close at hand in the images of elsewhere.
    Anon.

Viewing 3 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic. Login to reply