Get First row value

  • to get the employee data with the number

    try this query

    this query gives employee data with the number

    select MID, Status, Code, row_number() over (partition by code order by MID) as number

    from Emp

    to get the top 1 employees

    select MID, Status, Code

    FROM

    (

    select MID, Status, Code, row_number() over (partition by code order by MID) as number

    from Emp) AS TAB

    where number = 1

  • Gila,

    How many different codes are there?

    Around 2000 codes are there.

    karthik

  • Just i am thinking ,why can't we create the running number based on the code ?

    Running number has to increase when there is no change found on the CODE,if found ,it should start with 1 again like that shall we create ?

    karthik

  • I tried the below code.

    create table #emp

    (

    mid int,

    status char(1),

    code varchar(5)

    )

    insert into #emp

    select 1,'A','1X'

    union all

    select 25,'A','1X'

    union all

    select 37, 'T','1X'

    union all

    select 42,'T','2X'

    union all

    select 58,'A','2X'

    union all

    select 61,'A','3X'

    select * from #emp

    select code,N,count(code)

    from #emp,Tally

    group by code,N

    having N <= count(code)

    I think i am very near to the solution.But who know it ?

    But it is very slow,i don't know why TALLY table also executing slowly.

    Execution Time 32.

    SQL Server cpu time: 3200 ms. SQL Server elapsed time: 3140 ms.

    I remember that Tally table shouldn't take more time.

    Suggestions are welcome !

    karthik

  • i got the below output

    code N Count

    ---- ------- -----------

    1X 1 3

    1X 2 3

    1X 3 3

    2X 1 2

    2X 2 2

    3X 1 1

    karthik

  • Any Comments or suggestions?

    karthik

  • Yes, does my solution (bottom pg 3) work or not?

  • The Tally table cannot be used to solve ranking problems because of the inherent cross-join. You've limited the cross-join, but only with a triangular join... half as bad but still very bad.

    There's no such thing as "Physical Order" in a database. You can have an implied order based on an index, but unless it's a Clustered index, you can end up with the "merry-go-round" order that I spoke of in the Running Total article.

    What is the Clustered index of the table, Karthik? If there isn't one and the MID column can't be used to establish the "natural" order (as you've already stated), this problem cannot be solved. Sorry...

    --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)

  • Jeff,

    Thanks for highlighting some good points.

    Table doesn't have a single clustered index. But it have non clustered index on mid.

    Will it help to resolve this running total issue ?

    Janine,

    Thanks for your reply.

    If you can make the assumption "Smallest MID is always the 'first' row for each code" then this should work:

    I can't assume smallest MID is always the 'first' row for each code.

    karthik

  • Table doesn't have a single clustered index. But it have non clustered index on mid.

    Will it help to resolve this running total issue ?

    Only if the smallest MID was the value you want. Whcih, as you've said, it's not.

    You're probably going to have to create a column to specify which status is first for each code and update it manually.

    Gail Shaw
    Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
    SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

    We walk in the dark places no others will enter
    We stand on the bridge and no one may pass
  • karthikeyan (5/12/2008)


    Jeff,

    Thanks for highlighting some good points.

    Table doesn't have a single clustered index. But it have non clustered index on mid.

    Will it help to resolve this running total issue ?

    Janine,

    Thanks for your reply.

    If you can make the assumption "Smallest MID is always the 'first' row for each code" then this should work:

    I can't assume smallest MID is always the 'first' row for each code.

    Non-clustered indexes don't help with this particular problem. The running update process doesn't ackowledge that ordering during the updates, so you end up with junk results.

    Like Jeff mentioned - it's clustered or no go. (unless you start adding something else to tell which is "first")

    ----------------------------------------------------------------------------------
    Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?

  • So as your people said , table should be created with clustered index.

    But unfortunately table doesn't have clustered index.

    what is the solution to solve this problem ?

    i.e do i have create clustered index ? if yes, won't it affect the other proc's ?

    what can i do further to resolve it ?

    karthik

  • As I said earlier, you have to define what "first" means first... what do you consider to be the correct order of the data? Can't help unless we know that.

    --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)

Viewing 13 posts - 31 through 42 (of 42 total)

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