Использовать результаты подзапроса в основном запросе - PullRequest
0 голосов
/ 21 февраля 2019

Как сделать результат подзапроса доступным в основном запросе, как в этом примере?

SELECT Id AS ParticipantId, TeamId,
ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date), 0) AS FirstWeight,
ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date DESC), 0) AS LastWeight,
FirstWeight - LastWeight As WeightDiff // this doesn't work
FROM Participants

Ответы [ 3 ]

0 голосов
/ 21 февраля 2019

FirstWeight, LastWeight в строке Псевдоним не будет работать на сервере sql. Вы можете изменить свой запрос следующим образом

SELECT Id AS ParticipantId, TeamId,
ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date), 0) AS FirstWeight,
ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date DESC), 0) AS LastWeight,
ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date), 0)- ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date DESC), 0) As WeightDiff // this doesn't work
FROM Participants

или использовать подзапрос на следующий уровень

select ParticipantId, TeamId,FirstWeight,LastWeight,
 FirstWeight-LastWeight as WeightDiff from
  ( 
  SELECT Id AS ParticipantId, TeamId,
    ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date), 0) AS FirstWeight,
    ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date DESC), 0) AS LastWeight
FROM Participants
  ) as t
0 голосов
/ 21 февраля 2019

Использование outer apply:

SELECT Id AS ParticipantId, TeamId,
       COALESCE(f.FirstWeight, 0) as FirstWeight,
       COALESCE(f.LastWeight, 0) as LastWeight,
       (f.FirstWeight - l.LastWeight) As WeightDiff 
FROM Participants p OUTER APPLY
     (SELECT TOP(1) pd.Weight as firstWeight
      FROM ParticipantData pd
      WHERE (pd.ParticipantId = p.Id) AND Weight <> 0
      ORDER BY Date
     ) f OUTER APPLY
     (SELECT TOP(1) pd.Weight as lastWeight
      FROM ParticipantData pd
      WHERE (pd.ParticipantId = p.Id) AND Weight <> 0
      ORDER BY Date DESC
     ) l;
0 голосов
/ 21 февраля 2019

Один из вариантов - поместить текущий запрос в CTE, а затем выполнить его подзапрос, используя псевдонимы:

WITH cte AS (
    SELECT Id AS ParticipantId, TeamId,
        ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date), 0) AS FirstWeight,
        ISNULL((SELECT TOP(1) Weight FROM ParticipantData WHERE (ParticipantId = Participants.Id) AND (Weight <> 0) ORDER BY Date DESC), 0) AS LastWeight
    FROM Participants
)

SELECT
    ParticipantId,
    TeamId,
    FirstWeight,
    LastWeight,
    FirstWeight - LastWeight As WeightDiff
FROM cte;
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...