MS Access查询比较数字和显示

时间:2016-01-11 20:48:11

标签: sql-server ms-access

SELECT Student_Number,
DATEPART("yyyy", Starting_Date) AS Starting_Year,
DATEPART("yyyy",Ending_Date) AS Ending_Year
FROM   PGME
WHERE  ID = 1

显示如下

Student_Number    Starting_Year Ending_Year
003329497         1994          1999

但我需要的是比较" Starting_Year"和" Ending_Year" (他们是随机日期),当" Ending_Year"大于" Starting_Year",它将显示一个新行,直到" Starting_Year"等于" Ending_Year"。 (我希望得到以下结果)

Student_Number    Starting_Year Ending_Year
003329497         1994          1999
003329497         1995          1999
003329497         1996          1999
003329497         1997          1999
003329497         1998          1999
003329497         1999          1999

Student_Number(1)

Student_Number    Starting_Year Ending_Year
002442895         1993          2000
002442895         1994          2000
002442895         1995          2000
002442895         1996          2000
002442895         1997          2000
002442895         1998          2000
002442895         1999          2000
002442895         2000          2000

Student_Number(2)

有超过100个" Student_Number"我需要做的。我正在考虑使用"如果"声明要做到这一点,但无法弄明白。有什么办法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用笛卡尔(乘法)查询:

SELECT 
    Student_Number,
    Year(Starting_Date) + Ten.N AS Starting_Year,
    Year(Ending_Date) AS Ending_Year
FROM
    PGME,
        (SELECT DISTINCT Abs([id] Mod 10) AS N
        FROM msysobjects) As Ten
WHERE
    PGME.ID = 1
    And
    Ten.N <= Year(Ending_Date) - Year(Starting_Date)