How can I modify one column of current and next record depending of some criteria?

Having DDL
CREATE TABLE #ServiceChange(
[ID] [int] identity(1,1),
[SHCOMP] [char](2) NOT NULL,
[SHCRTD] [numeric](8, 0) NOT NULL,
[SHCUST] [numeric](7, 0) NOT NULL,
[SHDESC] [char](35) NOT NULL,
[SHTYPE] [char](1) NOT NULL,
[SHAMT] [numeric](9, 2) NOT NULL,
[CBLNAM] [char](30) NOT NULL,
[GROUPID] [char](2) NULL
And original and desire data in below link
https://www.dropbox.com/sh/bpapxquaae9aa13/AADnan31ZASublDjN7sa2Vvza
I would like to know how can I modify one column of current and next record depending of some criteria using SQL2012?
The criteria is:
Type should always flow F->T
if current abs(amount)> next abs(amount) then groupid = 'PD'
if current abs(amount)< next abs(amount) then groupid = 'PI'
there is no case when those amounts will be equals
where current(custid) = next(custid) and current(service) = next(service) and groupid is null
Any help will be really apreciated.
Thank you

I tried that and got this error
'LAG' is not a recognized built-in function name.
You said you were using SQL 2012, but apparently you are not. The LAG function was added in SQL 2012. This solution works on SQL 2005 and SQL 2008:
; WITH numbering AS (
   SELECT groupid,
          rowno = row_number()  OVER (PARTITION BY custid, service ORDER BY date, id)
   FROM   #ServiceChange
), CTE AS (
   SELECT a.groupid,
          CASE WHEN abs(a.amount) < abs(b.amount) THEN 'PD'
               WHEN abs(a.amount) > abs(b.amount) THEN 'PI'
          END AS newgroupid
   FROM  numbering a
   JOIN  numbering b ON b.custid  = a.custid
                    AND b.service = a.service
                    AND b.rowno   = a.rowno - 1
UPDATE CTE
SET   groupid = newgroupid
Erland Sommarskog, SQL Server MVP, [email protected]

Similar Messages

  • Pages:  How can I sort one column of words and not have it affect the other columns?

    How can I sort one column of words and not have it affect the other columns?  I have opened the inspector to the edit columns and rows under Table.  It will sort the column, but then it changes the other colums as well.  I know that if I use Numbers, it will work, but I want to know how to do the same thing in Pages.

    Hi Peter,
    Numbers sorts full rows on values in selected column(s). The technique for sorting a single column is essentially the same as Jerry is describing for Pages tables—separate the (data in the) column to be sorted, sort it, return it to the table.
    In Numbers the actual column may be separated from the original table, sorted, then returned. In Pages, the data must be extracted, sorted, then pasted back in, overwriting the unsorted data (if it was left in the original table).
    iWork tables follow a database model in which each row is a Record, and each column holds a Field within the records.
    As evidenced by the current question (and several similar questions arising in the Numbers community) that model doesn't apply to the way some users, especially some who come in from the MS Excel world, use tables.
    With Excels model—islands of data on a single large table, the ability to sort one or a selected few columns of data makes sense. One 'island' may comprise only cells AA21:AH50. Sorting that small 'table' should be possible without disturbing the rest of rows 21-30, which are probably part of one or more other 'data islands' in the sea that is a MS Excel spreadsheet.
    In Numbers, each of those 'islands' would be a separate Table, and that Table would be sortable without disturbing other Tables in the document.
    Regards,
    Barry

  • How can I separate one column into multiple column?

    How can I separate one column into multiple column?
    This is what I have:
    BUYER_ID ATTRIBUTE_NAME ATTRIBUTE_VALUE
    0001 PHONE_NUMBER 555-555-0001
    0001 EMAIL [email protected]
    0001 CURRENCY USD
    0002 PHONE_NUMBER 555-555-0002
    0002 EMAIL [email protected]
    0002 CURRENCY USD
    0003 PHONE_NUMBER 555-555-0003
    0003 EMAIL [email protected]
    0003 CURRENCY CAD
    This is what I would like to have:
    BUYER_ID PHONE_NUMBER EMAIL CURRENCY
    0001 555-555-0001 [email protected] USD
    0002 555-555-0002 [email protected] USD
    0003 555-555-0003 [email protected] CAD
    Any help would be greatly appreciated.

    This is another solution. Suppose your actual table's name is test(which has the redundant data). create a table like this:
    CREATE TABLE test2 (BUYER_ID number(10),PHONE_NUMBER varchar2(50),EMAIL varchar2(50),CURRENCY varchar2(50));
    then you will type this procedure:
    declare
    phone_number_v varchar2(50);
    EMAIL_v varchar2(50);
    CURRENCY_v varchar2(50);
    cursor my_test is select * from test;
    begin
    for my_test_curs in my_test loop
    select ATTRIBUTE_VALUE INTO phone_number_v from test
    where person_id=my_test_curs.person_id
    and attribute_name ='PHONE_NUMBER';
    select ATTRIBUTE_VALUE INTO EMAIL_v from test
    where person_id=my_test_curs.person_id
    and attribute_name ='EMAIL';
    select ATTRIBUTE_VALUE INTO CURRENCY_v from test
    where person_id=my_test_curs.person_id
    and attribute_name ='CURRENCY';
    INSERT INTO test2
    VALUES (my_test_curs.person_id,phone_number_v,EMAIL_v,CURRENCY_v);
    END LOOP;
    END;
    Then you will create your final table like this:
    create table final_table as select * from test2 where 1=2;
    After that write this code:
    INSERT ALL
    into final_table
    SELECT DISTINCT(BUYER_ID),PHONE_NUMBER,EMAIL,CURRENCY
    FROM TEST2;
    If you have a huge amount of data in your original table this solution may take a long time to do what you need.

  • HT4436 how can i setup one account for me and my kids ?

    how can i setup one account for me and my kids ?

    One account each, or one account for all of you to share?
    Either way, follow the instructions here: http://www.apple.com/icloud/setup/
    Remember, if you share an iCloud account, you won't be able to maintain independent Contact lists, calendars, bookmarks etc. They will all be synced to all devices setup using the same account.

  • How can I have one "family" Apple ID and two icloud accounts one for me and one for the wife?

    MacVirgin!Just got new Air and iMac (iphones on the way). How can I have one Apple ID but two iCloud accounts (one for me and one for my wife)? I have set up one iCloud account and am attempting to share contacts between iMac, iCloud and Air, however iMac and iCloud are not in sync? I must be missing something very simple.

    ok. I set up the imac and Air with one user account. this was used to activae iCloud on both machines and I have now se up one iCloud account and managed to share calander and contacts between the two. Ultimately both me and my wife will have an iphone and we wish to share calenders and contacts. We both also use the imac and air on one user account. It looks like I need to set another user account then activate iCloud in my wifes Apple ID

  • How can I modify ONE CHARACTER in the file name???

    I have in right side meta tag panel a file name , original name and copy name fields.
    The file was already renamed. So i am not interested original file name ,anyway i cant click on it.
    I would like to modify ONE +"!/"%/"+! CHARACTER in filename field which is 50 characters long.
    If I click on it gives me an batch rename window.
    If I click copy name I have to write all from zero BECAUSE I CANT COPY the filename field.
    so here we are in 21st century
    I have a solution: i copy the filename from vista explorer and paste it in copy name BUT what if I have to do this hundreds time???

    First, what has the copy name to do with file name? I think you're misunderstanding the purpose of the copy name field. It is for giving a virtual copy a distinctive name.
    Now for file naming... if I understood correctly, you want to edit the name right in the Metadata panel, but the file name field is non-editable. This is a bug. It is exhibited when you have multiple files selected and change the metadata panel view. You can temporarily fix it by selecting a single file in the grid and changing the metadata panel view from All to Default or vice versa (see the attached screenshot). Now that the field became editable, you change on character.
    If you want to do it for hundreds of files using a template, then would be nice if you provide your source and target filename templates.

  • How can pass any one column value

    hi all,
    i have table and i am quering that table like
    select d_type,d_number,d_date,draccount,craccount
    from d_table
    where d_date = :parameter.d_date
    and draccount or craccount = :parameter.code--here user may enter any code which may belongs to draccount or craccount how can check that in my query?Please help me..
    Thanks..

    Hi,
    A slight variation on Tubby's solution:
    select  d_type
    ,     d_number
    ,     d_date
    ,     draccount
    ,     craccount
    from      d_table
    where      d_date           = :parameter.d_date
    and      :parameter.code IN (draccount, craccount)
    user13329002 wrote:hi,
    even i was trying the same. what happen if the parameter value present in both the columns?
    both column's records will be displayed right..What happens when you try it?
    If that's what you want, what's the problem?
    If that's not what you want, what's the problem? Post a little sample data (CREATE TABLE and INSERT statements) and the results you want from that data.
    Do you want to use the condition :parameter_code = craccount only when the other condition, :parameter_code = draccount, produces no results? If so, use a CASE expression (or the DENSE_RANK function).

  • How can I modify a column to a primary key column

    Hi,
    I've created a table with no primary key. How I can set the primary key column now? The datatype of the "primary key" column is Raw(35).
    Thanks,
    Mike

    Mike,
    Issue the following command:
    ALTER TABLE table_name
    ADD CONSTRAINT constraint_name PRIMARY KEY(column_name);
    NOTE: If you want to place the primary key data in a separate tablespace, then you can add the key word USING INDEX TABLESPACE after the column_name.
    Regards,
    John

  • How can I Make one column into Multiple columns

    Id
    SCM-01-015
    SCM-01-020
    SFA-02-021
    SFC-02-042
    STB-03-035
    STP-04-167
    SVF-06-150
    I want the output like this
    Id 01 02 03 04 06
    SCM-01-015 5
    SCM-01-020 10
    SFA-02-021 15
    SFC-02-042 15
    STB-03-035 20
    STP-04-167 50
    SVF-06-150 5
    thank you in advance

    Hi,
    801668 wrote:
    Id
    SCM-01-015
    SCM-01-020
    SFA-02-021
    SFC-02-042
    STB-03-035
    STP-04-167
    SVF-06-150
    I want the output like this
    Id 01 02 03 04 06
    SCM-01-015 5
    SCM-01-020 10
    SFA-02-021 15
    SFC-02-042 15
    STB-03-035 20
    STP-04-167 50
    SVF-06-150 5
    thank you in advanceWhenever you post formatted text (such as query output) on this site, type these 6 characters:
    \(small letters only, inside curly brackets) before and after formatted text, to preserve spacing.
    Do you want output something like this, which is from the scott.emp table?ENAME ANALYST CLERK MANAGER PRESIDENT SALESMAN
    ALLEN 1600
    JONES 2975
    FORD 3000
    CLARK 2450
    MILLER 1300
    SMITH 800
    WARD 1250
    MARTIN 1250
    SCOTT 3000
    TURNER 1500
    ADAMS 1100
    BLAKE 2850
    KING 5000
    JAMES 950
    (ALLEN is a SALESMAN, JONES is a MANAGER, ..., JAMES is a CLERK.)
    If so, do a pivot, somehting like this:SELECT     ename
    ,     MIN (CASE WHEN job = 'ANALYST' THEN sal END)     AS analyst
    ,     MIN (CASE WHEN job = 'CLERK' THEN sal END)     AS clerk
    ,     MIN (CASE WHEN job = 'MANAGER' THEN sal END)     AS manager
    ,     MIN (CASE WHEN job = 'PRESIDENT' THEN sal END)     AS president
    ,     MIN (CASE WHEN job = 'SALESMAN' THEN sal END)     AS salesman
    FROM     scott.emp
    GROUP BY ename
    Always say which version of Oracle you're using.
    The query above will work in Oracle 8.1 or higher, but starting in Oracle 11 there's a better way:  SELECT ... PIVOT.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • How can we have a column having text and radio button in the same line?

    Hi,
    I have a column which needs to have data with a radio button. Was able to achieve this by using <trh:tableLayout>. But when i select the radio button it shifts to the next line. Want to stop this behavior of radio button of shifting to the next line. This column also includes a hyperlink.So the text and hyperlink are made visible invisible based on radio button action.
    Adding one more doubt. Can a particular columns horizontalgrid can be made invisible?
    Any help would be appreciated.
    sample code:
    <trh:cellFormat id="cf8" halign="start">
    <af:activeOutputText value="#{row.Actions}" id="aot2"/>
    <af:goLink text="#{row.Actions}" id="gl1"
    clientComponent="true" visible="false"
    targetFrame="_blank" destination="http:// + #{row.BUTTON_URL}">
    </af:goLink>
    <af:selectBooleanRadio text="" id="sbr1">
    <af:clientListener method="selectCheckBox" type="click"/>
    </af:selectBooleanRadio>
    </trh:cellFormat>
    Thanks,
    Nita

    Remove the <trh:> components. Put the other components as direct children of the PanelGroupLayout:
    <af:column id="c2" headerText="Actions">
      <af:panelGroupLayout id="pgl6" layout="horizontal">
        <af:activeOutputText value="#{row.Actions}" id="aot2"/>
        <af:goLink text="#{row.Actions}" id="gl1"
                   clientComponent="true" visible="false"
                   targetFrame="_blank" destination="http:// + #{row.BUTTON_URL}">
        </af:goLink>
        <af:selectBooleanRadio text="" id="sbr1">
          <af:clientListener method="selectCheckBox" type="click"/>
        </af:selectBooleanRadio>
      </af:panelGroupLayout>
    </af:column>If you need to add some horizontan space between the components, you can put <af:spacer>(s) between the components.
    Dimitar

  • How can i modify or delete desktop files and folders in Mac OS 10.6.8 ?

    i have Macbook pro running on Mac OS X, Snow Leopard ver. 10.6.8 . I am not able to delete rename and modify my desktop folder and files. even through the admin account and all previleges i cant move them to Thrash. as i try to delete them it asks for password and then no action takes place. i have even used repair permissions from disk utility, used the terminal window method but nothing happened. the "get info " shows read and write permision but still i cannot even rename them there.. can anyone please help me ?

    Is the Desktop folder inside the Users -> Yourname folder with the same contents of your Desktop?  If it isn't, then you've moved the Desktop folder to a place that can't be accessed by the account called Yourname.

  • My current Itunes store is not showing some artists albums. How can I fix this?, My current Itunes store is not showing some artists albums. How can I fix this?

    I am trying to download some new music and when I search for them in Itunes they have no albums listed. Is any one else having this issue??

    You should be able to check if it's a new account by viewing its purchase history e.g. via the Store > View Account menu option on your computer's iTunes or on your iPad you can see a cut-down version via http://reportaproblem.apple.com (which optionally allows you to contact Support if you have a problem with a purchase). If it's empty then that implies that it's a new account.
    If it is a new account then you could try getting access to the old account via http://iforgot.apple.com , and if that doesn't work then try contacting Support in your country : http://support.apple.com/kb/HT5699
    If/when you get access to the account then you can update the primary email address on it to something that you do have access to (you won't be able to use an email address that is being used on another account) either via http://appleid.apple.com, the Store > View Account menu option on your computer's iTunes or by tapping on it in Settings > iTunes & App Store on your iPad

  • How can i put one part in front and one part in the back?

    Alright i made this phone and a person and i want to put the bottom in front of his chin and the top behind the ear. How should i do this? Couldnt find anything in my classroom in a book, book.
    thanks

    The part in front and the part in back have to be two separate objects. Put a rectangle to completely cover the bottom part of the phone and let it mask the whole phone. Copy the clipping group (or duplicate its layer in the layers panel); on the copy, direct-select the end of the mask rectangle that doesn't bisect the phone and drag it the other way, so the rectangle now covers (and crops) the top half of the phone. If you do this on layers you need only make sure the phone-bottom is on the top layer, phone-top is on the bottom layer, and the person layer is between them.

  • How can you modify the displayed columns on a Related Information List?

    How can you modify the displayed columns on a Related Information List? For example, how could you add the "Type" column to the List of columns displayed for Service Requests when you are viewing the Contacts Detail page?
    Thanks

    I'd have to say I think this is one of the biggest flaws in the OnDemand system currently. The solution I have come up with is to create reports and put them in webapplets showing the data I want to show. I have then removed the standard Related Info List Objects and added weblinks to create new records as the button on the List object is also gone.
    Keep in mind that doing this does slow things down a little, so it may not work if you have a big user base.
    RWB.

  • How can we modify the maximum no. of columns in pivot table ?

    hi all,
    How can we modify the maximum no. of columns in pivot table ?
    do i need to change the nqconfig.ini or instanceconfig file or else?
    thnx..

    A little search on the forum :
    In the instanceconfig.xml add a <PivotView> element under <ServerInstance> if one does
    not exist already.
    Within the <PivotView> element add an entry that looks like:
    <MaxCells>nnnnnn</MaxCells>
    where nnnnnn is your desired limit for the maximum total number of cells allowed
    in a pivot.
    Warning: an excessively large number will cause more memory consumption and
    slower browser performance.The details here :
    Oracle BI EE (10.1.3.2): Maximum total number of cells in Pivot Table excee

Maybe you are looking for

  • How to reinstall Safari on Yosemite?

    Can I reinstall Safari without having to install the entire Yosemite OS?  My Safari suddenly started crashing this morning.

  • How can I get a word count in iCloud Pages Beta?

    Apparently there is no way to get a document word count in iCloud Pages Beta.  Has anyone figured out a work around for this?

  • Network Connection Timing Out

    When I download TV shows, I regularly get a message saying the Network Connection has Timed Out. Err: -3259. This will happen during a relatively quick download (with no interruption it takes about 25 mins), so I don't think it's the firewall. I keep

  • SSRS with SharePoint 2010 Managed Metadata or Lookup Values - Display

    When creating an SSRS (SQL Server 2008 R2) report from SharePoint 2010 list/library data using BIDS: A Lookup field or Managed Metadata field displays in the format "1;#value" rather then just the "value". SharePoint columns that have Rich Text displ

  • Issue with WIP_MASSLOAD_PUB.createOneJob, R12.

    Hello, For one of the custom program, we have been using WIP_MASSLOAD_PUB.createOneJob to create a WIP Job. The custom program has been designed more on the same lines(almost exactly) as mentioned in http://apps2fusion.com/forums/viewtopic.php?f=3&t=