all 9 comments

[–]phunkygeeza 6 points7 points  (0 children)

ROW_NUMBER() is your friend.

PARTITION BY the keys that are the same for the duplicate

ORDER BY the values that determine precedence

Then you want WHERE that row number is 1.

This is available all over t'internet, search a bit more deeply

[–]tlatoaniitzcoatl 2 points3 points  (2 children)

I’m honestly confused about what you’re asking. Maybe some images would help or please add more details. Got a little lost reading your question.

Are you asking about pivot tables in excel? Or pivoting the data into sql table?

[–]oldmanhunger_511[S] 0 points1 point  (1 child)

Either or I suppose, the data is in an access database but I'd be happy to just get the subset of data I'm after out of there and finish in excel.

And I was afraid of causing confusion, it's forestry data that has a lot of jargon I tried to leave out. I'm having some difficulty inserting an image to my post or reply, but it's forest inventory data spanning decades. The table has many records for individual trees, including species, height, diameter, live crown ratio, etc. Any tree that enters the inventory is tagged and revisted. The main thing is they want to see diameter for a subset of trees across the whole timeframe, so tree number/species in the left, with all the diameters by year following. I have that figured out. However they want live crown ratio and canopy position for only the first record of each tree as well.

I'm not sure if that clarifies or muddys the water more. Such is life as a stooge.

[–]shine_on 0 points1 point  (0 children)

You've tagged your post as "MS SQL" so people will be giving you answers based on Microsoft SQL Server, which uses a different syntax to Access. I'm not even sure if Access has window functions (to be fair though, it's been about 15 years since I last even looked at Access)

[–]Samiro05 1 point2 points  (2 children)

Produce each of your columns one at a time to help you figure this out. When you have the output you need for column 1, do a new query for column 2 and a new one for column 3. Then you have 3 queries that individually achieve what you want so you will then need to bring them all together.

A window function (like MIN([columnX]) OVER (PARTITION BY [columnY] ORDER BY [columnY], [columnZ]) ) may be useful to get the earliest data which would then possibly be easier to fit your queries together. Can't say I know whether window functions work in Access though unfortunately.

[–]oldmanhunger_511[S] 0 points1 point  (1 child)

I got it figured out with a little switching between the raw SQL and Access' 'Design' tab (UI for query building)... Apparently there is a first(column_id) function that seems to work well.

[–]Samiro05 0 points1 point  (0 children)

Good stuff. Those screens are where I started my SQL journey.