仅获得日期最早的一个结果

时间:2019-03-28 08:14:03

标签: sql sql-server

我有一个查询,返回的值如下:

enter image description here

如您所见,我得到多个具有相同“ SeminarId”值的结果。我想获得具有最低“ Anreise”日期值的结果,并返回具有最早日期的结果。

我尝试使用MIN(),但出现聚合函数错误,并且我没有其他想法。

SELECT * FROM
(
SELECT TuId as TeiluberId, TU_Sem_ID AS SeminarId, TU_HotAnreiseDatum AS Anreise, TU_HotAbreiseDatum AS Abreise
,sem.Sem_von,sem.Sem_bis
FROM [WinClient].[TeilnehmerVerpflegungSeminar] tns
LEFT JOIN acc_seminar.t_Teiluber ON TuId = TU_ID
JOIN acc_seminar.t_Seminar sem ON sem_id = TU_Sem_ID
WHERE CONVERT(date,tns.Datum) < CONVERT(date, sem_von)
GROUP BY TuId, TU_Name, TU_Vorname, VpId, TU_Typ, TU_Sto, tu_sem_id, TU_HotAnreiseDatum, TU_HotAbreiseDatum, sem.Sem_von, sem.Sem_bis

UNION ALL

SELECT TU_ID, TU_Sem_ID, TU_HotAnreiseDatum, ISNULL(TU_HotAbreiseDatum, CONVERT(date, sem_bis)) AS Abreise, Sem_von, Sem_bis
FROM acc_seminar.t_Teiluber
JOIN acc_seminar.t_Seminar ON sem_id = TU_Sem_ID
WHERE TU_ID NOT IN (SELECT tvs.TuId FROM [WinClient].[TeilnehmerVerpflegungSeminar] tvs) AND TU_HotAnz > 0
AND TU_HotAnreiseDatum IS NOT NULL AND (CONVERT(date, TU_HotAnreiseDatum) != CONVERT(date, sem_von) OR CONVERT(date, TU_HotAbreiseDatum) != CONVERT(date, sem_bis))
) tu
WHERE tu.SeminarId = @Sem_ID

2 个答案:

答案 0 :(得分:1)

您可以像这样使用with命令:

;With wholeData AS
(
    SELECT TuId as TeiluberId, TU_Sem_ID AS SeminarId, TU_HotAnreiseDatum AS Anreise, TU_HotAbreiseDatum AS Abreise
        ,sem.Sem_von,sem.Sem_bis
    FROM [WinClient].[TeilnehmerVerpflegungSeminar] tns
    LEFT JOIN acc_seminar.t_Teiluber ON TuId = TU_ID
    JOIN acc_seminar.t_Seminar sem ON sem_id = TU_Sem_ID
    WHERE CONVERT(date,tns.Datum) < CONVERT(date, sem_von)
    GROUP BY TuId, TU_Name, TU_Vorname, VpId, TU_Typ, TU_Sto, tu_sem_id, TU_HotAnreiseDatum, TU_HotAbreiseDatum, sem.Sem_von, sem.Sem_bis

    UNION ALL

    SELECT TU_ID, TU_Sem_ID, TU_HotAnreiseDatum, ISNULL(TU_HotAbreiseDatum, CONVERT(date, sem_bis)) AS Abreise, Sem_von, Sem_bis
    FROM acc_seminar.t_Teiluber
    JOIN acc_seminar.t_Seminar ON sem_id = TU_Sem_ID
    WHERE TU_ID NOT IN (SELECT tvs.TuId FROM [WinClient].[TeilnehmerVerpflegungSeminar] tvs) AND TU_HotAnz > 0
    AND TU_HotAnreiseDatum IS NOT NULL AND (CONVERT(date, TU_HotAnreiseDatum) != CONVERT(date, sem_von) OR CONVERT(date, TU_HotAbreiseDatum) != CONVERT(date, sem_bis))
) 
SELECT q1.* from wholeData q1
    JOIN (select SeminarId, MIN(Anreise) from WholeData group by SeminarId) q2
    ON q1.SeminarId = q2.SeminarId AND q1.Anreise = q2.Anreise
    WHERE q1.SeminarId = @Sem_ID

答案 1 :(得分:1)

您可以使用row_number()

    with cte as
    (
    SELECT *,row_number() over(partition by SeminarId order by Anreise) rn
  FROM
    (
    SELECT TuId as TeiluberId, TU_Sem_ID AS SeminarId, TU_HotAnreiseDatum AS Anreise, TU_HotAbreiseDatum AS Abreise
    ,sem.Sem_von,sem.Sem_bis
    FROM [WinClient].[TeilnehmerVerpflegungSeminar] tns
    LEFT JOIN acc_seminar.t_Teiluber ON TuId = TU_ID
    JOIN acc_seminar.t_Seminar sem ON sem_id = TU_Sem_ID
    WHERE CONVERT(date,tns.Datum) < CONVERT(date, sem_von)
    GROUP BY TuId, TU_Name, TU_Vorname, VpId, TU_Typ, TU_Sto, tu_sem_id, TU_HotAnreiseDatum, TU_HotAbreiseDatum, sem.Sem_von, sem.Sem_bis

    UNION ALL

    SELECT TU_ID, TU_Sem_ID, TU_HotAnreiseDatum, ISNULL(TU_HotAbreiseDatum, CONVERT(date, sem_bis)) AS Abreise, Sem_von, Sem_bis
    FROM acc_seminar.t_Teiluber
    JOIN acc_seminar.t_Seminar ON sem_id = TU_Sem_ID
    WHERE TU_ID NOT IN (SELECT tvs.TuId FROM [WinClient].[TeilnehmerVerpflegungSeminar] tvs) AND TU_HotAnz > 0
    AND TU_HotAnreiseDatum IS NOT NULL AND (CONVERT(date, TU_HotAnreiseDatum) != CONVERT(date, sem_von) OR CONVERT(date, TU_HotAbreiseDatum) != CONVERT(date, sem_bis))
    ) tu
    WHERE tu.SeminarId = @Sem_ID
    ) select * from cte where rn=1