在访问查询中仅显示具有特定字段数据的最后一条记录
Show only last record with specific field data in access query
我的 SQL 技能很基础,但我正在尝试为小型企业设置一个非营利性数据库。
我有一个 table (extract_financial) 的财务数据:
regno (organisation registration number, unique),
fystart (date, financial year start),
fyend (date, financial year end),
income,
exped (expenditure).
每个组织都会有一些不同财政年度的记录。并非所有记录都包括收入和支出值。
我只想显示每个组织的一条记录(包括regno、fyend、income),最新的有任何收入。
我试过以下改编自类似问题的脚本,但没有用:
SELECT ef.regno, ef.fyend, ef.income
FROM extract_financial ef
INNER JOIN
(
SELECT regno, Max(fyend) AS MaxOfFyend
FROM extract_financial
GROUP BY regno
) AS efx
ON ef.regno = efx.regno
AND ef.fyend = efx.MaxOfFyend
WHERE ef.income IS NOT NULL
为每个 [regno] 查找最新条目的查询有效,但问题是最新记录中从来没有任何收入...所以我想我需要一个 IF THEN?
感谢您的帮助,谢谢!
你很接近。 WHERE
子句需要进入子查询:
SELECT ef.regno, ef.fyend, ef.income
FROM extract_financial as ef INNER JOIN
(SELECT regno, Max(fyend) AS MaxOfFyend
FROM extract_financial as ef
WHERE ef.income IS NOT NULL
GROUP BY regno
) AS efx
ON ef.regno = efx.regno AND ef.fyend = efx.MaxOfFyend;
我的 SQL 技能很基础,但我正在尝试为小型企业设置一个非营利性数据库。 我有一个 table (extract_financial) 的财务数据:
regno (organisation registration number, unique),
fystart (date, financial year start),
fyend (date, financial year end),
income,
exped (expenditure).
每个组织都会有一些不同财政年度的记录。并非所有记录都包括收入和支出值。
我只想显示每个组织的一条记录(包括regno、fyend、income),最新的有任何收入。
我试过以下改编自类似问题的脚本,但没有用:
SELECT ef.regno, ef.fyend, ef.income
FROM extract_financial ef
INNER JOIN
(
SELECT regno, Max(fyend) AS MaxOfFyend
FROM extract_financial
GROUP BY regno
) AS efx
ON ef.regno = efx.regno
AND ef.fyend = efx.MaxOfFyend
WHERE ef.income IS NOT NULL
为每个 [regno] 查找最新条目的查询有效,但问题是最新记录中从来没有任何收入...所以我想我需要一个 IF THEN?
感谢您的帮助,谢谢!
你很接近。 WHERE
子句需要进入子查询:
SELECT ef.regno, ef.fyend, ef.income
FROM extract_financial as ef INNER JOIN
(SELECT regno, Max(fyend) AS MaxOfFyend
FROM extract_financial as ef
WHERE ef.income IS NOT NULL
GROUP BY regno
) AS efx
ON ef.regno = efx.regno AND ef.fyend = efx.MaxOfFyend;