Skip to content
Advertisement

SQL. Join with filter and offset

I need to create a SQL query that will return images with tags. The result should be ordered by some column and filtered by UserID and tags. The result should be paginated.

There is a SQL statement

This request is not working as expected. I expect to get 50 images, but they will be combined with tags and the result will be something like …

| Image.ImageID | Tag.Name | ... |
| 1             | First    | ... |
| 1             | Second   | ... |
| 2             | First    | ... |
| 3             | First    | ... |
....

How do I get 50 images instead of 50 rows?

Advertisement

Answer

You can change the order by to interleave the images:

Basically, this enumerates the rows for each image, returning the “first” row. Note: If you don’t have 50 images, then there will be duplicates as the code moves to the second tag.

If you want only one row per image with all tags, you can use APPLY:

This aggregates the tags for each image in the subquery, so only one row per image is returned. This concatenates the tags together. Of course, you could use other aggregation functions, such as MIN() or MAX().

You can also use:

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement