T-SQL how to set last row as first

Hi,
I have the following query:
SELECT leveltmp, 'Level ' + CAST( leveltmp as varchar(5)) + ' (' +
STUFF((SELECT top(3) ', '+ BES.Name
FROM BESTB BES
WHERE BES.leveltmp = BE.leveltmp
AND BESId=2
FOR XML PATH('')), 1, 1, '') + ',...)' as 'level'
FROM BESTB as BE
WHERE BEId = 2
GROUP BY leveltmp
ORDER BY 1
and I get the following results:
1    Level 1 ( TestFirstLvl1, TestFirstLvl2, TestFirstLvl3,...)
2    Level 2 ( TestSecondLvl1, TestSecondLvl2, TestSecondLvl3,...)
3    Level 3 ( TestThirdLvl1, TestThirdLvl2, TestThirdLvl3,...)
4    Level 4 ( TestFourthLvl1, TestFourthLvl2, TestFourthLvl3,...)
5    Level 5 ( TestFifthLvl1,...)
However, I need the last one always to be the first one, but the others to remain as they are. I need my order to be 5,1,2,3,4 instead of 1,2,3,4,5.
So basically I need to get the following results:
1    Level 1 ( TestFifthLvl1,...)
2    Level 2 ( TestFirstLvl1, TestFirstLvl2, TestFirstLvl3,...)
3    Level 3 ( TestSecondLvl1, TestSecondLvl2, TestSecondLvl3,...)
4    Level 4 ( TestThirdLvl1, TestThirdLvl2, TestThirdLvl3,...)
5    Level 5 ( TestFourthLvl1, TestFourthLvl2, TestFourthLvl3,...)
Any idea on how to achieve this?
Thanks

