保持结果数据与插入顺序相同

时间:2019-02-22 07:35:26

标签: sql average

我已经将数据插入表部这样

Flowable.just("miss")
        ./* some other operators */
        .flatMap(s -> getDbOnlyIfNotCached(s))
        // if "miss" is cached, chain switches to another thread.
        .observeOn(AndroidSchedulers.mainThread())
        // Now switched to the main thread.
        .subscribe(s -> {
            Log.d(TAG, Thread.currentThread().getName());
        });

然后在将Dept表中的dept匹配到Emp表之后,我想以相同的deptname顺序显示匹配结果以及dept的avg sal。 结果应该是这样

Dept  Deptname
1     '1  dept'
5     '5   dept'
10     '10 dept'
20     '20 dept'
4      '4  DEPT'
3      '3 DEPT'

如何仅显示deptname和avg sal来达到相同的结果

2 个答案:

答案 0 :(得分:0)

使用order by,除非使用order by才能显示任何特定顺序的结果

  order by Dept asc

答案 1 :(得分:0)

您可以尝试以下代码:

li = [['Afghanistan', 2.66171813, 7.460143566, 0.490880072, 52.33952713, 0.427010864, -0.106340349, 0.261178523],
    ['Albania', 4.639548302, 9.373718262, 0.637698293, 69.05165863, 0.74961102, -0.035140377, 0.457737535],
    ['Algeria', 5.248912334, 9.540244102, 0.806753874, 65.69918823, 0.436670482, -0.194670126, 0],
    ['Argentina', 6.039330006, 9.843519211, 0.906699121, 67.53870392, 0.831966162, -0.186299905, 0.305430293],
    ['Armenia', 4.287736416, 9.034710884, 0.697924912, 65.12568665, 0.613697052, -0.132166177, 0.246900991],
    ['Australia', 7.25703764, 10.71182728, 0.949957848, 72.78334045, 0.910550177, 0.301693261, 0.45340696]]

li2 = [(min(y),max(y)) for y in [[x for x in l[1:]] for l in li]]
print (li2)

通过计算select dept.deptname, (select avg(emp.sal) from emp where emp.dept = dept.dept) as avgsal from dept 子句中的平均薪水,您将保留从SELECT表中进行选择时看到的数据顺序(而不是dept或{ {1}})。如果我没错,那正是您想要的。

希望我能帮上忙!