简体   繁体   中英

SQL-query with sorting

Please help by writing a SQL-script that will collate data.
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.
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.
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):

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):

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:

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 :

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     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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