SQL Developer - Pulling column value from previous row

I am using Oracle SQL developer and trying to pull a value from a column from the previous row.
Currently this is an example from the code im using.
Select CD1.nbr_dw, CD1.SEQ_DW, CD1.CDL_NET_PAID, CD1.UNITS_ALLOW,CD1.ALLOW_AMT, CD1.UNIT_PRICE, CD1.BACKED_OUT,
case
When CD1.NET_PAID = 0 then 0
****NEW CODE*****
Else CD1.ALLOW_AMT / CD1.UNIT_PRICE
end as NEW_UNITS
From Table_A CD1
join (Select nbr_dw, DET_DW, SEQ_DW
From Table_B
) CD2
on CD1.nbr_dw = CD2.nbr_dw
I need to add another when statement that says when CD1.BACKED_OUT = 'Y' populated the NEW_UNITS field with the previous rows value of CD1.UNIT_PRICE. Note, the CD1.SEQ_DW can be used to determine the previous row. Any ideas?

Hi,
Welcome to the forum!
The analytic LAG function can return a value from the previous row:
LAG (cd1.unit_price) OVER ( -- PARTITION BY  x          -- if needed
                               ORDER BY          cd1.seq_dw
                 )It's unclear if you need a PARTITION BY clause or not.
"PARTITION BY x" means that you want the value from the last row that has the same value of x as the current row.
If you omit the PARTITION BY clause, it means you want the last row regardless of what any other columns are.
Whenever you have a problem, it ehlps if you post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
Explain how you get those results from that data.
Always say what version of Oracle you're using.

