I'm new to APPLY statements in SQL and I'm trying to make a solution using an APPLY statement.
I have two tables:
Original ID | Target ID |
---|---|
SHAPE1 | SHAPE2 |
SHAPE3 | SHAPE4 |
Shape | Color | Num Sides | Size |
---|---|---|---|
SHAPE1 | Orange | 3 | Large |
SHAPE2 | Red | 6 | Small |
SHAPE3 | Orange | 3 | Large |
SHAPE4 | Green | 6 | Small |
These tables describe Shape IDs and a 'target' Shape. The goal here is to determine if the characteristics of Original Shape are the same as Target Shape. The query would compare the two columns and return 'TRUE' if they shared the same characteristics.
So, in the second table-- Shape 1 is a twin of Shape 3 meaning a query should return 'true' for Shape 1 and Shape 3, but not for anything else as there isn't a perfect match. (note the color green/red are different for 2 and 4)
SELECT CASE WHEN
(SELECT COUNT(1)
FROM table2 t2 (NOLOCK)
OUTER APPLY (SELECT t2.size, t2.color, t2.sides
WHERE t1.original_id = t2.shape
) source
OUTER APPLY (SELECT t2.size, t2.color, t2.sides
WHERE t1.target_id = t2.shape
) target
WHERE (ISNULL(source.size, '') <> ISNULL(target.size, '')
OR ISNULL(source.color, '') <> ISNULL(target.color, '')
OR ISNULL(source.sides, '') <> ISNULL(target.sides, '')) > 0
THEN NULL ELSE 'TRUE'
END
This was my attempt to do this as an outer apply. I've made the query work using both an 'EXISTS' and 'COUNT' statement, but I wanted to test my knowledge and see if it was ever possible using APPLYs. Unfortunately, this query doesn't yield correct results. It seems to think no matter what, there's always a difference between Shapes.
Well it is possible to use apply operators, but I sincerely don't believe it is worth the effort when simple joins will do the job. Note for the results below I added a row:
fiddle