I have this table:
from which I create this View:
This view's CardId
allows me to JOIN
against the Card
table with so I can retrieve the Count
from any CardId
. Here's my SQL:
SELECT * FROM (
SELECT
si.CardId SourceCardId,
ti.CardId TargetCardId,
(SELECT TOP 1 r.Name
FROM dbo.Relationship r
WHERE r.Id = rac.RelationshipId) [Name],
Count(*) [Count]
FROM dbo.Relationship_AcquiredCard rac
JOIN dbo.AcquiredCard sac ON rac.SourceAcquiredCardId = sac.Id
JOIN dbo.AcquiredCard tac ON rac.TargetAcquiredCardId = tac.Id
JOIN dbo.CardInstance si ON sac.CardInstanceId = si.Id
JOIN dbo.CardInstance ti ON tac.CardInstanceId = ti.Id
GROUP BY si.CardId, ti.CardId, rac.RelationshipId
-- you can probably ignore everything above
) X
CROSS APPLY
(values (X.TargetCardId),
(X.SourceCardId)
) whatdoesthisdo(CardId) --wut
What does whatdoesthisdo
do? I got the CROSS APPLY
from this answer. If I try to alias in the usual way, I get this error:
Thanks!