仅加入其他表中每个rown的最高(最新)值

时间:2017-09-24 08:29:33

标签: sql sqlite

我是SQL的新手,所以现有的答案对我来说有点复杂。 我有三张桌子:

WORKER
|id 
|name
|date
|...

JOB
|id
|name
|salary
|accept

APPOINTMENT
|id
|worker_id
|job_id
|date

因此,如果一年内多次任命工人,我需要知道他在某个特定时间所做的工作。

我现在有这样的事情:

SELECT w.name,w.id FROM worker w 
INNER JOIN appointment a ON w.id = worker_id
INNER JOIN job j on job_id = j.id 
WHERE accept = 1 AND a.date <= (SELECT date FROM orders WHERE id = 2);

现在它显示的所有约会都小于或等于某个日期,但我只需要为每个工作人员提供最后一个约会。 我需要如何修改它?

编辑:

ORDER
|id
|accepted_by //worker_id
|...

订单用于获取日期。它可以从任何来源更改为任何来源。所以在这方面并不重要。 在工作中接受只是一个bool值,它表示指定的工人可以接受新订单。 因此,完全意义是在ComboBox中以编辑形式显示所有能够接受订单的工人(不仅仅是现在可以接受订单的工人),订单创建时。

日期表示为1970年的整数天数。 假装输出的行:

w.name  w.id  a.id  a.date  j.name        j.accept
Smith   2     7     42999   administrator 1
Joe     1     6     42994   administrator 1
Smith   2     5     42994   waiter        0
Joe     1     4     42993   waiter        0
Smith   2     3     42992   administrator 1
Smith   2     2     42991   waiter        0
Smith   2     1     42990   administrator 1

我通过查询收到的内容(在此编辑上方列出)a.date&lt; = 42998 and accept = 1;

Joe     1     6     42994   administrator 1
Smith   2     3     42992   administrator 1 //isn't current Smith's job
Smith   2     1     42990   administrator 1 //isn't current Smith's job

我的查询a.date&lt; = 42998;

我应该收到什么
//Last job in 42998
Joe     1     6     42994   administrator 1
Smith   2     5     42994   waiter        0

我的查询a.date&lt; = 42999;

应该收到什么
//Last job in 42999
Smith   2     7     42999   administrator 1
Joe     1     6     42994   administrator 1

我最终需要接收的内容(a.date&lt; = 42998 and accept = 1):

//Workers which were able to accept order in 42998
Joe     1     6     42994   administrator 1

如果(a.date&lt; = 42999 and accept = 1);

我应该收到什么
//Workers which were able to accept order in 42999
Smith   2     7     42999   administrator 1
Joe     1     6     42994   administrator 1

表格(删除所有未使用的字段):

CREATE TABLE appointment (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, worker_id INTEGER NOT NULL, job_id INTEGER, date INTEGER NOT NULL);
CREATE TABLE worker (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,name TEXT NOT NULL);
CREATE TABLE job (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,name TEXT NOT NULL,accept INTEGER NOT NULL);

插入(如上例所示):

INSERT INTO worker (name) VALUES ('Joe');
INSERT INTO worker (name) VALUES ('Smith');
INSERT INTO job (name,accept) VALUES ('waiter',0);
INSERT INTO job (name,accept) VALUES ('administrator',1);
INSERT INTO appointment (worker_id,job_id,date) VALUES (2,2,42990);
INSERT INTO appointment (worker_id,job_id,date) VALUES (2,1,42991);
INSERT INTO appointment (worker_id,job_id,date) VALUES (2,2,42992);
INSERT INTO appointment (worker_id,job_id,date) VALUES (1,1,42993);
INSERT INTO appointment (worker_id,job_id,date) VALUES (2,1,42994);
INSERT INTO appointment (worker_id,job_id,date) VALUES (1,2,42994);
INSERT INTO appointment (worker_id,job_id,date) VALUES (2,2,42999);

1 个答案:

答案 0 :(得分:1)

要获得每个工作人员的最后一次约会,请使用分组。在第二步中过滤掉不接受的:

var testdata = [{
        "x": [
            "Jan-10",
            "Feb-10",
            "Mar-10",
            "Apr-10",
            "May-10"
        ],
        "y": [
            "100.0",
            "25.0",
            "100.0",
            "60.0",
            "500.0"
        ],
        "xf": "Jan-10",
        "xl": "May-10",
        "yf": "100.0",
        "yl": "500.0",
        "xtitle": "day",
        "ytitle": "rs"
    },
    {
        "x": [
            "Jan-11",
            "Feb-11",
            "Mar-11",
            "Apr-11",
            "May-11"
        ],
        "y": [
            "450.0",
            "650.0",
            "300.0",
            "70.0",
            "360.0"
        ],
        "xf": "Jan-11",
        "xl": "May-11",
        "yf": "450.0",
        "yl": "360.0",
        "xtitle": "day",
        "ytitle": "rs"
    }
];

if(testdata){
    months = testdata[0]['x'];


    output = [];
    for(var i = 0; i < months.length; i++){
        month = months[i].split('-')[0]
        output.push({
            "month" : month,
            "y1"    : testdata[0]['y'][i],
            "y2"    : testdata[1]['y'][i]
        });
    }

    console.log(output)
}