How do I select the items based on their adjacent equality?

In the below table,
    DECLARE @t1 TABLE(id INT,NAME VARCHAR(MAX))
    INSERT INTO @t1 VALUES(1,'test')
    INSERT INTO @t1 VALUES(2,'test')
    INSERT INTO @t1 VALUES(3,'best')
    INSERT INTO @t1 VALUES(4,'rest')
    INSERT INTO @t1 VALUES(5,'rest')
how can I select the records that have equals values coming adjacently? That is I am running an SQL job. It should take the adjacent equal value  count for its processing. In the below table, first time the job is running it should take count as 2 the
2nd time job is running it should be 1. Third time it should be 2. What is the query that will fetch the cont of equal records for name column? In case no equal columns it should be 1. Please help
mayooran99

Hi
Try the following code.
DECLARE @t1 TABLE(id INT,NAME VARCHAR(MAX))
INSERT INTO @t1 VALUES(1,'test')
INSERT INTO @t1 VALUES(2,'test')
INSERT INTO @t1 VALUES(3,'best')
INSERT INTO @t1 VALUES(4,'rest')
INSERT INTO @t1 VALUES(5,'rest')
INSERT INTO @t1 VALUES(6,'test')
INSERT INTO @t1 VALUES(7,'best')
INSERT INTO @t1 VALUES(8,'best')
INSERT INTO @t1 VALUES(9,'vest')
--ranking adjacent names, running total
SELECT rs.id, rs.NAME, ROW_NUMBER() over (partition by name, nxt_id order by id) RankingCount
FROM (
SELECT nxt.id, nxt.NAME,(SELECT min(s.id)
FROM @t1 s
WHERE s.name <> nxt.name
AND s.id > nxt.id
) as nxt_id
FROM @t1 nxt) rs
ORDER BY rs.id;
--code to just return the totals
with cteTotals
as
SELECT rs.id, rs.NAME, ROW_NUMBER() over (partition by name, nxt_id order by id) RankingCount, nxt_id, ROW_NUMBER() over (partition by name, nxt_id order by id desc) Ranking
FROM (
SELECT nxt.id, nxt.NAME,(SELECT min(s.id)
FROM @t1 s
WHERE s.name <> nxt.name
AND s.id > nxt.id
) as nxt_id
FROM @t1 nxt) rs
select Name, RankingCount
from cteTotals
WHERE Ranking=1
order by id
The first query keeps a running total of adjacent names, the second one just reports the totals

