简体   繁体   English

带排序的SQL查询

[英]SQL-query with sorting

Please help by writing a SQL-script that will collate data. 请编写可整理数据的SQL脚本来提供帮助。
A key difficulty - need to create an additional column on which sorting will take place. 关键困难-需要创建一个额外的列来进行排序。
I tried to describe the situation as detailed as possible. 我试图尽可能详细地描述这种情况。

Let's get started. 让我们开始吧。 There is a table of the following form: 有以下形式的表格:
DATAS
We will receive a user ID and return data, only those who do not have he, but there are others. 我们将收到一个用户ID并返回数据,只有那些没有他的用户,但还有其他人。
Next step: sort by artificially created column. 下一步:按人工创建的列排序。

Next, I'll step by step. 接下来,我将逐步进行。
So what do I mean by artificial column: 所以我所说的人工列是什么意思:
This column will contain the difference between the estimates. 此列将包含估算值之间的差异。 So to get it - you need to first perform a number of actions: 因此,要获取它-您首先需要执行许多操作:
According to the information which is like set the user and at other user to calculate the difference in assessment, and get an average score. 根据类似的信息,设置用户和其他用户来计算评估差异,并获得平均分数。
The following two pictures show the same data and then the calculation itself, it seems to me - it's pretty simple. 在我看来,以下两张图片显示了相同的数据,然后是计算本身,这很简单。
在此处输入图片说明


Calculation of this column is as follows: 该列的计算如下:

User with 2nd id:
1: 5 - 1 = 4; 
2: 2 - 9 = -7;
3: next data what is in user 1 - absent in user 2, and we ease pass it;
User with 3rd id:
1: 3 - 1 = 2;
2: the next data's is absent in user with 3rt id;
3: 8 – 9 = -1;
4: 6 – 2 = 4; 
5: passed;

End in the end:
User_2 will have new mark = -1.5
User_3 will have new mark = 1.66666

And in the end I need to return the table: 最后,我需要返回表: 在此处输入图片说明
But that's not all. 但这还不是全部。 Often, the data will be duplicated and I'd like to get average results from the data obtained. 通常,数据将被复制,我想从获得的数据中获得平均结果。 Please look at the following example: 请看下面的例子:

在此处输入图片说明

And this is the end. 到此为止。 I really need your help, experts. 专家,我真的需要您的帮助。 I teach sql code myself, but it is very difficult for me. 我自己教SQL代码,但这对我来说很困难。
Had the idea of ​​making the script as follows: 制作脚本的想法如下:

SELECT d.data, (d.mark + myCount(d.user, 1)) newOrder
FROM info d
WHERE -- data from user_1 NOT equal data from other users
ORDER BY newOrder;

But the script will execute a lot of time, because it uses its own function that could do with a query to each user, and not to record. 但是该脚本将执行大量时间,因为它使用了自己的功能,该功能可以对每个用户进行查询,而不是进行记录。 I hope someone will be able to cope with this task. 我希望有人能够应付这个任务。

Following your steps: 遵循您的步骤:

First, we need to isolate the data from the selected user (let's assume it's 1): 首先,我们需要将数据与所选用户隔离开(假设为1):

CREATE TEMP TABLE sel_user AS
SELECT data, mark FROM info d WHERE user = 1;

Now, we calculate the mark for every other user (again, the selected user is 1): 现在,我们为每个其他用户计算标记(同样,所选用户为1):

SELECT d.user user, d.mark - s.mark mark
FROM info d JOIN sel_user s USING (data)
WHERE d.user <> 1;

Result: 结果:

user        mark      
----------  ----------
2           4         
2           -7        
3           2         
3           -1        
3           4         

We can query just the average: 我们可以查询平均值:

SELECT d.user user, AVG(d.mark - s.mark) mark
FROM info d JOIN sel_user s USING (data)
WHERE d.user <> 1 GROUP BY user;

user        mark      
----------  ----------
2           -1.5      
3           1.66666666

But you still want to do calculations with the marks that do not correspond to user 1: 但是您仍然想使用与用户1不对应的标记进行计算:

SELECT d.user user, mark FROM info d
WHERE d.user <> 1 AND d.data NOT IN (SELECT data FROM sel_user);
user        mark      
----------  ----------
2           4         
3           3         
3           10

Specifically, you want to add the previously calculated average to each row: 具体来说,您想将先前计算的平均值添加到每一行:

SELECT d.user user, d.data, d.mark + d2.mark AS neworder FROM info d JOIN (
    SELECT d.user user, AVG(d.mark - s.mark) mark
    FROM info d JOIN sel_user s USING (data)
    WHERE d.user <> 1 GROUP BY user
) d2 USING (user)
WHERE d.data NOT IN (SELECT data FROM sel_user)
ORDER BY neworder DESC;
user        data        neworder        
----------  ----------  ----------------
3           6           11.6666666666667
3           3           4.66666666666667
2           5           2.5             

And your last request is to get the average for each data : 您的最后一个请求是获取每个data的平均值:

SELECT data, AVG(neworder) final FROM (
    SELECT d.user user, d.data, d.mark + d2.mark AS neworder FROM info d JOIN (
        SELECT d.user user, AVG(d.mark - s.mark) mark
        FROM info d JOIN sel_user s USING (data)
        WHERE d.user <> 1 GROUP BY user
    ) d2 USING (user)
    WHERE d.data NOT IN (SELECT data FROM sel_user)
)
GROUP BY data
ORDER BY final DESC;
data        final           
----------  ----------------
6           11.6666666666667
3           4.66666666666667
5           2.5     

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM