2

I have a query like this :

SELECT WorkId, 
RegisterDate , Location

FROM (

SELECT dbo.[Work].WorkId ,
dbo.[Work].RegisterDate , dbo.Look.Location

FROM dbo.Municipality INNER JOIN
dbo.[Work] ON dbo.Municipality .Municipality Id = dbo.[Work].MunicipalityWorkId INNER JOIN
dbo.Look ON dbo.[Work].LookWorkId = dbo.Look.LookId
WHERE (dbo.Look.Location IS NOT NULL) AND Type= 1

) E

GROUP BY WorkId ,RegisterDate , Location

And I get this error :

The type "geography" is not comparable. It cannot be used in the GROUP BY clause.

I need to add Location to Group By, because I need to display Location in the database. What is the solution for this situation? Thanks.

jason
  • 6,962
  • 36
  • 117
  • 198
  • Post the complete query. – Pரதீப் Oct 14 '16 at 12:56
  • convert/cast to something else then group? it kinda makes sense to me not be able to group by geography... – gsharp Oct 14 '16 at 13:00
  • Please post the custom user type for `geography`? You may be missing a flag in your definintion - https://technet.microsoft.com/en-us/library/ms187630(v=sql.105).aspx . According to that site `You can perform ORDER BY, GROUP BY, and PARTITION BY operations with user-defined types if the type supports binary ordering. A type is binary ordered if it has the IsByteOrdered flag set to true in the SqlUserDefinedType attribute that is specified as part of the type definition. This flag indicates that the binary representation for the type is in the semantically correct order for that type.` – Steve Mangiameli Oct 14 '16 at 13:03
  • Unless you have horribly misnamed columns, you shouldn't need to use `GROUP BY`. You're grouping by `WorkId`. That name indicates it's unique in the `Work` table. You're joining on the `MunicipalityId` and `LookId` columns of the `Municipality` and `Look` tables. Those names indicate they're unique in their tables. What duplicates were you getting that made you think to do any grouping at all? –  Oct 14 '16 at 13:10

2 Answers2

4

Here is one way.

Convert the Location to text using Stastext and use it Group by. Then convert it back to geo in Select using STGeomFromText

SELECT WorkId,
       RegisterDate,
       geography::STGeomFromText(Location.STAsText(), 4326)
FROM   (SELECT dbo.[Work].WorkId,
               dbo.[Work].RegisterDate,
               dbo.Look.Location
        FROM   dbo.Municipality
               INNER JOIN dbo.[Work]
                       ON dbo.Municipality.MunicipalityId = dbo.[Work].MunicipalityWorkId
               INNER JOIN dbo.Look
                       ON dbo.[Work].LookWorkId = dbo.Look.LookId
        WHERE  ( dbo.Look.Location IS NOT NULL )
               AND Type = 1) E
GROUP  BY WorkId,
          RegisterDate,
          Location.STAsText() 

Referred from this answer

Note : The geography function are CASE sensitive it should be used as it is

  • STGeomFromText

  • STAsText

Community
  • 1
  • 1
Pரதீப்
  • 91,748
  • 19
  • 131
  • 172
0

You an not using aggregate function like sum or count so you could avoid group by and order by

SELECT 
    WorkId
  , RegisterDate 
  , Location

FROM (
    SELECT 
          dbo.[Work].WorkId 
        , dbo.[Work].RegisterDate 
        , dbo.Look.Location
    FROM dbo.Municipality 
    INNER JOIN dbo.[Work] ON dbo.Municipality.Municipality Id = dbo.[Work].MunicipalityWorkId 
    INNER JOIN dbo.Look ON dbo.[Work].LookWorkId = dbo.Look.LookId
WHERE (dbo.Look.Location IS NOT NULL) AND Type= 1
ORDER  BY WorkId ,RegisterDate , Location
) E
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107
  • Isn't there another solution? Because my actual query is based on Group By – jason Oct 14 '16 at 13:01
  • I changed my query to what you said and I got this : `The geography data type cannot be selected as DISTINCT because it is not comparable.` – jason Oct 14 '16 at 13:03