Now I need to add a col0 to the data table by using generic script that to using server variables which should contain unique value for each value in col1. for example in first and second rows col1 value is 'A' so for these two rows col0 value should be same let take 1, for third row col1 value is different so for this col0 value shoulbe different Final table result should be Please help me on this.
(comments are locked)
|
|
As an aside, here is something interesting: Why isn't the id for both A rows the same? NEWID is being evaluated for each row of the outer query? The query plan shows the output list from the subquery only contains col1. The compute scalar should not be the final step. I think it is strange but this works: Yes because you haven't explicitly specified the NEWID() as being 'in' the aggregation, the optimizer thinks it's fine (and probably cheaper) to calculate it outside of the sub query as there are (potentially) fewer invocations - by adding the explicit MAX() or by forcing the materialization (temp table etc) you ensure the behaviour is as you expect. Good spot - it could catch many people out that one!
Aug 20 '12 at 08:47 AM
Kev Riley ♦♦
Thank you @kev Riley and @scot Hauder.
Aug 21 '12 at 03:56 PM
satya
(comments are locked)
|
|
I guess using DENSE_RANK() function is one way of doing it. For e.g. which gives the desired output. Yes, but we should not use rank functions. By using server varibales we need to generate the unique key. Its an interview question.
Aug 18 '12 at 04:55 AM
satya
(comments are locked)
|
|
If by server variables you mean some value that is specific to that server,e.g. server name then you can use Thank you @kev Riley, Is there any server variable which holds the previous result or previous output?
Aug 19 '12 at 06:18 AM
satya
Previous result/output of what?
Aug 19 '12 at 08:19 PM
Kev Riley ♦♦
Previous query/statement result.
Aug 21 '12 at 03:54 PM
satya
(comments are locked)
|


What do you mean by 'server variables'? Or is that part of the interview question?