我对MS Access中的两个SQL查询感到有些困惑,我必须在SQL Server中添加这些查询。
我想编写一个存储过程,但我不知道如何组合这两个sql查询。
这是第一个MaxMonthDate
:
SELECT Max(Day([Datum])) AS DayMax,
Month([Datum]) AS MonthMax,
Year([Datum]) AS YearMax,
DateSerial([YearMax],[MonthMax],[DayMax]) AS DatumMax
FROM DS
WHERE (((DS.Datum)>=[Formulare]![Hauptmenü]![Startdatum] And (DS.Datum)<=[Formulare]![Hauptmenü]![Enddatum]))
GROUP BY Month([Datum]), Year([Datum]);
第二个查询MaxMonthVal
:
SELECT DS.*
FROM MaxMonthDate INNER JOIN DS ON MaxMonthDate.DatumMax = DS.Datum
ORDER BY DS.Datum;
有人能告诉我如何在一个查询中组合这些吗?
答案 0 :(得分:0)
您可以使用子查询来组合查询。请参考以下查询:
SELECT DS.*
FROM DS where DS.Datum in
(Select DatumMax from
(SELECT Max(Day([Datum])) AS DayMax,
Month([Datum]) AS MonthMax,
Year([Datum]) AS YearMax,
DateSerial([YearMax],[MonthMax],[DayMax]) AS DatumMax
FROM DS
WHERE (((DS.Datum)>=[Formulare]![Hauptmenü]![Startdatum] And (DS.Datum)<=[Formulare]![Hauptmenü]![Enddatum]))
GROUP BY Month([Datum]), Year([Datum]))a
)
ORDER BY DS.Datum;
如果它可以帮助您解决问题,请将其标记为答案。
谢谢!