Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 446591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:29:10+00:00 2026-05-12T21:29:10+00:00

We’re required to add Ordering to our EventVenue table, which we’ve done by adding

  • 0

We’re required to add Ordering to our EventVenue table, which we’ve done by adding an int field to store the EventVenue’s placing in the order. The ordering is based upon the parent Event’s ID and the Event’s VenueGrouping.

If the VenueGrouping is false the EventVenues will be ordered sequentially. However, if true, the EventVenues for the same Event and derived from the same parent Event will have the same ordering. For example [assuming we’ve filtered by Event]:

EVID | VenueID | Not Grouped | Grouped
  1  |    1    |      1      |   1
  2  |    2    |      2      |   2
  3  |    2    |      3      |   2
  4  |    3    |      4      |   3

The problem is when the grouping is changed. I need some SQL to update the Event’s Venue Ordering appropriately but am finding it tricky to get my head round it, especially when going from ungrouped to grouped without leaving gaps.

I’d presume it needs to be in an if statement like below, but if a more efficient/DRY solution is suitable, all the better:

-- Changing from Ungrouped to Grouped
IF @OldGrouping=0 AND @NewGrouping=1
   BEGIN
      UPDATE EventVenues SET Ordering=...
   END
-- Changing from Grouped to Ungrouped
ELSE IF @OldGrouping=1 AND @NewGrouping=0
   BEGIN
      UPDATE EventVenues SET Ordering=...
   END

Any help appreciated.

As requested, more example data:
Got annoyed at formatting so used a key for headers:
A = EventVenueID, B = VenueID, C = Original Ordering, D = Updated Ordering

Updating from Ungrouped to Grouped

 A  | B | C | D
101 | 1 | 4 | 3
102 | 2 | 2 | 2
103 | 2 | 3 | 2
104 | 3 | 1 | 1

Updating from Grouped to Ungrouped

 A  | B | C | D
101 | 1 | 3 | 4
102 | 2 | 2 | 2
103 | 2 | 2 | 3
104 | 3 | 1 | 1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T21:29:10+00:00Added an answer on May 12, 2026 at 9:29 pm
    WITH    rows AS
            (
            SELECT  e.*,
                    ROW_NUMBER() OVER (ORDER BY evid) AS rn,
                    DENSE_RANK() OVER (ORDER BY venueid) AS dr
            FROM    EventVenues e
            )
    UPDATE  rows
    SET     Ungrouped = rn,
            Grouped = dr
    

    Update:

    If I understand correctly, Ordering is one column which you update depending on some external parameter (not stored in the DB).

    To update from ungrouped to grouped:

    WITH    rows AS
            (
            SELECT  e.*,
                    DENSE_RANK() OVER (ORDER BY VenueID DESC) AS dr
            FROM    EventVenues e
            )
    UPDATE  rows
    SET     Ordering = dr
    

    This will update the Ordering in descending VenueID order, assigning same orders to same VenueID‘s

    To update from grouped to ungrouped:

    WITH    rows AS
            (
            SELECT  e.*,
                    ROW_NUMBER() OVER (ORDER BY evid DESC) AS rn
            FROM    EventVenues e
            )
    UPDATE  rows
    SET     Ordering = rn
    

    This will update the Ordering in descending EVID order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.