What do I have to modify on below query to keep same order?

here is my original post
http://social.msdn.microsoft.com/Forums/sqlserver/en-US/1edac5ae-8c61-4dcf-9a15-b01df5cad617/how-to-remove-next-and-previous-record-if-amount-0?forum=transactsql#982b368a-bc6a-461a-9be2-37b3a7331dd8
if I add an ID column to assign row order, what do I need to modify in the below query to keep that order
CREATE TABLE #Temp(
[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,
[SHCRTT] [numeric](6, 0) NOT NULL,
[CBLNAM] [char](30) NOT NULL,
Query
; With cte As
(Select *, 
     Row_Number() Over(Partition By SHCOMP, CBLNAM, SHDESC, SHAMT Order By ID) As rn, Abs(SHAMT) As AbsAmt
From #Temp) 
Select c1.* into #t1
From cte c1
Left Join cte c2 On c1.SHCOMP= c2.SHCOMP
  And c1.CBLNAM = c2.CBLNAM 
  And c1.SHDESC = c2.SHDESC
  And c1.SHAMT = -1 * c2.SHAMT
  And c1.rn = c2.rn
Where c2.SHCOMP Is Null
order by ID
Original data
SHCOMP     CBLNAM       SHDESC    SHAMT     SHTYPE    ID
123         cust1       desc1      45          F       1
123         cust1       desc1     -45          T       2
123         cust1       desc1      45         F        3
123         cust1       desc1     -45        T         4
123         cust1       desc1      45         F        5
123         cust1       desc1      -35         T       6
234         cust3       desc2     -60          F       7
234         cust3       desc2      60          T       8
234         cust3       desc2      30          F       9
234         cust3       desc2     -30         T        10
234         cust3       desc2      30         F        11
Results I want
SHCOMP      CBLNAM      SHDESC    SHAMT      SHTYPE     ID
123         cust1       desc1      45         F         5   
123         cust1       desc1      -35         T        6
234         cust3       desc2      30         F         11
That query is changing my ID, instead of keep 5,6,11 it is assigning other numbers. For example
SHCOMP      CBLNAM      SHDESC    SHAMT      SHTYPE     ID
123         cust1       desc1      -35         T        4
123         cust1       desc1      45         F         7 
234         cust3       desc2      30         F         9

Declare @Sample Table(ID int identity, SHCOMP int, CBLNAM varchar(30), SHDESC varchar(10), SHAMT int, SHTYPE char(1));
Insert @Sample(SHCOMP, CBLNAM, SHDESC, SHAMT, SHTYPE) Values
(123 ,'cust1', 'desc1', 45, 'F'),
(123 ,'cust1', 'desc1', -45, 'T'),
(123 ,'cust1', 'desc1', 45, 'F'),
(123 ,'cust1', 'desc1', -45, 'T'),
(123 ,'cust1', 'desc1', 45, 'F'),
(123 ,'cust1', 'desc1', -35, 'T'),
(234 ,'cust3', 'desc2', -60, 'F'),
(234 ,'cust3', 'desc2', 60, 'T'),
(234 ,'cust3', 'desc2', 30, 'F'),
(234 ,'cust3', 'desc2', -30, 'T'),
(234 ,'cust3', 'desc2', -30, 'F');
; With cte As
(Select SHCOMP, CBLNAM, SHDESC, SHAMT, SHTYPE, ID,
Row_Number() Over(Partition By SHCOMP, CBLNAM, SHDESC, SHAMT Order By SHAMT, ID) As rn
From @Sample)
Select c1.SHCOMP, c1.CBLNAM, c1.SHDESC, c1.SHAMT, c1.SHTYPE, c1.ID
From cte c1
Left Join cte c2 On c1.SHCOMP = c2.SHCOMP
And c1.CBLNAM = c2.CBLNAM
And c1.SHDESC = c2.SHDESC
And c1.SHAMT = -1 * c2.SHAMT
And c1.rn = c2.rn
Where c2.SHCOMP Is Null
Order By c1.ID;
Tom

Similar Messages

  • HT4972 How can i download a folder of photos from my computer and have them appear on my ipad in the same order as they were in on my computer

    How can I download a photo folder from my computer and have them appear on my ipad in the same order as they were on my computer

    The sort order on iOS 5+ is by the photos 'date modified' field, on iOS 4 it's the 'date taken' - so you will need to modify that field on your photos.
    Or you could consider using a third-party photo management app such as Photo Manager Pro which allows you to change the sort order within the app e.g. by date, size, type or you can drag them into your own sort order.

  • Who knows what Can I do to modify the privileges on alloc users in order to it cannot delete the allocations created?

    Hi everybody.
    I need to know how I can check the privilege that an alloc user has to delete, modify or insert an allocation.
    I don´t know what are the security tables associated to the application user.
    I'll appreciate a lot the help that everybody can provide me.
    Regards,
    Jean

    KAS184 wrote:
    I have the new iPad so I can't shut it off with the Sleep/Wake button which it doesn't have.
    Ah ... yes ..... it does have a power button. Every iPad has the same sleep/power button in the same place on the iPad. I'm happy that you found a solution .... but maybe you should download this for reference.
    http://manuals.info.apple.com/en/ipad_user_guide.pdf

  • Hello. I'm already registered in iTunes and want to have a second iPod (with a separate email-adress) being capable of iMessage. What do I have to do?

    Hello. I'm already registered in iTunes and want to have a second iPod (with a separate email-adress) being capable of iMessage. What do I have to do?

    You can use the same iTunes account on both iPod.  To allow Messags betwen the two, one one iPod go to Settings>Messages>Receive At and add another email address and delete the iTunes account email address.
    If that is not what you want plase explain better what you want to obtain.

  • [SOLVED] See what files have been modified from original package?

    Is there a pacman command (or any command) that will tell me what files in the whole system have been modified from their original packaged state?
    EDIT 8/31/2013 2:17pm: pacman now has the -Qk and -Qkk options to see files that have been modified, removed, touched, chmodded etc. This works only for packages that contain a valid mtree file. Not all packages contain an mtree file, but efforts by package maintainers are being made to include these in packages. Read more in `man pacman`.
    Last edited by trusktr (2013-08-31 21:17:54)

    There is no native pacman (or related) command, but I made it an habit of mine (coming from Slackware) to create backups of config files with a certain extension before modifying them.
    Mostly they are tracked through pacman anyway (backup files), so whenever there's an update that creates a .pacnew file (see the section "handling config files" in the pacman manpage), I have a /etc/file.conf_orig to compare to.
    I also try to keep my modifications as minimal as possible or to post feature requests on the bugtracker to get the maintainer to follow the upstream more closely.
    That way you could see 1) your modifications with a simple diff between current config and _orig config and 2) what has changed in the update by diffing conf.pacnew and _orig.

  • When I try to upgrade, it tells me all that is on my iPad 1 is going to be erased. How to I upgrade and keep and backup what I already have. Thanks for any info.

    When I try to upgrade, it tells me all that is on my iPad 1 is going to be erased.
    How to I upgrade and keep and backup what I already have.
    Thanks for any info.
    Shirley

    Hello, alaute. 
    Thank you for visiting Apple Support Communities.
    If you are experiencing issues activating your device, see the article below.
    iPad: Troubleshooting activation issues
    http://support.apple.com/kb/ts3424
    Cheers,
    Jason H.

  • Configuration error:16, what do i have to do? I cant lunch the adobe indesign?

    configuration error:16, what do i have to do? I cant lunch the adobe indesign?

    Hi Inrow,
    Please try the steps mentioned in the link below and then try to launch the program.
    Configuration error 16 | CC, CS
    Regards,
    Anand

  • What do I have to do to order a photo book from Portugal? There is no photo service in Portugal!!!

    I'm living in Portugal and I have created a photo book with iPhoto 11 with all the instructions in Portuguese. When I was about to order it, I couln't finish my order because there no photo service in Portugal. I'm very disappointed !! There is any possibility to order it from Spain? What do I have to do?

    I have requested a moderator move your thread from the MacBook Pro forum.   There is a separate iPhoto forum.  This is a user to user forum, thus no one can really guess Apple's policy here.   It may depend entirely on the internet provider's location that you are connected to, or it may depend on the iTunes Store settings or Mac App Store settings.  There is this support page for ordering albums.  One for the Spanish store, and one for the Portuguese store below:
    http://store.apple.com/es/help/print_products
    http://store.apple.com/pt/help/print_products

  • Powershell script to set custom attribute on mailuser returns WARNING: The command completed successfully but no settings of user have been modified.

    I am trying to write a script to enable a mailuser (I do know the difference between mailuser and mailbox) and set a custom attribute for that mailuser.  Every time I run the script I get "WARNING: The command completed successfully but no settings
    of <User DN> have been modified."  Both commands being invoked work when typed manually into the exchange management shell on the exchange server itself.  I am using the same administrator account in the script, and when I login to the
    exchange server to manually run the commands, so it shouldn't be a permission issue.  Here is my script so far.  If anyone can shed some light on what I'm doing wrong, I'd appreciate it.
    $excel = new-object -com excel.application
    $wb = $excel.workbooks.open("c:\temp\testmail8.xlsx")
    $ws = $wb.Worksheets.Item(1)
    $row = 1
    $s = New-PSSession -ConfigurationName microsoft.exchange -ConnectionUri http://<Exchange Server Name>/powershell -Credential [email protected]
    Do {
    $Email = $ws.Cells.Item($row, 1).Value()
    $Cat = $ws.Cells.Item($row, 2).Value()
    invoke-command -Session $s -ScriptBlock {Enable-MailUser -ExternalEmailAddress $($args[0][0] + "@domain.com") -Identity $($args[0][0])} -ArgumentList (,$Email, $Cat)
    invoke-command -Session $s -ScriptBlock {Set-MailUser -Identity $($args[0][0]) -CustomAttribute1 $($args[0][1])} -ArgumentList (,$Email, $Cat)
    $row++
         } While ($ws.Cells.Item($row,1).Value() -ne $null)
    $excel.quit    
    Exit-PSSession

    Hi,
    I'm not sure where is wrong in your script. If you want to get more help about the script troubleshooting, I recommand you to ask a question in Script Center forum for more professional answers:
    http://social.technet.microsoft.com/Forums/scriptcenter/en-US/home?forum=ITCG
    As a workaround, please directly create a mail user in EAC and set related custom attribute to have a try.
    Thanks,
    Winnie Liang
    TechNet Community Support

  • InfoPath 2013: InfoPath cannot load the view. The view may have been modified to contain unsupported HTML such as frames.

    Hello Folks,
    I'm facing a strange behavior of InfoPath 2013 while using with SharePoint Designer 2013 to customize the forms. Whenever i open any SharePoint form to customize it in InfoPath 2013, it gives me error "InfoPath cannot load the view. The view may have
    been modified to contain unsupported HTML such as frames."
    Just like all other folks, i also assumed that the forms are corrupted and i need to change the extension to *.cab and blah blah blah. But, when i noticed that the error is coming with new list forms, new workflow forms as well then i tried to research on
    the issue. Surprisingly, all forms are opening on my colleague's PC but not from my PC. We're using the same server, same SharePoint Production, same SharePoint Development and similar InfoPath. Infact, our system team has prepared both of us in a similar
    way. The only difference is OS, where he is using Win 8 and mine is Win 7 Pro.
    Therefore, from last 1 week, i've to swap my PC with my colleague if i've to customize or update any form in InfoPath 2013. This is quite bizarre. Can someone let me know what's wrong with this issue?
    Regards,
    Sohaib
    Sohaib Khan

    Hi Sohaib,
    I'd like to double confirm with you that the issue only occurs to your client machine. Please log on to another machine and see if the issue is related to your account.
    If the issue is only related to your machine, I'd recommend you clear InfoPath cache and repair Office program.
    Then go to InfoPath > Options > More Options > General > Internet Options > Add SharePoint sites to your trusted sites and disbale protected mode.
    http://blogs.msdn.com/b/michael_yeager/archive/2008/07/09/infopath-cache.aspx
    http://office.microsoft.com/en-in/infopath-help/repair-office-programs-HA010357402.aspx
    Regards,
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact
    [email protected] .
    Rebecca Tu
    TechNet Community Support

  • I have a mac OS X 10.4.11 and have created a website in iweb08  ... in order to get it to my host site for it to go live I am having difficulty finding a ftp client to get me there. Can anyone suggest what I should do?

    I have a mac OS X 10.4.11 and have created a website in iweb08  ... in order to get it to my host site for it to go live I am having difficulty finding a ftp client to get me there. Can anyone suggest what I should do?

    Check if any of the below have an older version by contacting the author for your 10.4.11:
    http://www.rbrowser.com/
    http://rsug.itd.umich.edu/software/fugu
    http://cyberduck.io/
    http://panic.com/transmit/
    http://fetchsoftworks.com/
    Or use Applications -> Utilities -> Terminal to do command line FTP:
    http://www.dummies.com/how-to/content/how-to-use-ftp-from-terminal-to-transfer-m ac-files.navId-400509.html
    Upgrading to 10.6.8 should still give you mostly the same compatibility as 10.4.11. Upgrading to 10.7 and above won't.

  • RetainDeletedItemsFor - Command completed but no settings of username have been modified.

    Group,
    I'm trying to increase the retention days for deleted items on a single mailbox.  I run the command from the Technet site and it returns, "WARNING: The command completed successfully but no settings of '<user name>' have been modified."
     I verified my account has the correct permissions but it doesn't want to work.  What am I missing?
    Thank you,
    Jason

    David, His issue is not that he can change the setting even when it's not used - his issue is that even if he does change the UseDatabaseDefaults setting to False, he can't change the RetainDeletedItemdFor setting.  And you can change several
    mailbox settings that are ignored without a warning or error - another example is that you can set a mailbox size limit even when the mailbox is set to use database defaults.
    Well, Im not David, but regardless, I was referring to a *Permissions* issue when you typically receive an error, not changing a mailbox setting :)
    But, I think we are taking about the same thing. That is why I asked above:
    "By the way, once I set : -UseDatabaseRetentionDefaults $false, it worked,
    but you stated that it doesnt work for you when set to $false?"
    But I  dont want to create a lot of noise for the OP, so we can argue later over a beer :)
    Question for the OP: 
    If you run the following, does it work, Yes or no?
    set-mailbox <user> -RetainDeletedItemsFor 90:00:00:00 -RetainDeletedItemsUntilBackup $false -UseDatabaseRetentionDefaults $false
    ( Note the additional RetainDeletedItemsUntilBackup $false )
    Twitter!:
    Please Note: My Posts are provided “AS IS” without warranty of any kind, either expressed or implied.

  • I am testing a Sony a7r, shooting RAW.  I can't seem to import the files (which are .awr) into my Lightroom 4. Anybody know what options I have?  Thanks!

    I am testing a Sony a7r, shooting RAW.  I can't seem to import the files (which are .awr) into my Lightroom 4. Anybody know what options I have?  Thanks!

    Support was added by Adobe for this camera model in LR 5.3 and ACR 8.3. see the link below.
    Camera Raw plug-in | Supported cameras
    You can download the latest trial version of LR 5 the current version is 5.7.1 and use tor 30 days.
    The other option is to download the latest Adobe DNG Converter (version 8.7.1) for free and convert the Sony .arw files to dng format and they will work fine with LR 4.
    Make sure you retain the original copies of the .arw files.  

  • What does Apple have that compares to Adobe flash player ?

    What does Apple have that compares to Adobe Flash Player, I keep getting Blocked plugged message and it tells me to download Adobe Flash Player and when I do nothing happens.  Also when I purchased my iMac it came with Leopard osx system, last year I installed the Snow Leopard 10.6.3 system and since then cannot read CD's or DVD's from PC or my husbands new iMac with Mountain Lion system.

    There is no equivalent to Adobe's Flash Player. You either have it installed or not. For the reasons outlined below, Apple no longer sell Macs with Flash Player pre-installed, you have to obtain it from Adobe.
    Flash - Apple's official position:
    http://www.apple.com/hotnews/thoughts-on-flash/
    Flash v. HTML5
    http://www.streaminglearningcenter.com/articles/flash-player-cpu-hog-or-hot-tama le-it-depends-.html
    Apple may have more reasons behind not installing Flash by default other than the stated reason of ensuring that users always have the most up-to-date version. Having Flash installed can cut battery runtime considerably—as much as 33 percent in our testing. With a handful of websites loaded in Safari, Flash-based ads kept the CPU running far more than seemed necessary, and the best time I recorded with Flash installed was just 4 hours. After deleting Flash, however, the MacBook Air ran for 6:02—with the exact same set of websites reloaded in Safari, and with static ads replacing the CPU-sucking Flash versions.
    http://arstechnica.com/apple/reviews/2010/11/the-future-of-notebooks-ars-reviews -the-11-macbook-air.ars/3
    And this:
    http://www.usatoday.com/tech/columnist/kimkomando/2011-02-18-changes-web_N.htm
    You can check here:  http://www.adobe.com/products/flash/about/  to see which version you should install for your Mac and OS.
    You should first uninstall any previous version of Flash Player, using the uninstaller from here (make sure you use the correct one!):
    http://kb2.adobe.com/cps/909/cpsid_90906.html
    and also that you follow the instructions closely, such as closing ALL applications first before installing. You must also carry out a permission repair after installing anything from Adobe.

  • Muse CC thinks that ALL of my assets have been modified and wants to "convert" my site.

    I opened up my muse site and somehow Muse CC thinks that ALL of my assets have been modified and wants to "convert" my site. It makes me a bit worried.
    Here's what I've done since opening the site without a problem:
    I used the in-browser editing feature to change simple numbers on a single page - the home page - I got the regular notice to review and update - that worked with no problems.
    I have added several Muse-Themes widgets/templates/tools
    I've had a computer crash that required a hard re-boot to correct, Muse and this site were running at the time. (now that I think about it, this might be the problem)
    I don't recall whether Muse CC has updated in the last week (since it worked correctly for me)
    I run anti-malware software and virus protection - both of which run in realtime scanning internet connection, file downloads, etc.
    Can someone assure me that everything is OK - that sometimes this happens?
    My next step as I leave here is to login to my site and check file sizes to see if there's a significant difference...yes, I'm worried about viruses - I have great software but all software can be overcome by determined (and annoying) people.
    thanks for your attention to this and hopefully everything is just fine if a bit glitchy. I can work with glitchy :-)

    Thank you Akshay for replying so quickly. Lots of work to do then on my part.
    Is there a list of CC apps that have been updated to their 2014 versions, so that I can check that I have all the updates?
    Many thanks,
    David Perkins
    (Brighton, UK)

Maybe you are looking for