Similar Messages

  • Classyfying the customers based on their sales

    Hi,
    I have report showing the cutomers and the sales units to them
    for example:
    customer     sales
    A     200
    B     300
    C     50
    D     100
    how can i grade the customers based on their sales, for example
    Grading                customer     sales
    Gold >=300            B              300
    Sub total                   300
    silver >100 & <300    A              200
    sub total                   200
    Bronze <=100          C               50
                       D              100
    sub total                   150
    Could anyone please help me
    Message was edited by:
            rapo
    Message was edited by:
            rapo

    Thanks ganesh and ramesh,
    Thanks for your inputs.
    By exceptions, i can get classigy them and even color them and sort them but i cannot get the sub totals for each classification.
    Ramesh, can you please clarify in detail how this can be done by using the formula.
    thanks and regards

  • Firefox fails to import history, cookies, favourites etc. from opera; cookies etc. are not listed in the space where you can select the items you want to import any idea how this comes? thanks!

    In the importing operation Firefox detects Internet Explorer and Opera as installed browsers. When i select Internet Explorer cookies, history, favourites etc. are listed in a box where you can select the items you want to import in Firefox. When i select Opera no items are listed that can be imported. Any idea how this comes?

    Hi,
    I think that what you are searching be in Tools >> Options >> Privacy >> On Firefox will: change to "Use custom settings for history" then Button Show Cookies will be allowed and you will see the cookies in your browser in the next window
    If you uncheck "Accept cookies from sites", probably the sites that need cookies will alert you that you need active the storage cookies

  • HT1451 when editing info, how do i select multiple items to edit a group? For example, changing the artist name on multiple tracks at once?

    when editing info, how do i select multiple items to edit a group? For example, changing the artist name on multiple tracks at once?

    Same way you select multipl items anywhere else.
    Hold ctrl and click the items you want.
    If they are in consecutive order, click the 1st then Shift click the last.

  • How do I select multiple items on blackberry phone?

    How do I select multiple items on blackberry phone?

    Multiple items of what?
    1. If any post helps you please click the below the post(s) that helped you.
    2. Please resolve your thread by marking the post "Solution?" which solved it for you!
    3. Install free BlackBerry Protect today for backups of contacts and data.
    4. Guide to Unlocking your BlackBerry & Unlock Codes
    Join our BBM Channels (Beta)
    BlackBerry Support Forums Channel
    PIN: C0001B7B4   Display/Scan Bar Code
    Knowledge Base Updates
    PIN: C0005A9AA   Display/Scan Bar Code

  • HorizontalList - How to programatically select an item

    Hi All,
    I am new to flex and new to this forum. I am building an
    application that uses a HorizontalList and a button.
    When the button is clicked, I need to add a new item (image +
    label) on the HorizontalList, and I need to make the newly added
    item the default selection.
    I tried to set the selectedIndex property, but that does not
    work.
    Any idea how to make this work ? ie how to programatically
    select an item in the HorizontalList
    Regards
    Roni

    This code seems to do what you want:

  • HT3819 can you select the items you don't want your family to be able to see?

    I want to get some of my dad's songs he has on his ipod without buying them so i was going to try using the home sharing but i dont want to share all of my songs. Before i turn home sharing on i would like to know if i can select the items i want and don't want my family to be able to see

    I really do appreciate everyone's input to my question. I guess what I wanted to do, and how it can be done aren't as simple as I had hoped. But I know what will work now. Thank you again and have a nice holiday season.

  • How do I access the web-based utility

    I just purchased a wireless router...WRT300N. I want to access the "web-based utility" in order to update the firmware.
    How do I access the web-based utility...help!
    Thanks.

    Access the router’s web-based setup page.
    For instructions, click here.
    Linksys Easy Answer 4030, applies to WRT300N
    Upgrading the Linksys Router's Firmware
    Hope this helps,
    Lilla
    Westell 6100 modem/router; Linksys WRT54GL v1.1 router firmware 4.30.9; Linksys WPC54G v2 Notebook Card; CenturyTel DSL using PPPoE

  • The bookmarks toolbar is there but has no item buttons on it. How do I get the items back for this toolbar? Also would like to know if the toolbars can be put on the same line to make more room on the page (drag & drop) ??

    The bookmarks toolbar is there but has no item buttons on it. How do I get the items back for this toolbar?
    Also would like to know if the toolbar can be put on the same line to make more room on the page (drag & drop) ?? Like the menu & bookmarks toolbar could/should fit on same line. This would add more page view...
    Floyd Perry
    Thanks

    Check that you still have the "Bookmarks Toolbar items" placed on the Bookmarks Toolbar
    * Make sure that you have the "Bookmarks Toolbar" visible: "View > Toolbars"
    * Check in "View > Toolbars > Customize" that the "Bookmarks Toolbar items" is on the Bookmarks Toolbar
    * If the "Bookmarks Toolbar items" is not on the Bookmarks Toolbar then drag it back from the Customize window onto the Bookmarks Toolbar
    * If you do not see the "Bookmarks Toolbar items" then click the "Restore Default Set" button
    You can only move the content from a toolbar onto other toolbars if all toolbars support that feature. You need to check that in the options of each toolbar.

  • How do I select the entire text when the focus is moved to one TextBox?

    How do I select the entire text when the focus is moved to one TextBox?
    There are three TextBoxes.  I use <TextBox TabIndex="1"/> to set the order they are selected.  When I enter one specific TextBox, I would like to have the entire text selected ... one key stroke removes all of the text in that TextBox.
    bhs67

    I do it the following way:
    1.  Override the OnStartup for the application.  Use the following code: (VB.NET)
    EventManager.RegisterClassHandler(GetType(TextBox), TextBox.GotFocusEvent, New RoutedEventHandler(AddressOf TextBox_GotFocus))
    2.  In the event handler use this code (again VB.NET)
    Private Sub TextBox_GotFocus(ByVal sender As Object, ByVal e As RoutedEventArgs)
    _theTextBox = CType(sender, TextBox)
    If setFocusTimer IsNot Nothing Then
    RemoveHandler setFocusTimer.Tick, AddressOf setFocusTimer_Tick
    End If
    setFocusTimer = New DispatcherTimer
    setFocusTimer.Interval = TimeSpan.FromMilliseconds(10)
    AddHandler setFocusTimer.Tick, AddressOf setFocusTimer_Tick
    setFocusTimer.Start()
    End Sub
    Private Sub setFocusTimer_Tick(ByVal sender As Object, ByVal e As System.EventArgs)
    setFocusTimer.Stop()
    _theTextBox.SelectAll()
    End Sub
    What this does is register a handler for every textbox in your application.  This will execute when the GotFocus event is raised and will allow you to do whatever (in this case select all) you want.  A great way of handling things at the application
    level.
    Lloyd Sheen

  • I can't seem to open (view) my photos.  I get an error message that says "the file couldn't be opened because you don't have permission to view it. To view or change permission, select the item in the finder and choose File. Get info."

    I can't seem to open (view) my photos.  I get an error message that says "the file couldn't be opened because you don't have permission to view it. To view or change permission, select the item in the finder and choose File. Get info."  I have never had this happen before?  Thanks!

    Try the following:
    1 - launch iPhoto with the Command+Option keys held down and rebuild the library.
    2 - Run Option #1, Repair Permissions.
    OT

  • I'm trying to install an update to itunes that downloaded but can't complete installation as its trying to install onto D: drive. I don't have a D drive and i'm not given option of selecting the drive to install on. How can i select the drive that i want

    I'm trying to install itunes but can't complete installation as its trying to install onto F: drive. I don't have a F drive and I'm not given option of selecting the drive to install on. How can I select the drive that i want to install on?

    Hi,
    How do you connect the printer to the XP machine ? If USB, you need to make that machine as a Print server. Please try this:
       http://techtips.salon.com/make-windows-computer-pr​int-server-11914.html
    Regards.
    BH
    **Click the KUDOS thumb up on the left to say 'Thanks'**
    Make it easier for other people to find solutions by marking a Reply 'Accept as Solution' if it solves your problem.

  • I'm trying to print from a full size 30x42 pdf of an AutoCAD file.  I only want to print an 8 1/2 x 11 portion, in scale.  How do I select the window and print in scale on my 8 1/2 x 11 printer?

    I'm trying to print from a full-size, 30x42 pdf of an AutoCAD file.  I only want to print an 8 1/2 x 11 portion, in scale.  How do I select the window and print in scale on my 8 1/2 x 11 printer (Brother MFC-9340CDW)?

    You might be better off contacting an AutoCAD support group.

  • How can i select the next column instead of next row when press enter key

    I need to know how can i select the next column instead of next row when i press the enter key.By default ,when i press enter key the next row is selected and the column remain unchanged but I wants opposite that is the row should remain unchanged but column index will changed.
    Thanks to all.

    Well, the right arrow key will already move you to the next column, so the easiest way to do this is to modify the InputMap to have the Enter key invoke the same Action as the right arrow key.
    You can search the forum for my "Table Actions" (without the space) example that will show you how to do this.

  • I can not open documents I get a message that says To view or change permissions, select the item in the Finder and choose File Get Info.  help

    I have a mac with os X.  I have saved documents and photos on my computer and am now not able to open them.  I get a message that tells To view or change permissions, select the item in the Finder and choose File > Get Info.  I do that and it is not locked.  confused.  help

    In the Info window it's this:
    Do you have read and write privileges?

Maybe you are looking for