Similar Messages

  • How To Concatenate Column Values from Multiple Rows into a Single Column?

    How do I create a SQL query that will concatenate column values from multiple rows into a single column?
    Last First Code
    Lesand Danny 1
    Lesand Danny 2
    Lesand Danny 3
    Benedi Eric 7
    Benedi Eric 14
    Result should look like:
    Last First Codes
    Lesand Danny 1,2,3
    Benedi Eric 7,14
    Thanks,
    David Johnson

    Starting with Oracle 9i
    select last, first, substr(max(sys_connect_by_path(code,',')),2) codes
    from
    (select last, first, code, row_number() over(partition by last, first order by code) rn
    from a)
    connect by last = prior last and first = prior first and prior rn = rn -1
    start with rn = 1
    group by last, first
    LAST       FIRST      CODES                                                                                                                                                                                                  
    Lesand         Danny          1,2,3
    Benedi         Eric           7,14Regards
    Dmytro

  • Update field taking value from previous row

    I have a table TEST (ID int, name varchar(20), amount decimal(18,0)).
    First row of the table have correct value. I want to update amount column except first row by adding 10 with the previous row value without using cursor.
    id    Name    Amount
    1    abc    10
    2    def    15
    3    ghi    20
    4    jkl    22
    5    mno    25
    6    pqr    27
    7    stu    30
    8    vwx    33
    id    Name    Amount
    1    abc    10
    2    def    20
    3    ghi    30
    4    jkl    40
    5    mno    50
    6    pqr    60
    7    stu    70
    8    vwx    80
    please help me.
    thanks
    srimanta manna

    create table #t (id int, amount int)
    insert into #t values (1,10)
    insert into #t values (2,25)
    insert into #t values (3,46)
    insert into #t values (4,55)
    insert into #t values (5,160)
    insert into #t values (6,89)
    insert into #t values (7,100)
    with cte
    as
    select *,(select min(amount) from #t t where t.id=1) as a
    FROM #t 
    ) update #t set amount=(select sum(a) from cte c where c.id<=cte.id) 
     from cte where #t.id=cte.id
     select * from #t
    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

  • Custom row-fetch and how to get column values from specific row of report

    Hi -- I have a case where a table's primary key has more than 3 columns. My report on the
    table has links that send the user to a single-row DML form, but of course the automatic
    fetch won't work because 1) I can't set more than 3 item values in the link and 2) the
    auto fetch only handles 2 PK columns.
    1)
    I have written a custom fetch (not sure it's the most elegant, see second question) that is working
    for 3 or few PK columns (it references the 1-3 item values set in the link), but when there are
    more than 3, I don't know how to get the remaining PK column values for the specific row that was
    selected in the report. How can I access that row's report column values? I'll be doing it from the
    form page, not the report page. (I think... unless you have another suggestion.)
    2)
    My custom fetch... I just worked something out on my own, having no idea how this is typically
    done. For each dependent item (database column) in the form, I have a source of PL/SQL
    function that queries the table for the column in question, using the primary key values. It works
    beautifully, though is just a touch slow on my prototype table, which has 21 columns. Is there
    a way to manually construct the fetch statement once for the whole form, and have APEX be smart
    about what items get what
    return values, so that I don't have to write PL/SQL for every item? Because my query data sources
    are sometimes in remote databases, I have to write manual fetch and dml anyway. Just would like
    to streamline the process.
    Thanks,
    Carol

    HI Andy -- Well, I'd love it if this worked, but I'm unsure how to implement it.
    It seems I can't put this process in the results page (the page w/ the link, that has multiple report rows), because the link for the row will completely bypass any after-submit processes, won't it? I've tried this in other conditions; I thought the link went directly to the linked-to page.
    And, from the test of your suggestion that I've tried, it's not working in the form that allows a single row edit. I tried putting this manually-created fetch into a before header process, and it seems to do nothing (even with a hard-coded PK value, just to test it out). In addition, I'm not sure how, from this page, the process could identify the correct PK values from the report page, unless it can know something about the row that was selected by clicking on the link. It could work if all the PK columns in my edit form could be set by the report link, but sometimes I have up to 5 pk columns.
    Maybe part of the problem is something to do with the source type I have for each of the form items. With my first manual fetch process, they were all pl/sql functions. Not sure what would be appropriate if I can somehow do this with a single (page level?) process.
    Maybe I'm making this too hard?
    Thanks,
    Carol

  • Sum of Values from previous rows in a dynamic table

    Hello,
    I have a dynamic table has multiple rows (added dynamically via a button).  One column in the table is a Quantity column.  Another is a Total column.  We do not want to keep a running total, but only wish to display a total when the rest of the cells in that row are emty and the previous row(s) has/have values in the Quantity column.  The Total is the sum on the Quatnities up to a blank row.  Below is a sample.  How do I get teh value of the Quantities in the previous rows?
    Quantity
    Total
    5
    8
    12
    25
    7
    27
    34
    22
    22
    Another option might be to put the Total in the last row with data as opposed to the row below the data.
    Any ideas?
    Regards,
    Karl

    The script is written to achive your requirement (i.e. the second approach you have mentioned)
    You can either contact me at [email protected] to get the sample form created.
    Or the following is the XML Source of the form. Copy the entire content in XML source view of your form and see the result.
    <?xml version="1.0" encoding="UTF-8"?>
    <?xfa generator="AdobeLiveCycleDesignerES_V9.0.0.0.20091029.1.612548" APIVersion="3.1.9277.0"?>
    <xdp:xdp xmlns:xdp="http://ns.adobe.com/xdp/" timeStamp="2012-02-28T14:04:54Z" uuid="2c561cdf-3377-4e84-9a89-51e740bb2fea">
    <template xmlns="http://www.xfa.org/schema/xfa-template/2.8/">
    <?formServer defaultPDFRenderFormat acrobat9.0dynamic?>
    <subform name="form1" layout="tb" locale="en_US" restoreState="auto">
    <pageSet>
    <pageArea name="Page1" id="Page1">
    <contentArea x="0.25in" y="0.25in" w="197.3mm" h="284.3mm"/>
    <medium stock="a4" short="210mm" long="297mm"/>
    <?templateDesigner expand 1?></pageArea>
    <?templateDesigner expand 1?></pageSet>
    <subform w="197.3mm" layout="tb">
    <subform name="Subform1" w="190.5mm" h="25.4mm">
    <field name="Button1" y="15.875mm" x="60.325mm" w="28.575mm" h="6mm">
    <ui>
    <button highlight="inverted"/>
    </ui>
    <font typeface="Myriad Pro"/>
    <caption>
    <value>
    <text>Add Row</text>
    </value>
    <para vAlign="middle" hAlign="center"/>
    </caption>
    <border hand="right">
    <?templateDesigner StyleID apbx2?>
    <edge stroke="raised"/>
    <fill/>
    </border>
    <bind match="none"/>
    <event activity="click" name="event__click">
    <script contentType="application/x-javascript">
    try
    Subform2.Table1._Row1.addInstance(1);
    //for(var i=0;i&lt;Subform2.Table1._Row1.count;i++)
    //xfa.resolveNode("Subform2.Table1.Row1["+i+"].Total").execEvent("exit");
    }catch(e)
    app.alert(e)
    </script>
    </event>
    </field>
    <?templateDesigner expand 1?></subform>
    <subform name="Subform2" w="196.85mm">
    <subform name="Table1" layout="table" columnWidths="55.916mm 44.47mm" x="38.1mm" y="12.7mm">
    <border>
    <edge/>
    </border>
    <subform layout="row" name="HeaderRow" id="HeaderRow_ID">
    <assist role="TH"/>
    <draw h="10mm" name="Cell1">
    <border>
    <edge/>
    <corner thickness="0.1778mm"/>
    </border>
    <ui>
    <textEdit/>
    </ui>
    <value>
    <text>Quantity</text>
    </value>
    <font typeface="Myriad Pro"/>
    <margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>
    <para vAlign="middle" hAlign="center"/>
    </draw>
    <draw h="10mm" name="Cell2">
    <border>
    <edge/>
    <corner thickness="0.1778mm"/>
    </border>
    <ui>
    <textEdit/>
    </ui>
    <value>
    <text>Total</text>
    </value>
    <font typeface="Myriad Pro"/>
    <margin topInset="0.5mm" bottomInset="0.5mm" leftInset="0.5mm" rightInset="0.5mm"/>
    <para vAlign="middle" hAlign="center"/>
    </draw>
    <border>
    <edge presence="hidden"/>
    </border>
    <occur max="-1"/>
    <?templateDesigner expand 1?></subform>
    <subform layout="row" name="Row1">
    <assist role="TR"/>
    <field name="Quantity" w="55.916mm" h="9.317mm">
    <ui>
    <numericEdit>
    <border presence="hidden">
    <?templateDesigner StyleID aped0?></border>
    <margin/>
    </numericEdit>
    </ui
    ><
    font typeface="Myriad Pro"/><
    margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/><
    para vAlign="middle"/><
    border><
    edge/><
    corner thickness="0.1778mm"/></
    border></
    field><
    field name="Total" w="44.47mm" h="9.317mm" access="readOnly"><
    ui><
    numericEdit><
    border presence="hidden"> 
    <?templateDesigner StyleID aped0?>
    </border><
    margin/></
    numericEdit></
    ui><
    font typeface="Myriad Pro"/><
    margin topInset="1mm" bottomInset="1mm" leftInset="1mm" rightInset="1mm"/><para vAlign="middle"/>
    <border>
    <edge/>
    <corner thickness="0.1778mm"/>
    </border>
    <calculate>
    <script contentType="application/x-javascript">
    try
    var nextRow = xfa.resolveNode("Subform2.Table1.Row1["+(this.parent.instanceIndex+1)+"]"); // Next row of the table
    if((nextRow==null || nextRow.Quantity.rawValue == null || nextRow.Quantity.rawValue=="") &amp;&amp; Quantity.rawValue!=null) // If there is no more rows OR the next row don't contain any Quantity value
    var subTotal = 0;
    for(var j=this.parent.instanceIndex;j&gt;=0;j--) // Loop until the previous row has empty value OR reach start of row Index (i.e. 0)
    var prevObj = xfa.resolveNode("Subform2.Table1.Row1["+j+"].Quantity"); // locate the previous row's Quantity object
    if(prevObj.rawValue == null || prevObj.rawValue=="")
    break; // if blank value found, skip the loop
    subTotal+= prevObj.rawValue;
    this.rawValue = subTotal; // Update the Total value
    }else
    this.rawValue = null; // Clear the total value
    }catch(e)
    app.alert(e)
    </script>
    </calculate>
    </field>
    <border>
    <edge presence="hidden"/>
    </border>
    <occur max="-1"/>
    <?templateDesigner expand 1?></subform>
    <keep intact="contentArea"/>
    <?templateDesigner rowpattern first:1, next:1, firstcolor:f0f0f0, nextcolor:ffffff, apply:0?>
    <overflow leader="HeaderRow"/>
    <?templateDesigner expand 1?></subform>
    <?templateDesigner expand 1?></subform>
    <?templateDesigner expand 1?></subform>
    <proto/>
    <desc>
    <text name="version">9.0.0.0.20091029.1.612548.606130</text>
    </desc>
    <?templateDesigner expand 1?></subform>
    <?templateDesigner DefaultPreviewDynamic 1?>
    <?templateDesigner DefaultRunAt client?>
    <?templateDesigner Grid show:1, snap:1, units:0, color:ff8080, origin:(0,0), interval:(125000,125000)?>
    <?templateDesigner FormTargetVersion 28?>
    <?templateDesigner DefaultLanguage JavaScript?>
    <?acrobat JavaScript strictScoping?>
    <?templateDesigner Zoom 62?>
    <?templateDesigner Rulers horizontal:1, vertical:1, guidelines:1, crosshairs:0?>
    <?templateDesigner SaveTaggedPDF 1?>
    <?templateDesigner SavePDFWithEmbeddedFonts 1?></template>
    <config xmlns="http://www.xfa.org/schema/xci/2.8/">
    <agent name="designer">
    <!-- [0..n] -->
    <destination>pdf</destination>
    <pdf>
    <!-- [0..n] -->
    <fontInfo/>
    </pdf>
    </agent>
    <present>
    <!-- [0..n] -->
    <pdf>
    <!-- [0..n] -->
    <fontInfo/>
    <version>1.7</version>
    <adobeExtensionLevel>3</adobeExtensionLevel>
    </pdf>
    <xdp>
    <packets>*</packets>
    </xdp>
    </present>
    </config>
    <localeSet xmlns="http://www.xfa.org/schema/xfa-locale-set/2.7/">
    <locale name="en_US" desc="English (United States)">
    <calendarSymbols name="gregorian">
    <monthNames>
    <month>January</month>
    <month>February</month>
    <month>March</month>
    <month>April</month>
    <month>May</month>
    <month>June</month>
    <month>July</month>
    <month>August</month>
    <month>September</month>
    <month>October</month>
    <month>November</month>
    <month>December</month>
    </monthNames>
    <monthNames abbr="1">
    <month>Jan</month>
    <month>Feb</month>
    <month>Mar</month>
    <month>Apr</month>
    <month>May</month>
    <month>Jun</month>
    <month>Jul</month>
    <month>Aug</month>
    <month>Sep</month>
    <month>Oct</month>
    <month>Nov</month>
    <month>Dec</month>
    </monthNames>
    <dayNames>
    <day>Sunday</day>
    <day>Monday</day>
    <day>Tuesday</day>
    <day>Wednesday</day>
    <day>Thursday</day>
    <day>Friday</day>
    <day>Saturday</day>
    </dayNames>
    <dayNames abbr="1">
    <day>Sun</day>
    <day>Mon</day>
    <day>Tue</day>
    <day>Wed</day>
    <day>Thu</day>
    <day>Fri</day>
    <day>Sat</day>
    </dayNames>
    <meridiemNames>
    <meridiem>AM</meridiem>
    <meridiem>PM</meridiem>
    </meridiemNames>
    <eraNames>
    <era>BC</era>
    <era>AD</era>
    </eraNames>
    </calendarSymbols>
    <datePatterns>
    <datePattern name="full">EEEE, MMMM D, YYYY</datePattern>
    <datePattern name="long">MMMM D, YYYY</datePattern>
    <datePattern name="med">MMM D, YYYY</datePattern>
    <datePattern name="short">M/D/YY</datePattern>
    </datePatterns>
    <timePatterns>
    <timePattern name="full">h:MM:SS A Z</timePattern>
    <timePattern name="long">h:MM:SS A Z</timePattern>
    <timePattern name="med">h:MM:SS A</timePattern>
    <timePattern name="short">h:MM A</timePattern>
    </timePatterns>
    <dateTimeSymbols>GyMdkHmsSEDFwWahKzZ</dateTimeSymbols>
    <numberPatterns>
    <numberPattern name="numeric">z,zz9.zzz</numberPattern>
    <numberPattern name="currency">$z,zz9.99|($z,zz9.99)</numberPattern>
    <numberPattern name="percent">z,zz9%</numberPattern>
    </numberPatterns>
    <numberSymbols>
    <numberSymbol name="decimal">.</numberSymbol>
    <numberSymbol name="grouping">,</numberSymbol>
    <numberSymbol name="percent">%</numberSymbol>
    <numberSymbol name="minus">-</numberSymbol>
    <numberSymbol name="zero">0</numberSymbol>
    </numberSymbols>
    <currencySymbols>
    <currencySymbol name="symbol">$</currencySymbol>
    <currencySymbol name="isoname">USD</currencySymbol>
    <currencySymbol name="decimal">.</currencySymbol>
    </currencySymbols>
    <typefaces>
    <typeface name="Myriad Pro"/>
    <typeface name="Minion Pro"/>
    <typeface name="Courier Std"/>
    <typeface name="Adobe Pi Std"/>
    <typeface name="Adobe Hebrew"/>
    <typeface name="Adobe Arabic"/>
    <typeface name="Adobe Thai"/>
    <typeface name="Kozuka Gothic Pro-VI M"/>
    <typeface name="Kozuka Mincho Pro-VI R"/>
    <typeface name="Adobe Ming Std L"/>
    <typeface name="Adobe Song Std L"/>
    <typeface name="Adobe Myungjo Std M"/>
    </typefaces>
    </locale>
    </localeSet>
    <x:xmpmeta xmlns:x="adobe:ns:meta/" x:xmptk="Adobe XMP Core 4.2.1-c043 52.398682, 2009/08/10-13:00:47 ">
    <
    rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><
    rdf:Description xmlns:xmp="http://ns.adobe.com/xap/1.0/" rdf:about=""><
    xmp:MetadataDate>2012-02-28T14:04:54Z</xmp:MetadataDate><
    xmp:CreatorTool>Adobe LiveCycle Designer ES 9.0</xmp:CreatorTool></
    rdf:Description><
    rdf:Description xmlns:pdf="http://ns.adobe.com/pdf/1.3/" rdf:about=""><
    pdf:Producer>Adobe LiveCycle Designer ES 9.0</pdf:Producer></
    rdf:Description><
    rdf:Description xmlns:xmpMM="http://ns.adobe.com/xap/1.0/mm/" rdf:about=""><
    xmpMM:DocumentID>uuid:2c561cdf-3377-4e84-9a89-51e740bb2fea</xmpMM:DocumentID></
    rdf:Description><
    rdf:Description xmlns:desc="http://ns.adobe.com/xfa/promoted-desc/" rdf:about=""><
    desc:version rdf:parseType="Resource"><
    rdf:value>9.0.0.0.20091029.1.612548.606130</rdf:value><
    desc:ref>/template/subform[1]</desc:ref></
    desc:version></
    rdf:Description></
    rdf:RDF></
    x:xmpmeta></xdp:xdp>

  • Expanding table: Calculating difference to value from previous row

    Hi there,
    I am a complete newbie to LiveCycle Designer (ES2) and have created below document with an expanding table.
    Shared Files - Acrobat.com
    My aim is to make the form user friendly by adding a calculation in the field "Twist" which calculates the difference from the current row to the previous row.
    I have no idea about Java Script, I am afraid, and also don't know if this is possible at all so it's probably a bad idea after all.
    The reason for setting it up as an expanding table is so that the form can be used for all 32 instances the form would be used for (they would all require different amount of rows altogether).
    The calculation would start in row 2 (Checklist.Table1), take the twist value entered in row 2 and calculate the difference to the twist value entered in row 1.
    Then in row 3, it would calculate the difference between twist value in row 3 to row 2 etc.
    So subtract value of field in rown with value in rown-1 or something in this direction.
    Is there anyone at all who could help me with this at all?
    I'd be very grateful for any advice you could give me.
    Thanks a lot!
    Mathilda

    Not sure what happened with the formatting there, try this;
    if (Row1.index > 0 && !Row1.resolveNode("NumericField4[0]").isNull)
        var prevTwist = Table1.resolveNode("Row1[" + (Row1.index - 1) + "].NumericField4[0]").rawValue
        var result = Math.abs(3000 / (Row1.resolveNode("NumericField4[0]").rawValue - prevTwist));
       this.rawValue = result;
    else
        this.rawValue = "";

  • Getting previous value from previous row

    Hi,
    My desired output should be
    ID     Book_date     Credit     Previous_Credit
    123IT1     12/8/2008     100800     72500
    from this table:
    ID     Book date     Credit
    123IT1     12/8/2008     100800
    123IT1     12/18/2007     72500
    123IT1     11/14/2006     150720
    I tried to use the lead function but I'm having problem when I included the date range condtion:
    This is my query:
    select id, book_date, credit, lead(credit) over (order by rownum) as previous_credit
    from t
    where book_date between '01-December-2008' and '31-December-2008'
    Result
    ID     Book_date     Credit     Previous_Credit
    123IT1     12/8/2008     100800     
    The previous_credit column is always blank. I think because of the date range..?
    Any idea how to get the right result? pls. help

    Your expected output indicates that you may require a LAG
    SQL> with t as
      2  (
      3    select '123IT1' id, to_date('12/08/2008','mm/dd/yyyy') Book_Date, 100800 Credit from dual uni
    on
      4    select '123IT1', to_date('12/18/2007','mm/dd/yyyy'), 72500 from dual union
      5    select '123IT1', to_date('11/14/2006','mm/dd/yyyy'), 150720 from dual
      6  )
      7  --
      8  select id
      9        ,Book_Date
    10        ,Credit
    11        ,lag(Credit) over (partition by id order by Book_Date) Previous_Credit
    12  from t
    13  /
    ID     BOOK_DATE     CREDIT PREVIOUS_CREDIT
    123IT1 14-NOV-06     150720
    123IT1 18-DEC-07      72500          150720
    123IT1 08-DEC-08     100800           72500If you always require a Previous Credit then you may want to modify this:
    (partition by id order by Book_Date)to this:
    (order by id, Book_Date)

  • Is it possible to Lock just one column value in a row?

    Is it possible to Lock just one column value in a row
    A Java Developer has just asked me if it is possible to Lock just one column value in a row in Oracle
    select ename from emp where empno=7369 for update;
    EMPNO ENAME      JOB              MGR HIREDATE         SAL       COMM     DEPTNO
    7369 SMITH      CLERK           7902 17-DEC-80        800                    20
    will lock the entire row with empno=7369.
    But is it possible to lock one column value from this row, like SAL=800 ?
    Edited by: J.Kiechle on Jan 8, 2009 10:45 PM

    J.Kiechle wrote:
    Is it possible to Lock just one column value in a rowNo. Locking granularity is a row.

  • How to get the value of column from previous row to current row?

    Hi All,
    I am facing a critical problem in SQL query (for reporting purpose (COGNOS)). please reply to my query.
    REQUIREMENT: i want to retrive value of a column (this is not a table column, this will be calculated based expression) from previous row to current row.
    EXAMPLE:
    TABLE NAME: i have to join multiple tables. so, i am not mentioning table names here.
    DISPLAY COLUMNS in the report: item, loc, sku, beginval (not table column), endval (not table column, this will calculated based on some expressions), etc. Here, first time the value of BEGINVAL will be taken from some x column name of x table. ENDVAL will calculated based on the expression. For the next row onwards, BEGINVAL will become the value of ENDVAL and the value of ENDVAL will be calculated based on the expression and this expression uses the value of BEGINVAL.
    my report will look like below.
    ITEM LOC SKU BEGINVAL ENDVAL
    1 HYD 1-HYD 10 10+1+2 (13)
    2 HYD 2-HYD 13 13+1+2 (16)
    3 SEC 3-SEC 16 16+1+2 (19)
    4 SEC 4-SEC 19 19+1+2 (22)
    etc....
    in the above output, BEGINVAL and ENDVAL columns are not part of any table. they are alias column names.if you observe intially BEGINVAL will be assigned to some value, but from subsequent rows, BEGINVAL will become the ENDVAL of previous row.
    Please help me on how to write this query?

    Hi, please find the detail description of the problom.
    DDL (table scripts)
    ITEM
    CREATE TABLE ITEM
    ITEM VARCHAR2(50 CHAR) NOT NULL,
    DESCR VARCHAR2(50 CHAR) DEFAULT ' ' NOT NULL,
    UOM           VARCHAR2(50 CHAR)
    SKU
    CREATE TABLE SKU
    ITEM VARCHAR2(50 CHAR) NOT NULL,
    LOC VARCHAR2(50 CHAR) NOT NULL,
    OH float
    SKUPROJSTATIC
    CREATE TABLE SKUPROJSTATIC
    ITEM VARCHAR2(50 CHAR) NOT NULL,
    LOC VARCHAR2(50 CHAR) NOT NULL,
    STARTDATE DATE
    VehicleLoad
    CREATE TABLE VEHICLELOAD
    LOADID VARCHAR2(50 CHAR) NOT NULL,
    DESCR VARCHAR2(50 CHAR) DEFAULT ' ' NOT NULL,
    SHIPDATE DATE DEFAULT TO_DATE('01/01/1970','MM/DD/YYYY') NOT NULL,
    ARRIVDATE      DATE
    VEHICLELOADLINE
    CREATE TABLE VEHICLELOADLINE
    LOADID VARCHAR2(50 CHAR) NOT NULL,
    ITEM VARCHAR2(50 CHAR) NOT NULL,
    QTY float(126)
    DML scripts
    I can't provide the the DML scripts, because its very huge data.
    Main QUERY is below.
    below is the query which i have to write and execute.
    select
    i.item,
    i.descr,
    i.unitsperpallet,
    sp.loc,
    sp.startdate,
    'Crucial_IND',
    LAG (EndBal,1,0) OVER (ORDER BY STARTDATE) BeginBal,
    SP.FCSTCUSTORDERS CustOrders,
    SP.COMMITINTRANSOUT,
    SP.RECARRIV,
    SP.TOTINTRANSIN,
    (BeginBal - sp.FCSTCUSTORDERS - sp.COMMITINTRANSOUT + sp.TOTINTRANSIN ) EndBal,
    'CuttingQty',
    VLL.QTY,
    vl.source,
    vl.arrivdate,
    vl.shipdate,
    vl.loadid,
    s.oh
    from item i, skuprojstatic sp, sku s, VehicleLoad vl, VehicleLoadLine vll
    where sp.item = i.item
    and s.item=i.item
    and sp.item =s.item (+)
    and sp.loc = s.loc (+)
    and vll.item = s.item
    and vll.loadid = vl.loadid
    and to_char(sp.startdate ,'mm/dd/yyyy') = to_char(vl.arrivdate,'mm/dd/yyyy')
    and sp.loc = vl.dest (+)
    order by sp.startdate
    problem description: in the above query, BeginBal and EndBal is what i am looking for. when i execute this query, oracle throwing an error saying that alias names can't be used as expressions. For the first row, the BeginBal should be the value sku.OH and for the subsequent rows, this value will be the previous row value of EndBal.

  • Pulling a value from a field for a SQL statement

    How do i pull the value from a Formatted Text Field into a sql query? Im trying to create an interface to allow a user to put the first name in a field then poll the database for anyone of that name. I do not know of how to get the value that is put in the field by the user.

    Roy, can you give more details about what version of JDeveloper you are using?

  • Reg : Concatenation of a column value with multiple rows... URGENT

    Hello,
    Could any of u help me in concatenating a column value with
    multiple rows ???
    For ex : I've the following data from emp table :
    DEPTNO ENAME
    10 KING'S
    30 BLAKE
    10 CLARK
    10 TOM JONES
    30 ALLEN
    30 JAMES
    20 SMITH
    20 SCOTT
    20 MILLER
    10 MILLER
    20 rajeev
    I want the following output :
    deptno Concat_value
    10 KING'S,CLARK,TOM JONES,MILLER
    20 Rajeev,MILLER,SMITH,SCOTT
    30 BLAKE,ALLEN,JAMES
    Thanks in Advance,
    Srini

    Hello Naveen,
    Thanks for ur answer. But I need a single SQL query for getting
    what I want. I know the solution in PL/SQL.
    Please try it in a single SQL....
    Thanks again,
    Srini

  • Copying values from a row to a current row

    hi all, i have a complex scenario here. i have a table that has the following data.
    WITH table1 AS
      SELECT 111 the_id,  To_Date('6/30/2009','mm/dd/yyyy') dt,  93 src, 'A-C' the_name, 'AA' cod, 'BB' pad, ' CC' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/1/2009','mm/dd/yyyy') dt,  93 src, 'B-L' the_name, 'AA' cod, 'NN' pad, ' TT' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('10/2/2009','mm/dd/yyyy') dt,  93 src, 'C-H' the_name, 'BA' cod, 'RR' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/5/2009','mm/dd/yyyy') dt,  93 src, 'D-R' the_name, 'HH' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/6/2009','mm/dd/yyyy') dt,  93 src, 'E-C' the_name, 'JJ' cod, 'EE' pad, ' JJ' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/7/2009','mm/dd/yyyy') dt,  93 src, 'F-L' the_name, 'DD' cod,  null pad, ' BAN' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('10/8/2009','mm/dd/yyyy') dt,  93 src, 'G-H' the_name, 'HG' cod, 'YY' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/9/2009','mm/dd/yyyy') dt,  93 src, 'K-R' the_name, 'EE' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('7/31/2009','mm/dd/yyyy') dt,  93 src, 'G-H' the_name, 'HG' cod, 'YY' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('8/3/2009','mm/dd/yyyy') dt,  93 src, 'K-R' the_name, 'EE' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('8/4/2009','mm/dd/yyyy') dt,  93 src, 'K-R' the_name, 'EE' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('8/5/2009','mm/dd/yyyy') dt,  93 src, 'K-R' the_name, 'EE' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('8/6/2009','mm/dd/yyyy') dt,  93 src, 'K-R' the_name, 'EE' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('8/7/2009','mm/dd/yyyy') dt,  93 src, 'K-R' the_name, 'EE' cod, 'FCY' pad,  null tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('6/30/2009','mm/dd/yyyy') dt,  22 src, 'A-C' the_name, 'AA' cod, 'BB' pad, ' CC' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/1/2009','mm/dd/yyyy') dt,  22 src, 'B-L' the_name, 'AA' cod, 'NN' pad, ' TT' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('10/2/2009','mm/dd/yyyy') dt,  22 src, 'C-H' the_name, 'BA' cod, 'RR' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/5/2009','mm/dd/yyyy') dt,  22 src, 'D-R' the_name, 'HH' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/6/2009','mm/dd/yyyy') dt,  22 src, 'E-C' the_name, 'JJ' cod, 'EE' pad, ' JJ' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/7/2009','mm/dd/yyyy') dt,  22 src, 'F-L' the_name, 'DD' cod, 'FIC' pad, ' BAN' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('10/8/2009','mm/dd/yyyy') dt,  22 src, 'G-H' the_name, 'HG' cod, 'YY' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/9/2009','mm/dd/yyyy') dt,  22 src, 'K-R' the_name, 'EE' cod, 'FCY' pad, ' BANW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('10/30/2009','mm/dd/yyyy') dt, 168 src, 'T-C' the_name, 'WW' cod, 'AA' pad, ' NN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('11/2/2009','mm/dd/yyyy') dt,  168 src, 'Y-L' the_name, 'HH' cod, 'AA' pad, ' BAN' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('11/3/2009','mm/dd/yyyy') dt,  168 src, 'G-H' the_name, 'BA' cod, 'WW' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('11/4/2009','mm/dd/yyyy') dt,  168 src, 'E-R' the_name, 'JJ' cod, 'XX' pad, ' DD' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('11/5/2009','mm/dd/yyyy') dt,  168 src, 'T-C' the_name, 'TT' cod, 'JJ' pad, ' WW' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('11/6/2009','mm/dd/yyyy') dt,  168 src, 'H-L' the_name, 'EE' cod, 'DD' pad, ' BAN' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('11/9/2009','mm/dd/yyyy') dt,  168 src, 'S-H' the_name, 'BA' cod, 'WW' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('11/10/2009','mm/dd/yyyy') dt, 168 src, 'H-R' the_name, 'VV' cod, 'FCY' pad, ' YY' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('11/30/2009','mm/dd/yyyy') dt,  90 src, 'V-C' the_name, 'GG' cod, 'FCS' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('12/1/2009','mm/dd/yyyy') dt,  90 src, 'B-L' the_name, 'QQ' cod, 'GG' pad, ' BAN' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('12/2/2009','mm/dd/yyyy') dt,  90 src, 'A-H' the_name, 'BA' cod, 'WW' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('12/3/2009','mm/dd/yyyy') dt,  90 src, 'N-R' the_name, 'TT' cod, 'JJ' pad, ' TT' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('12/7/2009','mm/dd/yyyy') dt,  90 src, 'C-C' the_name, 'YY' cod, 'QQ' pad, ' WE' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('12/8/2009','mm/dd/yyyy') dt,  90 src, 'X-L' the_name, 'UU' cod, 'JJ' pad, ' BAN' tek FROM dual UNION all
      SELECT 111 the_id,  To_Date('12/9/2009','mm/dd/yyyy') dt,  90 src, 'V-H' the_name, 'BA' cod, 'PP' pad, ' YR' tek FROM dual UNION ALL
      SELECT 111 the_id,  To_Date('12/10/2009','mm/dd/yyyy') dt,  90 src, 'E-R' the_name, 'FIN2' cod, 'FCY' pad, ' RE' tek FROM dual UNION ALL
      SELECT 112 the_id,  To_Date('2/28/2009','mm/dd/yyyy') dt,  93 src, 'V-C' the_name, 'EE' cod, 'FCS' pad, ' BAN' tek FROM dual UNION ALL
      SELECT 112 the_id,  To_Date('3/2/2009','mm/dd/yyyy') dt,  93 src, 'A-S' the_name, 'BA' cod, 'WW' pad, ' TT' tek FROM dual UNION ALL
      SELECT 112 the_id,  To_Date('3/3/2009','mm/dd/yyyy') dt,  93 src, 'N-R' the_name, 'TT' cod, 'JJ' pad, ' TT' tek FROM dual UNION ALL
      SELECT 112 the_id,  To_Date('3/4/2009','mm/dd/yyyy') dt,  93 src, 'C-C' the_name, 'YY' cod, 'QQ' pad, ' WE' tek FROM dual UNION ALL
      SELECT 112 the_id,  To_Date('3/5/2009','mm/dd/yyyy') dt,  93 src, 'X-L' the_name, 'SS' cod, 'JJ' pad, ' BAN' tek FROM dual UNION all
      SELECT 112 the_id,  To_Date('3/6/2009','mm/dd/yyyy') dt,  93 src, 'V-H' the_name, 'BA' cod, 'PP' pad, ' YR' tek FROM dual UNION ALL
      SELECT 112 the_id,  To_Date('3/9/2009','mm/dd/yyyy') dt,  93 src, 'E-R' the_name, 'HH' cod, 'KK' pad, ' RE' tek FROM dual
    )so as you can see, there are month end dates and well and daily dates. the daily are basically business days. what i want to do is the following
    for a particular id, i would like to take the month end date, look forward 5 business day and copy the column values from the 5th business day
    to the current row.
    there is two exceptions:
    1. i am only interested on those src=93 and 168.
    2. the_name column will not be copy, but the rest will
    so lets take an example, id = 111 src = 93. since i am only interested in src=93 and 168, this data fall in that category.
    the month end dates for id=111 and src=93 are 6/30/09 and 7/31/2009.
    in this case i want to look forward 5 business days. in the case of 6/30/09, the 5th business day is 10/7/2009 and for 7/31/2009 is 8/7/2009.
    i want to take all the column values except the_name column and display those values for 6/30/09. same thing with 7/31/2009, take column values from 8/7/2009
    and display those values.
    so the output for 6/30/2009 should look like this
    THE_ID     DT            SRC     THE_NAME     COD       PAD      TEK
    111         6/30/2009     93     A-C           DD               BANif ou notice here 6/30/2009 retain the value for the_name column but took the values for the remaining column from 10/7/2009 (even the null values got copy)
    the same logic should apply to 7/31/2009 and value should be taking from 8/7/2009
    if the src is not equal to 93 or 168. then just display the data and no logic apply, for id=111 and src=90
    no logic apply, the data should just be display.
    i would like to write a query tha give me the output below.
    i tried using last_value function but i am using oracle 9i and ignore null is not supported in oracle 9i. i need to copy null if the 5th day contain null in the columns
    the final ouput for the data above should be this
    THE_ID     DT             SRC     THE_NAME     COD     PAD     TEK
    111         6/30/2009       93     A-C           DD            BAN
    111         10/1/2009       93     B-L           AA     NN      TT
    111         10/2/2009       93     C-H           BA     RR      BAN
    111         10/5/2009       93     D-R           HH     FCY      BANW
    111         10/6/2009       93     E-C           JJ     EE      JJ
    111         10/7/2009       93     F-L           DD     FIC      BAN
    111         10/8/2009       93     G-H           HG     YY      BAN
    111         10/9/2009       93     K-R           EE     FCY      BANW
    111         7/31/2009       93     G-H           EE     FCY
    111         8/3/2009       93     K-R          EE     FCY      BANW
    111         8/4/2009       93     K-R           EE     FCY      BANW
    111         8/5/2009       93     K-R           EE     FCY      BANW
    111         8/6/2009       93     K-R          EE     FCY      BANW
    111         8/7/2009       93     K-R           EE     FCY
    111         6/30/2009       22     A-C           AA     BB      CC
    111         10/1/2009       22     B-L           AA     NN      TT
    111         10/2/2009       22     C-H           BA     RR      BAN
    111         10/5/2009       22     D-R           HH     FCY      BANW
    111         10/6/2009       22     E-C           JJ     EE      JJ
    111         10/7/2009       22     F-L          DD     FIC      BAN
    111         10/8/2009       22     G-H          HG     YY      BAN
    111         10/9/2009       22     K-R           EE     FCY      BANW
    111         10/30/2009     168     T-C           EE     DD      BAN
    111         11/2/2009       168     Y-L           HH     AA      BAN
    111         11/3/2009       168     G-H           BA     WW      BAN
    111         11/4/2009       168     E-R           JJ     XX      DD
    111         11/5/2009       168     T-C           TT     JJ      WW
    111         11/6/2009       168     H-L          EE     DD      BAN
    111         11/9/2009       168     S-H          BA     WW      BAN
    111         11/10/2009     168     H-R           VV     FCY      YY
    111         11/30/2009     90     V-C           GG     FCS      BAN
    111         12/1/2009       90     B-L           QQ     GG      BAN
    111         12/2/2009       90     A-H           BA     WW      BAN
    111         12/3/2009       90     N-R           TT     JJ      TT
    111         12/7/2009       90     C-C           YY     QQ      WE
    111         12/8/2009       90     X-L           UU     JJ      BAN
    111         12/9/2009       90     V-H           BA     PP      YR
    111         12/10/2009     90     E-R           FIN2 FCY      RE
    112         2/28/2009     93       V-C           BA     PP      YR
    112         3/2/2009     93       A-S           BA     WW      TT
    112         3/3/2009     93       N-R           TT     JJ      TT
    112         3/4/2009     93       C-C           YY     QQ      WE
    112         3/5/2009     93       X-L           SS     JJ      BAN
    112         3/6/2009     93       V-H           BA     PP      YR
    112         3/9/2009     93       E-R           HH     KK      RE

    hi i am using oracle 9i 9.2.0.7.0
    you are right about the 5th business day, for 6/30/2010 it should have been 8/7/2010. 7/31/2010 is a month end so it doesnt count when you go down the list of business days. and for 7/31/2010 the 5th business day should have been 10/7/2010.
    sorry for the mistake, i got both of them backwards. so for the output, 6/30/2010 should copy from 8/7/2010 and 7/31/2010 from 10/7/2010
    THE_ID     DT             SRC     THE_NAME     COD     PAD     TEK
    111         6/30/2009       93     A-C           EE    FCY
    111         8/3/2009       93     K-R          EE     FCY      BANW
    111         8/4/2009       93     K-R           EE     FCY      BANW
    111         8/5/2009       93     K-R           EE     FCY      BANW
    111         8/6/2009       93     K-R          EE     FCY      BANW
    111         8/7/2009       93     K-R           EE     FCY
    111         7/31/2009       93     G-H           DD              BAN
    111         10/1/2009       93     B-L           AA     NN      TT
    111         10/2/2009       93     C-H           BA     RR      BAN
    111         10/5/2009       93     D-R           HH     FCY      BANW
    111         10/6/2009       93     E-C           JJ     EE      JJ
    111         10/7/2009       93     F-L           DD             BAN
    111         10/8/2009       93     G-H           HG     YY      BAN
    111         10/9/2009       93     K-R           EE     FCY      BANWEdited by: elmasduro on May 10, 2010 11:53 AM

  • Newbie blues: Oracle Sql Developer showing different data from Sqlplus.

    Newbie blues: Oracle Sql Developer showing different data from Sqlplus.
    First of all; it would be difficult to know less about Oracle than myself.
    I just installed a recent copy of the server and am running SQl Developer 3.2.20.10.
    I made a small test table , borrowed some pl sql code to add rows, then wrote code to delete some rows. Lots of fun.
    In SQL Developer “Select * from t1;” now shows 7 records, which seems right.
    I wanted other views of the data and so logged in from the command line using SQLPlus.
    The same code here shows the table prior to the delete, with 600 plus rows. Needless to say I’ve logged out and back in again, with no changes.
    No fun ! What am I not understanding here ?

    5e33d18c-3b32-48d4-82a7-676feeaa97c9 wrote:
    Newbie blues: Oracle Sql Developer showing different data from Sqlplus.
    First of all; it would be difficult to know less about Oracle than myself.
    I just installed a recent copy of the server and am running SQl Developer 3.2.20.10.
    I made a small test table , borrowed some pl sql code to add rows, then wrote code to delete some rows. Lots of fun.
    In SQL Developer “Select * from t1;” now shows 7 records, which seems right.
    I wanted other views of the data and so logged in from the command line using SQLPlus.
    The same code here shows the table prior to the delete, with 600 plus rows. Needless to say I’ve logged out and back in again, with no changes.
    No fun ! What am I not understanding here ?
    Others sessions never see uncommitted data
    > then wrote code to delete some rows. Lots of fun.
    did you ever actually issue COMMIT so other session can see the changed data?

  • Attribute value of previous row

    Hi
    I have a read only VO.
    I need attribute value of previous row .
    how can i get it ?
    consider these 3 attributes and their values :
    Id ** add ** sum
    1 *** 10 *** 10 -- > (value of previous sum + new value of add)
    2 *** 20 *** 10 + 20 = 30 -- > (value of previous sum + new value of add)
    3 *** 30 *** 30 + 30 = 60 -- > (value of previous sum + new value of add)
    now , i write a method in impl and call this method in page def by invoke action .
    i used Row[] rows = this.getViewObject().getAllRowsInRange(); in my method.
    but i am looking for better solution.

    Hi,
    If you wanted to do a running total like that, you could certainly use analytics to do that in the SQL statement itself (provided you're using an Oracle database). use SUM() over ()...
    John

  • Concatenate a column value across multiple rows - PDW

    We are using PDW based on SQL2014. We require an efficient logic on how to concatenate a column value across multiple rows. We have the following table
    T1
    (CompanyID, StateCD)
    Having following rows:
    1              NY
    1              NJ
    1              CT
    2              MA
    2              NJ
    2              VA
    3              FL
    3              CA
    We need a code snippet which will return following result set:
    1                    
    CT,NJ,NY
    2                    
    MA,NJ,VA
    3                    
    CA,FL
    We have tried built-in function STUFF with FOR XML PATH clause and it is not supported in PDW. So, we need a fast alternative.

    Hi Try this:
    SELECT * INTO #ABC
    FROM 
    SELECT 1 AS ID,'NY' AS NAME
    UNION 
    SELECT 1 AS ID,'NJ' AS NAME
    UNION 
    SELECT 1 AS ID,'CT' AS NAME
    UNION 
    SELECT 2 AS ID,'MA' AS NAME
    UNION 
    SELECT 2 AS ID,'NJ' AS NAME
    UNION 
    SELECT 2 AS ID,'VA' AS NAME
    UNION 
    SELECT 3 AS ID,'FL' AS NAME
    UNION 
    SELECT 3 AS ID,'CA' AS NAME
    )A
    CREATE TABLE ##CDB (ID INT, NAME NVARCHAR(800)) 
    DECLARE @TMP VARCHAR(MAX), 
            @V_MIN INT,
    @V_MAX INT,
    @V_COUNT INT
    SELECT @V_MIN=MIN(ID),@V_MAX=MAX(ID) FROM #ABC 
    SET @V_COUNT=@V_MIN
    WHILE @V_COUNT<=@V_MAX
    BEGIN
    SET @TMP = '' SELECT @TMP = @TMP + CONVERT(VARCHAR,NAME) + ', ' FROM #ABC 
    WHERE ID=@V_COUNT
    INSERT INTO ##CDB (ID, NAME) SELECT @V_COUNT AS ID ,CAST(SUBSTRING(@TMP, 0, LEN(@TMP)) AS VARCHAR(8000)) AS NAME 
    SET @V_COUNT=@V_COUNT+1
    END
    SELECT * FROM ##CDB
    OR
    SELECT * INTO #ABC
    FROM 
    SELECT 1 AS ID,'NY' AS NAME
    UNION 
    SELECT 1 AS ID,'NJ' AS NAME
    UNION 
    SELECT 1 AS ID,'CT' AS NAME
    UNION 
    SELECT 2 AS ID,'MA' AS NAME
    UNION 
    SELECT 2 AS ID,'NJ' AS NAME
    UNION 
    SELECT 2 AS ID,'VA' AS NAME
    UNION 
    SELECT 3 AS ID,'FL' AS NAME
    UNION 
    SELECT 3 AS ID,'CA' AS NAME
    UNION 
    SELECT 5 AS ID,'LG' AS NAME
    UNION 
    SELECT 5 AS ID,'AP' AS NAME
    )A
    CREATE TABLE ##CDB (ID INT, NAME NVARCHAR(800)) 
    DECLARE @TMP VARCHAR(MAX), 
            @V_MIN INT,
    @V_MAX INT,
    @V_COUNT INT
    SELECT @V_MIN=MIN(ID),@V_MAX=MAX(ID) FROM #ABC 
    SET @V_COUNT=@V_MIN
    WHILE @V_COUNT<=@V_MAX
    BEGIN
    SET @TMP = '' SELECT @TMP = @TMP + CONVERT(VARCHAR,NAME) + ', ' FROM #ABC 
    WHERE ID=@V_COUNT
    SELECT @V_COUNT AS ID ,CAST(SUBSTRING(@TMP, 0, LEN(@TMP)) AS VARCHAR(8000)) AS NAME INTO #TEMP 
    INSERT INTO ##CDB (ID, NAME) SELECT ID, NAME FROM #TEMP WHERE NAME<>''
    DROP TABLE #TEMP
    SET @V_COUNT=@V_COUNT+1
    END
    SELECT * FROM ##CDB
    Thanks Shiven:) If Answer is Helpful, Please Vote

Maybe you are looking for