我无法从表中提取数据的顶线并将其与其他表中的其他提取字段连接起来。
我有3张桌子:
Person
Folder
Earnings
人:
PERSONID |FORENAMES|SURNAME|DOB |GENDER|NINO
1000000 |JOHNSTON |ALI |10/10/80 |M |JK548754A
文件夹:
FOLDERID|FOLDERREF
1000000 |104567LK
收益:
FOLDERID|DATESTARTED|DATEENDED |GROSSEARNINGS
1000000 |01-04-2014 |31-03-2015 |31846.00
1000000 |01-04-2013 |31-03-2014 |31160.04
1000000 |01-04-2012 |31-03-2013 |30011.04
1000000 |01-04-2011 |31-03-2012 |29123.94
我需要我的数据:
JOHNSTON |ALI| 10-10-1980 | 31-03-2015 | 31846.00 | 31649.60
我试过了:
SELECT A.PERSONID, A.SURNAME, A.FORENAMES, A.DOB, B.FOLDERREF, C.DATEENDED, C.GROSSEARNINGS, C.BASICEARNINGS, C.FLUCTUATINGEARNINGS
FROM PERSON A, FOLDER B, EARNINGS C
WHERE A.PERSONID = B.FOLDERID AND B.FOLDERID = C.FOLDERID
从EARNINGS表中提取所有数据,但我只希望提取顶线。
非常接受任何建议。
答案 0 :(得分:0)
如果您只想要来自最新日期的数据,那么您可以执行类似下面的查询。请记住,您正在使用c.BasicEarnings和c.FluctuatingEarnings等字段,而这些字段在表格中并不存在'收入'
SELECT a.PersonID
,a.Suranme
,a.Forenames
,a.DOB
,b.FolderRef
,c.DateEnded
,c.GrossEarnings
FROM Person a
JOIN Folder b ON a.FolderID = b.FolderID
JOIN (
SELECT e.FolderID
,e.DateEnded
,e.GrossEarnings
FROM Earnings e
JOIN (
SELECT FolderID
,MAX(DateEnded) DateEnded
FROM Earnings
GROUP BY FolderID
) m ON e.FolderID = m.FolderID
AND e.DateEnded = m.DateEnded
) c ON a.FolderID = c.FolderID
答案 1 :(得分:0)
假设您的预期输出中的最后一个字段是GROUP BY
,而“我只希望提取顶行”您的意思是最新的(按日期),然后使用MAX
使用SELECT p.FORENAMES, p.SURNAME, p.DOB, MAX(e.DATEENDED), e.GROSSEARNINGS, e.BASICEARNINGS
FROM Person p
INNER JOIN Earnings e ON p.PERSONID = e.FOLDERID
GROUP BY p.FORENAMES, p.SURNAME, p.DOB, e.GROSSEARNINGS, e.BASICEARNINGS
函数。
{{1}}