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

  • SEARCH
  • Home
  • 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 84315
In Process

The Archive Base Latest Questions

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

Problem: a table of coordinate lat/lngs. Two rows can potentially have the same coordinate.

  • 0

Problem: a table of coordinate lat/lngs. Two rows can potentially have the same coordinate. We want a query that returns a set of rows with unique coordinates (within the returned set). Note that distinct is not usable because I need to return the id column which is, by definition, distinct. This sort of works (@maxcount is the number of rows we need, intid is a unique int id column):

select top (@maxcount) max(intid) from Documents d group by d.geoLng, d.geoLat 

It will always return the same row for a given coordinate unfortunately, which is bit of a shame for my use. If only we had a rand() aggregate we could use instead of max()… Note that you can’t use max() with guids created by newid().

Any ideas? (there’s some more background here, if you’re interested: http://www.itu.dk/~friism/blog/?p=121)

UPDATE: Full solution here

  • 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. 2026-05-10T21:54:42+00:00Added an answer on May 10, 2026 at 9:54 pm

    You might be able to use a CTE for this with the ROW_NUMBER function across lat and long and then use rand() against that. Something like:

    WITH cte AS (     SELECT         intID,         ROW_NUMBER() OVER             (                 PARTITION BY geoLat, geoLng                 ORDER BY NEWID()             ) AS row_num,         COUNT(intID) OVER (PARTITION BY geoLat, geoLng) AS TotalCount     FROM         dbo.Documents ) SELECT TOP (@maxcount)     intID, RAND(intID) FROM     cte WHERE     row_num = 1 + FLOOR(RAND() * TotalCount) 

    This will always return the first sets of lat and lngs and I haven’t been able to make the order random. Maybe someone can continue on with this approach. It will give you a random row within the matching lat and lng combinations though.

    If I have more time later I’ll try to get around that last obstacle.

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

Sidebar

Related Questions

I have a table that I've made sortable using the jQuery plugin Tablesorter 2.0
We have a table containing coordinates, among other things. We used to store these
I have a peculiar problem. I created a normal Objective C class called SampleTable.
I currently have the below tables representing a bus network mapped in hibernate, accessed
I have build a grid of div's as playground for some visual experiments. In
Problem This question actually came up at work today. We are planning an experiment
I am trying to get the current longitude and latitude right before the table
I'm having some difficulty figuring out what is going on and how to fix
i will need to print an x according to the coordinates given to me

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.