which one of the rows do you want to take, per template_id?
Main Topics
Browse All TopicsHi experts,
Here is my table (file_location):
file_id location_id entity_id template_id
100333 1255 1028 331
100333 1301 1025 331
100333 1322 1025 332
100333 1325 1025 332
100333 1326 1025 332
100335 1388 1028 331
My desired output:
100333 1301 1025 331
100333 1322 1025 332
I want to query the table to get entries with entity_id = 1025 AND unique template_id.
Thanks,
Stephen
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
> which one of the rows do you want to take, per template_id?
>Why these particular rows? What is significant about location id 1301 and 1322? Is it because these >appear first in the list or because these are the lowest value locations for each template_id?
Because they appear the first. I just want one copy for each template_id
Yes I tried it but it's not producing the result I want, let me explain more here
file_id location_id entity_id template_id
100333 1255 1028 331
100333 1301 1025 331
100333 1322 1025 332
100333 1325 1025 332 <-- for the same file_id, i just want one row per template_id
100333 1326 1025 332 <-- for the same file_id, i just want one row per template_id
100335 1388 1028 331
100334 1328 1025 332 <--- I want this because the file_id is different
My desired output:
100333 1301 1025 331
100333 1322 1025 332
100334 1328 1025 332
And the table is actually generated by 2 different tables, joined by file_id. The location_id the assoc_image table is just for quick reference, although it's value should be the same as the corresponding entry in the file_location table. See below:
file_location:
+ file_id
+ location_id
+ entity_id
assoc_image
+ file_id
+ location_id
+ template_id
Thanks,
Stephen
You say you only want one row per template_id for the same file_id. That's fine but which row do you want to retrieve as they have different values for location_id.
e.g. there are 3 possible rows for file_id 100333, entity_id 1025 and template_id 332. Does it matter which location_id is retrieved or don't you care?
file_id location_id entity_id template_id
100333 1322 1025 332
100333 1325 1025 332
100333 1326 1025 332
You show the following row as the one being retrieved:
100333 1322 1025 332
Why this one with location_id 1322? Is it because this is the lowest location_id? Does it matter which location_id is retrieved?
In the code ee_rlee has supplied he has assumed the row with the lowest location_id is retrieved but you haven't made it clear that this is your requirement.
try this, it will return the total number of rows and additional column on each row.
SELECT file_id, location_id, entity_id, template_id,count(*) over() cnt
FROM (SELECT file_id, location_id, entity_id, template_id,
ROW_NUMBER () OVER (PARTITION BY template_id ORDER BY location_id)
r
FROM file_location
WHERE entity_id = 1025)
WHERE r = 1
Business Accounts
Answer for Membership
by: ee_rleePosted on 2008-04-23 at 08:21:34ID: 21421657
hi, try this
Select allOpen in new window