create table #t (c char(1))
insert into #t values ('a'),('b'),('c'),('d')
select * from #t order by c ---now we need d to be first charachter 
declare @c char(1)=(select top 1 c from #t order by c desc)
select * from #t order by case when c =@c then '1' else c end
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence

Similar Messages

  • How to set special rows in jtable not selectable

    Hello programmers,
    anybody knows how to set special rows(p.E. row 0) in jtable not selectable.
    in advance thanks for your answers

    table = new JTable(...)
         public void changeSelection(int row, int column, boolean toggle, boolean extend)
              if (row == 0)
                   return;
              else
                   super.changeSelection(row, column, toggle, extend);
    };

  • How to set max row display rows in PO's PLD

    Hi,
    How to set max row display rows in PO's PLD
    Thanks and Best Regards
    Bruce

    Hi
    Max. Rows per Page is depend on PLD Settings and Depend on Repetitive Area Fieds Height.
    Try this,
    1st Case:
    ->> Administration Module.
    ->> System Initialization.
    ->> Open the Print Perferences Window.
    ->> Click General Tab on Print Perferences Window.
    ->> Assign the Max. Rows per Page - 99 (or) your wishes.
    ->> Update the Print Perferences.
    2nd Case:
    ->> Open your PLD.
    ->> Choose Repetitive Area on Field Index Window.
    ->> Click Format Tab on Properties Area Window.
    ->> Assign the Lines in Repetitive Area - Your Wishes .
    ->> Save the PLD.
    Regards,
    Madhan.

  • How to get fourthly row (row4) first column value (col1) in matrix

    Hi to all,
    In FMS, how to get fourthly row (row4) first column value (col1) in matrix in document.
    select $[$38.1.4]
    But it display the first row
    Please give me hint.
    Thank you

    Hi Eric,
    FMS may only apply to current row.  There is no way to get any other fixed row.
    Thanks,
    Gordon

  • How to set current row in calendar

    Hi All,
    I am implementing ADF calendar using ADF faces. When i click on any activity the current row selected is not being displayed. Instead it always points to the last row of the VO.
    How to i explicitly set the current row to the selected row.(this can be declaratively achieved in tables selectedRow property)
    Thanks
    Sandeep.G

    Use the following code to default the calender date to today's.
      public void initializeSearchFormData()
        String methodName = "initializeSearchFormData()";
        _logger.entering(CLAZZ_NAME, methodName);
        MapDataSearchFormVOImpl searchDataVO = getMapDataSearchForm();
        // only create if we don't have any rows yet
        if (searchDataVO.getEstimatedRowCount() == 0)
          _logger.logp(Level.FINEST, CLAZZ_NAME, methodName,
                       "Creating new row for MapDataSearchFormVOImpl.");
          MapDataSearchFormVORowImpl newRow =
            (MapDataSearchFormVORowImpl) searchDataVO.createRow();
          newRow.setSELECT_DATE(DateTimeUtil.singleton().getCurrentJboDate());
          searchDataVO.clearCache();
          searchDataVO.insertRow(newRow);
          searchDataVO.setCurrentRow(newRow);
       * Container's getter for MapDataSearchForm.
       * @return MapDataSearchForm
      public MapDataSearchFormVOImpl getMapDataSearchForm()
        return (MapDataSearchFormVOImpl) findViewObject("YOUR VO NAME");
        _logger.exiting(CLAZZ_NAME, methodName);
      }Regards,
    Kiran Konjeti

  • How to find last row in itab?

    hi experts,
                  How to find the last row or item in an itab.I am using if statement for which i have to check the last row and if it is the last row some set of statements should trigger.
    thanks
    mani

    Just, find the last record :
    * Standard table
      DESCRIBE TABLE itab LINES n.
      READ TABLE itab INDEX n.
    Check on the table with special work at last record :
    * Loop
      LOOP AT itab.
        MOVE itab TO wa_itab.
        AT LAST.
    * here wa_itab is the last record (itab is *****)
        ENDAT.
      ENDLOOP.
    Regards

  • How to set max rows in flowed textfield?

    Is there a way to limit the rows allowed to input on a flowed textarea? I now we can limit the length of max chars, but that would not help me.

    Hi Rishit,
    Thanks for your answer!
    Had a long holiday, so didn't give a feedback in time. Sorry for that!
    There is a tab 'Controls' in Universe Parameter, where there is a setting used to limit the size of result set.
    Then what's the priority between this setting and the similar setting in WebI query properties?
    However, acutally my question is how to set the limitation by user security profile in CMC.
    Do you have any idea about it?
    Qing
    Edited by: Qing Zhou on Jun 21, 2010 11:12 AM

  • How to set a row as default row in adf table

    Hi,
    I have a requirement: when page is launched there are multiple records with different status displayed on the page and i had to make the first record with Status = XXX as default selected row in adf table. How to code it?
    Thanks!
    Susan

    Hi Frank,
    Thanks a lot for your quick response!
    We are using Jdev 11g. After received your response, i went through all of your responses in ADF coner and OTN for the related topics (setting detail row) and got some ideas(Specially, i got your detail coding for moving makeCurrent expression into selection listener method into backing bean, etc ) and was ready for coding this function. But our PM changed their mind and we dont need this function. So, I had to move to another area......
    Your articles and your responses really help me!
    Thanks again for your help!
    Susan

  • Adobe form:How to Print last row of body to next page & printing max7 rows?

    Hi,
    In ADOBE PDF, Design view, for item level data only 7 row or less should appear in first page for following page if rows are greater than 7 if its less than  7 then last row should be on second page. In design page Below Body Row, user details are 'positioned' so they overlap as soon as item level data exceeds 7.
    Using a Static PDF, 7.2. I checked with Pagination property of Item level data, its been set to Place:Following previous and After Continue filling parent. Will putting condition using Form Calc would suffice in condition breaks? What would be condition?
    Thanks
    Mohinder

    Hi,
    Solved by changing Condition in Table pane, making one row and increasing Header and footer.
    Thanks
    Mohinder

  • How to set one row or node 's height in the jtree?

    How to set height of one row or node in the jtree?
    I found the method :jTree.setRowHeight(height);.
    But I want to set height of one row ,not all rows of a jtree.
    Thanks for help!

    Who can help me?

  • How to set Max Rows Retrieved by user security profile in CMC?

    Hi
    As we know, it is possible to set Max Rows Retrieved with query property (in the area 'Limits').
    However, it is mentioned in official documents that this setting can be overwritten by the BOE administrator in user security profile. Could any one tell me how this is done in CMC?
    Thanks in advance!
    Qing

    Hi Rishit,
    Thanks for your answer!
    Had a long holiday, so didn't give a feedback in time. Sorry for that!
    There is a tab 'Controls' in Universe Parameter, where there is a setting used to limit the size of result set.
    Then what's the priority between this setting and the similar setting in WebI query properties?
    However, acutally my question is how to set the limitation by user security profile in CMC.
    Do you have any idea about it?
    Qing
    Edited by: Qing Zhou on Jun 21, 2010 11:12 AM

  • How to set the row height for a list control in flex 4.5 for mobile?

    Hi,
      I have a List in my Adobe Flash Builder 4.5 and flex 4.5 mobile application. I show the image and the label in my list. The list item height gets adjusted based on the image height. I don't want this auto row adjustment. Instead I would like to set the row height manually.
    Is there any property to set the item height in a list?

    setting rowHeight in the List layout works for me.
    <s:List>
    <s:layout>
    <s:VerticalLayout rowHeight="48"/>
    </s:layout>
    </s:List>

  • How To Find Last Row With Data +2, Then Format?

    I need to find the last row +2, select the range A:L, then format.
    Could someone please supply the code?  I can handle the format part.
    Thanks!
    A. Wolf

    Works great!
    I need to do 2 other things:
    1) Add a text value to r in A: & center over selection A:L
    Dim r As Long
        r = Cells.Find(What:="*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
           USED THIS BUT ENDING UP IN MIDDLE OF L:!
        ActiveCell = Range("A" & r)
        ActiveCell.Value = "Estimated True Up Total"
    2) 2) Format a total in r as currency/2 decimels/aligned to right.
    THANKS AGAIN!!!
    A. Wolf

  • How to set the rows of dataTable using loadBundle tag?

    Hi,
    Can i set the rows property of a <h:dataTable> using the value from a <f:loadBundle> tag?
    I tried the following code
    <f:loadBundle basename="com.message.NumberOfRows" var="numRows"/>
    I have NumberOfRows.properties file with the following entry
    rows=5
    The dataTable code
    <h:dataTable value="#{mastData.list}" rows="#{numRows.rows}" align="center" var="mastBean">
    </h:dataTable>
    This code is generating ClassCast Exception.
    Please comment on this issue.
    Thanks,
    Renju

    Hi,
    Exactly Reymond, See Renju you already using mastData bean for datatable. In that create a bean method something like rows & create a getter/setter method returning int. In getter method try this code as
    ResourceBundle bundle = ResourceBundle.getBundle("ur resource bundle");
    rows = Integer.parseInt(bundle.getString("rows_to_display"));
    return rows.
    Hope you can now get some clear idea i guess
    Regards,
    A.

  • How to select last year's first day

    Hi friends,
    How to get the last year's first day using sysdate as on sysdate
    this gives the current year's start date
    I need last year's start date like 01-jan-2010 as on sysdate
    thanks

    Another:
    SQL> select trunc(sysdate-365.25,'YYYY') from dual;
    TRUNC(SYSDATE-365.25
    01-JAN-2010 00:00:00Note: the .25 accounts for this query being run on the last day of a leap year e.g.
    SQL> select trunc(date '2012-12-31'-365.25,'YYYY') from dual;
    TRUNC(DATE'2012-12-3
    01-JAN-2011 00:00:00Edit: Note, this still isn't completely accurate and could give a wrong result. Better solution is to use the add_months function.
    Edited by: BluShadow on 06-Apr-2011 11:55

Maybe you are looking for

  • How do I restore old 32GB 4s backup to new 16 GB 5s?

    My old iPhone 32G 4s' screen quit working. I managed to get my old 4s backed up (this was no easy feat as iTunes expected me to respond to a prompt on my phone that I could not see). Then, I traded up to a new 5s.  On my first iTunes sync with my new

  • Problem loading from an attached MovieClip

    Project: Flash Professional CS5.5 | AS3 Platform: Mac OSX Lion Hello: I have a projector that calls on an attached movieclip with the following function: function fbtnclicka(e:MouseEvent):void{ var mc:moduleexit_mc=new moduleexit_mc(); The functions

  • Airport status icon blinking in menu

    My MacBook's Airport status icon in the menu bar blinks periodically, losing then recapturing wireless access. Very annoying. Obviously I can't download anything while it's blinking. And this appears to be when I'm connecting to my Airport extreme wi

  • Help with show pop up menu

    I have added a show pop up menu to one of my images however i can only get it to have one column if veritcal or one row if horizontal. is there a way to have multiple rows and columns so that it would read like below. item 1 item 2 item 3 item 4 item

  • Change in KM discussion forum after SP18

    Hi, We have just applied SP18 (from SP14). In our KM discussion forum the time shown for 'last post' is now changed to the time when the post was 'last read' by a user. Have anyone seen this before? and where can I change so it displays when the post