Click here to Skip to main content
15,891,431 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How Do I remove duplicate x,y,z per location first and count duplicated x,y,zs throughout all locations.

What I have tried:

SELECT x, y, z, name, number, count_ 
    FROM
            (SELECT x,y,z,name, number, count(*)
            OVER
                (PARTITION BY
                x,
                y,
                z
                ) AS count_
                       FROM locations AS ml
		               JOIN locs AS ma ON ma.locid= ml.locid GROUP BY x,y,z,lomdrivename) tableWithCount WHERE ORDER BY count_ desc
Posted
Updated 14-Oct-21 21:29pm

1 solution

Keep in mind that Group By happens before Aggregation OVER PARTITION BY.
So the best way is to only use one of them at a time
SQL
WITH Agg AS (
    SELECT  x
           ,y
           ,z
           ,name
           ,number
           ,count(*) OVER (PARTITION BY x,y,z ORDER BY ?) AS count_
           ,ROW_NUMBER() OVER (PARTITION BY x,y,z ORDER BY ?) AS rn
    FROM    locations AS ml
    JOIN    locs AS ma ON ma.locid = ml.locid
    )
SELECT  x
       ,y
       ,z
       ,name
       ,number
       ,count_
FROM    Agg
WHERE   rn = 1

Adjust as needed.
And keep in mind that you always need a field(s) to order by when you partition
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900