How can we sort these records this way ? - SQL HELP

  • Take a look at the output attached.
    What I need is when there is a value in the histclaim column, 
    then the records that has that  value in t he CLAIMNUM column should be 
    listed immediately below the record that had the HISTCLAIM.

    Ex: #6 should be immediately under #2

    https://www.sqlservercentral.com/Forums/Uploads/Images/bddad286-7b66-4e97-a4be-c7e0.PNG


    IF object_id('tempdb..#a') IS NOT NULL DROP TABLE #a;

    create table #a
    (
    memberid varchar(40),
    claimnum varchar(10),
    claimB varchar(10),
    histclaim varchar(10)
    )

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a100', 'CLM050','CLAIMB', NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a100', 'CLM200','CLAIMB200', 'CLM100' ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'aCVB', 'CLM300FF',NULL, NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'aCVBX', 'CLM350FF',NULL, NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a105', 'CLM700',NULL, NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a100', 'CLM100','CLAIMB', NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a400', 'CLM600','CLAIMB', NULL ) ;

    Select * FROM #a;

  • Select * FROM #a order by memberid, histclaim DESC

    is that the order you require?

    -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    This thing is addressing problems that dont exist. Its solution-ism at its worst. We are dumbing down machines that are inherently superior. - Gilfoyle

  • What defines the rest of your ordering? Do you need to retain the ordering in memberid of a100, then aCVB, then aCvbx, then a105, etc? Does the rest of the ordering actually matter, or do you only care about having a claim being the line after it's a line with it's histclaim?

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Bekker: Thanks That works!


    IF object_id('tempdb..#a') IS NOT NULL DROP TABLE #a;

    create table #a
    (
    memberid varchar(40),
    claimnum varchar(10),
    claimB varchar(10),
    histclaim varchar(10)
    )

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a100', 'CLM050','CLAIMB', NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a100', 'CLM200','CLAIMB200', 'CLM100' ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a200', 'CLM200','CLAIMB200', 'CLX200' ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a200', 'CLX200','CLAIMB200', '' ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'aCVB', 'CLM300FF',NULL, NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'aCVBX', 'CLM350FF',NULL, NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a105', 'CLM700',NULL, NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a100', 'CLM100','CLAIMB', NULL ) ;

    INSERT INTO #a( memberid, claimnum, claimB, histclaim )
    VALUES
    ( 'a400', 'CLM600','CLAIMB', NULL ) ;

    Select * FROM #a order by memberid, histclaim DESC

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

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