sorting (ascending & descending) value based on group same date? - pandas

I want to sort value (ascending/descending) value based on group same date. can anyone help how to achieve it?
df =
a b c
21-12-30 2 12 21
21-12-30 3 13 22
21-12-30 5 14 23
22-01-30 6 15 24
22-01-30 7 16 25
22-01-30 8 17 26
22-02-28 9 18 27
22-02-28 10 19 28
22-02-28 11 20 29
desired output =
a b c
21-12-30 5 14 23
21-12-30 3 13 22
21-12-30 2 12 21
22-01-30 8 17 26
22-01-30 7 16 25
22-01-30 6 15 24
22-02-28 11 20 29
22-02-28 10 19 28
22-02-28 9 18 27

One option:
out = (df.groupby(level=0, group_keys=False, sort=False)
.apply(lambda x: x.sort_values(by='a', ascending=False))
)
Another:
out = df.sort_values(by='a', ascending=False).sort_index(kind='stable')
output:
a b c
21-12-30 5 14 23
21-12-30 3 13 22
21-12-30 2 12 21
22-01-30 8 17 26
22-01-30 7 16 25
22-01-30 6 15 24
22-02-28 11 20 29
22-02-28 10 19 28
22-02-28 9 18 27

Related

How can i sum values that are unique which are connected to an id which is connected to a different id on which the summation needs to be based upon?

I have a Database Warehouse. That database warehouse has to have measures but i can't seem to get the measure to work as i wanted.
The measure is gonna be calculated by a dimension called document here is a table of what is currently in it:
document_id client_id contract size
12 4 NULL 431
13 4 NULL 2543
14 5 NULL 2565
15 8 NULL 2345
I try to make a measure in the fact table that sums up the document_id's and combines their size. But because its a database warehouse the data is not normalized so in the fact table there are multiple rows which have the same document id so if i sum it it won't work i'll get this data if i sum it:
company_id contract_id user_id task_id document_id insertion_date size_total
4 28 14 26 12 2021-01-16 431
4 28 14 27 12 2021-01-16 431
4 28 14 28 12 2021-01-16 431
4 28 14 29 12 2021-01-16 431
4 28 14 30 12 2021-01-16 431
4 28 14 26 13 2021-01-16 2543
4 28 14 27 13 2021-01-16 2543
4 28 14 28 13 2021-01-16 2543
4 28 14 29 13 2021-01-16 2543
4 28 14 30 13 2021-01-16 2543
4 29 14 26 12 2021-01-16 431
4 29 14 27 12 2021-01-16 431
4 29 14 28 12 2021-01-16 431
4 29 14 29 12 2021-01-16 431
4 29 14 30 12 2021-01-16 431
4 29 14 26 13 2021-01-16 2543
4 29 14 27 13 2021-01-16 2543
4 29 14 28 13 2021-01-16 2543
4 29 14 29 13 2021-01-16 2543
4 29 14 30 13 2021-01-16 2543
5 30 15 31 14 2021-01-16 2565
5 30 15 32 14 2021-01-16 2565
6 NULL 16 33 NULL 2021-01-16 NULL
6 NULL 16 34 NULL 2021-01-16 NULL
6 NULL 16 35 NULL 2021-01-16 NULL
6 NULL 17 33 NULL 2021-01-16 NULL
6 NULL 17 34 NULL 2021-01-16 NULL
6 NULL 17 35 NULL 2021-01-16 NULL
7 NULL NULL NULL NULL 2021-01-16 NULL
8 31 18 36 15 2021-01-16 2345
As you can see on the previous data you saw that company_id 4 had 2 documents combined the size was:431 +2543 = 2974 but i get different answers in the measure total_size. I want to have a total_size which combines unique ID's based upon their owner which is the company_id. I tried to do the following but it doesn't seem to work:
insert into [Stg_zone].[customer_usage]
( size_total, company_id,contract_id,user_id,
task_id,document_id,insertion_date)
select
sum ([size]) as size_total,
cu.[client_id] ,
cont.[contract_id],
usr.[user_id],
tsk.[task_id],
doc.[document_id] ,
SYSDATETIME ( ) as insertion_date
FROM [Stg_zone].[company] cu
left JOIN dbo.contract cont ON (cont.client_id=cu.client_id)
left JOIN dbo.[user] usr ON (usr.client_id=cu.client_id)
left JOIN dbo.document doc ON (doc.client_id=cu.client_id)
left JOIN dbo.task tsk ON (tsk.client_id=cu.client_id)
group by
cu.[client_id] ,
cont.[contract_id],
usr.[user_id],
tsk.[task_id],
doc.[document_id],
doc.size,
usr.role
Does anybody know how i can fix this issue ?

How can i change my AMPL mod/data file for Capacity to not get syntax error

My problem from NEOS server comes out as:
amplin, line 16 (offset 239):
syntax error
context: param Capacity{i in >>> 1...m <<< };
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Data is:
param m := 4;
param n := 30;
param Facilitycost:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 :=
1 2 7 7 6
2 8 3 1 5
3 2 5 6 6
4 7 5 2 3
5 5 3 6 8
6 9 8 5 1
7 4 4 6 7
8 8 4 8 11
9 10 5 2 5
10 1 8 9 9
11 7 1 5 8
12 1 7 8 8
13 1 7 8 8
14 7 1 3 6
15 3 5 8 8
16 8 1 4 8
17 7 1 3 6
18 7 3 2 4
19 10 3 3 7
20 4 4 7 9
21 4 5 5 4
22 6 6 4 2
23 9 6 2 3
24 6 4 8 10
25 6 6 4 2
26 4 7 6 4
27 8 1 3 7
28 1 8 9 8
29 6 2 6 9
30 9 6 2 3;
param Capacity:= 1 5000 2 5000 3 5000 4 5000;
param Demand:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 :=
1 220
2 374
3 351
4 432
5 161
6 300
7 300
8 219
9 339
10 312
11 653
12 440
13 207
14 492
15 91
16 190
17 351
18 323
19 23
20 157
21 281
22 233
23 409
24 215
25 7
26 680
27 215
28 395
29 165
30 333;
My model is:
param m; #number of facilites
param n; #number of customers needed to be served
param Facilitycost{j in 1..n, i in 1..m};
param Capacity{i in 1...m};
param Demand{j in 1..n};
#THE DECISION VARIABLES
var AllocatedFacility{j in 1..n, i in 1..m} binary;
#OBJECTIVE FUNCTION
minimize Total_AllocationCost: sum {j in 1..n, i in 1..m}:
Facilitycost[j,i] * AllocatedFacility[j,i];
#THE CONSTRAINTS
s.t. CapacityConstraints {i in 1..m}:
Demand[j] * AllocatedFacility[j,i] <= Capacity[i];
s.t. AllocatedFacilityContraints {j in 1..n}:
sum {i in 1..m} AllocatedFacility[j,i] = 1;
How can i change Capacity as such that the condition is met as wanted for the colum i?
Is the problem in the data of in the model?
The syntax error for this line is because you have 1...m where you should have 1..m.

How to use while loop in R to generate a matrix with specific number?(for->while)

I have generated a matrix by using the following for loop.
And now I am trying to generate a same matrix using while loop but don't know how to do so.
Can anyone help with this? Thank you so much.
a<-matrix(0, ncol=9, nrow=9)
for(i in 1:9) {
for(j in 1:9) {
a[i,j]<-i*j
}
}
a
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9]
[1,] 1 2 3 4 5 6 7 8 9
[2,] 2 4 6 8 10 12 14 16 18
[3,] 3 6 9 12 15 18 21 24 27
[4,] 4 8 12 16 20 24 28 32 36
[5,] 5 10 15 20 25 30 35 40 45
[6,] 6 12 18 24 30 36 42 48 54
[7,] 7 14 21 28 35 42 49 56 63
[8,] 8 16 24 32 40 48 56 64 72
[9,] 9 18 27 36 45 54 63 72 81
i<-1
j<-1
a<-matrix(0, ncol=9, nrow=9)
while (i<=9) {
+ while (j<=9) {
+ a[i,j]<-I*j
+ j<-j+1
+ }
+ i<-i+1
+ j<-1
+ }

how to eliminate a unique record using SQL?

I have a table with 3 columns. cid is the user, when is a timestamp of some transaction, and the 3rd column is me fumbling with how to achieve my objective.
In DB2, using this query:
SELECT cid, when, ROW_NUMBER() OVER (PARTITION BY cid ORDER BY when ASC) AS cid_when_rank
FROM (SELECT DISTINCT cid, when FROM yrb_purchase ORDER BY cid) AS temp
I get this table:
CID WHEN CID_WHEN_RANK
1 1999-04-20-12.12.00.000000 1
1 2001-12-01-11.59.00.000000 2
2 1998-08-08-17.33.00.000000 1
2 1999-02-13-15.13.00.000000 2
2 1999-04-16-11.46.00.000000 3
2 2001-02-23-12.37.00.000000 4
2 2001-04-24-17.02.00.000000 5
2 2001-10-21-11.05.00.000000 6
2 2001-12-01-15.39.00.000000 7
3 1998-01-27-09.19.00.000000 1
3 2001-10-06-11.12.00.000000 2
4 2000-06-13-09.45.00.000000 1
4 2001-06-30-13.58.00.000000 2
4 2001-08-11-17.40.00.000000 3
5 2001-07-17-16.27.00.000000 1
6 2000-05-18-11.43.00.000000 1
6 2001-07-08-18.09.00.000000 2
6 2001-10-02-12.37.00.000000 3
7 1999-06-15-12.13.00.000000 1
7 2000-05-05-14.49.00.000000 2
7 2000-09-26-16.32.00.000000 3
8 1999-01-19-09.32.00.000000 1
8 1999-08-02-09.20.00.000000 2
8 2000-07-03-12.39.00.000000 3
8 2001-08-13-13.11.00.000000 4
8 2001-10-18-10.18.00.000000 5
9 2001-09-10-13.03.00.000000 1
10 2000-03-11-10.05.00.000000 1
10 2001-03-11-15.46.00.000000 2
10 2001-04-29-18.30.00.000000 3
11 2001-07-27-11.45.00.000000 1
12 1999-02-07-10.59.00.000000 1
12 2001-08-24-11.12.00.000000 2
13 1998-03-17-14.04.00.000000 1
13 2001-05-18-10.11.00.000000 2
13 2001-09-14-12.56.00.000000 3
14 2001-10-10-17.18.00.000000 1
15 2000-12-01-18.27.00.000000 1
16 2000-01-04-14.18.00.000000 1
16 2001-02-27-15.08.00.000000 2
16 2001-11-16-09.52.00.000000 3
17 1998-04-08-17.59.00.000000 1
17 1999-06-07-10.13.00.000000 2
17 2001-09-13-12.08.00.000000 3
18 2001-09-22-10.01.00.000000 1
19 1999-03-09-12.11.00.000000 1
19 2001-07-23-09.27.00.000000 2
19 2001-12-01-16.10.00.000000 3
20 1999-11-22-14.29.00.000000 1
20 2000-05-27-17.56.00.000000 2
20 2001-06-01-09.37.00.000000 3
21 1998-02-17-16.08.00.000000 1
21 2000-02-15-13.22.00.000000 2
21 2001-03-10-15.05.00.000000 3
21 2001-03-10-16.22.00.000000 4
21 2001-10-25-10.15.00.000000 5
21 2001-11-19-11.02.00.000000 6
22 2001-03-04-17.13.00.000000 1
22 2001-08-16-16.59.00.000000 2
22 2001-10-23-11.24.00.000000 3
23 1998-07-04-16.33.00.000000 1
23 2000-09-26-13.17.00.000000 2
23 2000-09-27-12.27.00.000000 3
23 2001-06-23-16.45.00.000000 4
23 2001-10-27-18.01.00.000000 5
24 2001-10-23-14.59.00.000000 1
25 2001-03-14-09.26.00.000000 1
25 2001-11-30-14.23.00.000000 2
26 2001-04-27-15.07.00.000000 1
26 2001-06-30-11.26.00.000000 2
26 2001-12-01-18.04.00.000000 3
27 2000-06-05-09.44.00.000000 1
28 1999-07-17-10.14.00.000000 1
28 2001-02-03-15.50.00.000000 2
28 2001-02-13-12.08.00.000000 3
28 2001-07-20-16.52.00.000000 4
29 2001-06-10-17.16.00.000000 1
29 2001-09-20-10.19.00.000000 2
30 1999-05-22-16.59.00.000000 1
30 2001-10-20-15.28.00.000000 2
30 2001-12-01-14.50.00.000000 3
32 1999-05-05-14.20.00.000000 1
32 2000-05-12-13.51.00.000000 2
32 2001-05-18-10.43.00.000000 3
33 1999-02-07-18.58.00.000000 1
33 1999-09-30-14.05.00.000000 2
33 2001-09-18-12.48.00.000000 3
34 1999-05-29-15.57.00.000000 1
35 2001-03-19-18.38.00.000000 1
35 2001-03-28-15.49.00.000000 2
36 1999-06-22-11.42.00.000000 1
36 1999-10-30-15.25.00.000000 2
36 2000-01-27-10.17.00.000000 3
36 2000-11-04-09.06.00.000000 4
37 1999-01-11-09.51.00.000000 1
37 2000-11-25-17.53.00.000000 2
37 2000-12-01-17.21.00.000000 3
37 2001-10-21-16.49.00.000000 4
38 1997-10-11-17.15.00.000000 1
39 2000-03-09-13.46.00.000000 1
39 2001-01-09-16.22.00.000000 2
39 2001-07-03-14.12.00.000000 3
40 1998-07-27-17.39.00.000000 1
40 1999-01-27-09.36.00.000000 2
40 1999-06-12-17.18.00.000000 3
40 2000-05-17-14.17.00.000000 4
40 2001-04-08-15.39.00.000000 5
40 2001-09-30-10.26.00.000000 6
41 1998-06-05-10.06.00.000000 1
41 1998-08-23-09.39.00.000000 2
41 1999-12-01-18.42.00.000000 3
41 2001-03-30-15.26.00.000000 4
41 2001-11-15-15.33.00.000000 5
42 2000-06-22-12.16.00.000000 1
42 2001-01-13-15.03.00.000000 2
42 2001-08-19-14.18.00.000000 3
43 1998-07-07-11.29.00.000000 1
43 1999-01-22-15.46.00.000000 2
43 2000-08-04-12.16.00.000000 3
43 2001-03-17-14.18.00.000000 4
44 1999-11-03-09.32.00.000000 1
44 2001-05-26-17.23.00.000000 2
44 2001-07-18-12.59.00.000000 3
44 2001-10-23-10.04.00.000000 4
44 2001-11-09-16.18.00.000000 5
45 2000-03-19-10.31.00.000000 1
45 2001-07-14-11.36.00.000000 2
I am trying to eliminate all the customers (cid) who have made only one purchase. For example, cid=5 and cid=9 are good examples. The logic is that if they have a cid_when_rank=1, but no cid_when_rank=2, I need to drop those tuples. I have been breaking my head using INTERSECTION, EXCEPT, and using logic in the WHERE clause, but no luck. I looked online on how to eliminate DISTINCT records, but all I found was people discovering the DISTINCT keyword.
Please do not suggest hard coding cid=5 or cid=9 as there are more than those two records in the table.
Can you please suggest a simple SQL way to get this done. Please be aware I am not very strong at SQL yet, and would appreciate the most basic answer
Thanks in advance!
************************************EDIT #1**********************************
when I tried the first and second suggested answers my table went from 127 records to 287. I am trying to simply remove the records where a cid has a rank of 1, and does not have a rank of 2. Hope you can help.
The results of both suggested answers yield the same table:
CID WHEN CID_WHEN_RANK
1 1999-04-20-12.12.00.000000 1
1 2001-12-01-11.59.00.000000 2
1 2001-12-01-11.59.00.000000 3
1 2001-12-01-11.59.00.000000 4
1 2001-12-01-11.59.00.000000 5
2 1998-08-08-17.33.00.000000 1
2 1998-08-08-17.33.00.000000 2
2 1999-02-13-15.13.00.000000 3
2 1999-04-16-11.46.00.000000 4
2 2001-02-23-12.37.00.000000 5
2 2001-04-24-17.02.00.000000 6
2 2001-04-24-17.02.00.000000 7
2 2001-04-24-17.02.00.000000 8
2 2001-10-21-11.05.00.000000 9
2 2001-10-21-11.05.00.000000 10
2 2001-12-01-15.39.00.000000 11
3 1998-01-27-09.19.00.000000 1
3 1998-01-27-09.19.00.000000 2
3 1998-01-27-09.19.00.000000 3
3 2001-10-06-11.12.00.000000 4
3 2001-10-06-11.12.00.000000 5
3 2001-10-06-11.12.00.000000 6
3 2001-10-06-11.12.00.000000 7
3 2001-10-06-11.12.00.000000 8
4 2000-06-13-09.45.00.000000 1
4 2001-06-30-13.58.00.000000 2
4 2001-06-30-13.58.00.000000 3
4 2001-06-30-13.58.00.000000 4
4 2001-08-11-17.40.00.000000 5
5 2001-07-17-16.27.00.000000 1
5 2001-07-17-16.27.00.000000 2
5 2001-07-17-16.27.00.000000 3
5 2001-07-17-16.27.00.000000 4
5 2001-07-17-16.27.00.000000 5
5 2001-07-17-16.27.00.000000 6
5 2001-07-17-16.27.00.000000 7
6 2000-05-18-11.43.00.000000 1
6 2000-05-18-11.43.00.000000 2
6 2000-05-18-11.43.00.000000 3
6 2001-07-08-18.09.00.000000 4
6 2001-07-08-18.09.00.000000 5
6 2001-10-02-12.37.00.000000 6
7 1999-06-15-12.13.00.000000 1
7 1999-06-15-12.13.00.000000 2
7 2000-05-05-14.49.00.000000 3
7 2000-09-26-16.32.00.000000 4
8 1999-01-19-09.32.00.000000 1
8 1999-08-02-09.20.00.000000 2
8 2000-07-03-12.39.00.000000 3
8 2000-07-03-12.39.00.000000 4
8 2001-08-13-13.11.00.000000 5
8 2001-10-18-10.18.00.000000 6
8 2001-10-18-10.18.00.000000 7
9 2001-09-10-13.03.00.000000 1
9 2001-09-10-13.03.00.000000 2
9 2001-09-10-13.03.00.000000 3
9 2001-09-10-13.03.00.000000 4
9 2001-09-10-13.03.00.000000 5
9 2001-09-10-13.03.00.000000 6
9 2001-09-10-13.03.00.000000 7
9 2001-09-10-13.03.00.000000 8
10 2000-03-11-10.05.00.000000 1
10 2001-03-11-15.46.00.000000 2
10 2001-03-11-15.46.00.000000 3
10 2001-04-29-18.30.00.000000 4
10 2001-04-29-18.30.00.000000 5
11 2001-07-27-11.45.00.000000 1
11 2001-07-27-11.45.00.000000 2
11 2001-07-27-11.45.00.000000 3
11 2001-07-27-11.45.00.000000 4
11 2001-07-27-11.45.00.000000 5
12 1999-02-07-10.59.00.000000 1
12 2001-08-24-11.12.00.000000 2
12 2001-08-24-11.12.00.000000 3
12 2001-08-24-11.12.00.000000 4
13 1998-03-17-14.04.00.000000 1
13 2001-05-18-10.11.00.000000 2
13 2001-05-18-10.11.00.000000 3
13 2001-05-18-10.11.00.000000 4
13 2001-09-14-12.56.00.000000 5
14 2001-10-10-17.18.00.000000 1
14 2001-10-10-17.18.00.000000 2
14 2001-10-10-17.18.00.000000 3
14 2001-10-10-17.18.00.000000 4
14 2001-10-10-17.18.00.000000 5
14 2001-10-10-17.18.00.000000 6
14 2001-10-10-17.18.00.000000 7
14 2001-10-10-17.18.00.000000 8
15 2000-12-01-18.27.00.000000 1
15 2000-12-01-18.27.00.000000 2
15 2000-12-01-18.27.00.000000 3
15 2000-12-01-18.27.00.000000 4
15 2000-12-01-18.27.00.000000 5
16 2000-01-04-14.18.00.000000 1
16 2001-02-27-15.08.00.000000 2
16 2001-02-27-15.08.00.000000 3
16 2001-02-27-15.08.00.000000 4
16 2001-11-16-09.52.00.000000 5
16 2001-11-16-09.52.00.000000 6
16 2001-11-16-09.52.00.000000 7
17 1998-04-08-17.59.00.000000 1
17 1999-06-07-10.13.00.000000 2
17 2001-09-13-12.08.00.000000 3
17 2001-09-13-12.08.00.000000 4
17 2001-09-13-12.08.00.000000 5
18 2001-09-22-10.01.00.000000 1
18 2001-09-22-10.01.00.000000 2
18 2001-09-22-10.01.00.000000 3
19 1999-03-09-12.11.00.000000 1
19 1999-03-09-12.11.00.000000 2
19 1999-03-09-12.11.00.000000 3
19 2001-07-23-09.27.00.000000 4
19 2001-07-23-09.27.00.000000 5
19 2001-07-23-09.27.00.000000 6
19 2001-12-01-16.10.00.000000 7
19 2001-12-01-16.10.00.000000 8
19 2001-12-01-16.10.00.000000 9
19 2001-12-01-16.10.00.000000 10
19 2001-12-01-16.10.00.000000 11
20 1999-11-22-14.29.00.000000 1
20 1999-11-22-14.29.00.000000 2
20 2000-05-27-17.56.00.000000 3
20 2001-06-01-09.37.00.000000 4
20 2001-06-01-09.37.00.000000 5
21 1998-02-17-16.08.00.000000 1
21 2000-02-15-13.22.00.000000 2
21 2001-03-10-15.05.00.000000 3
21 2001-03-10-15.05.00.000000 4
21 2001-03-10-15.05.00.000000 5
21 2001-03-10-16.22.00.000000 6
21 2001-10-25-10.15.00.000000 7
21 2001-11-19-11.02.00.000000 8
21 2001-11-19-11.02.00.000000 9
21 2001-11-19-11.02.00.000000 10
21 2001-11-19-11.02.00.000000 11
22 2001-03-04-17.13.00.000000 1
22 2001-03-04-17.13.00.000000 2
22 2001-03-04-17.13.00.000000 3
22 2001-03-04-17.13.00.000000 4
22 2001-08-16-16.59.00.000000 5
22 2001-10-23-11.24.00.000000 6
23 1998-07-04-16.33.00.000000 1
23 2000-09-26-13.17.00.000000 2
23 2000-09-26-13.17.00.000000 3
23 2000-09-27-12.27.00.000000 4
23 2000-09-27-12.27.00.000000 5
23 2001-06-23-16.45.00.000000 6
23 2001-06-23-16.45.00.000000 7
23 2001-10-27-18.01.00.000000 8
23 2001-10-27-18.01.00.000000 9
23 2001-10-27-18.01.00.000000 10
23 2001-10-27-18.01.00.000000 11
24 2001-10-23-14.59.00.000000 1
24 2001-10-23-14.59.00.000000 2
24 2001-10-23-14.59.00.000000 3
25 2001-03-14-09.26.00.000000 1
25 2001-03-14-09.26.00.000000 2
25 2001-03-14-09.26.00.000000 3
25 2001-11-30-14.23.00.000000 4
26 2001-04-27-15.07.00.000000 1
26 2001-04-27-15.07.00.000000 2
26 2001-04-27-15.07.00.000000 3
26 2001-04-27-15.07.00.000000 4
26 2001-04-27-15.07.00.000000 5
26 2001-06-30-11.26.00.000000 6
26 2001-06-30-11.26.00.000000 7
26 2001-06-30-11.26.00.000000 8
26 2001-12-01-18.04.00.000000 9
26 2001-12-01-18.04.00.000000 10
26 2001-12-01-18.04.00.000000 11
27 2000-06-05-09.44.00.000000 1
27 2000-06-05-09.44.00.000000 2
28 1999-07-17-10.14.00.000000 1
28 2001-02-03-15.50.00.000000 2
28 2001-02-03-15.50.00.000000 3
28 2001-02-03-15.50.00.000000 4
28 2001-02-13-12.08.00.000000 5
28 2001-02-13-12.08.00.000000 6
28 2001-07-20-16.52.00.000000 7
28 2001-07-20-16.52.00.000000 8
29 2001-06-10-17.16.00.000000 1
29 2001-06-10-17.16.00.000000 2
29 2001-06-10-17.16.00.000000 3
29 2001-09-20-10.19.00.000000 4
29 2001-09-20-10.19.00.000000 5
29 2001-09-20-10.19.00.000000 6
30 1999-05-22-16.59.00.000000 1
30 2001-10-20-15.28.00.000000 2
30 2001-10-20-15.28.00.000000 3
30 2001-10-20-15.28.00.000000 4
30 2001-10-20-15.28.00.000000 5
30 2001-12-01-14.50.00.000000 6
30 2001-12-01-14.50.00.000000 7
32 1999-05-05-14.20.00.000000 1
32 1999-05-05-14.20.00.000000 2
32 2000-05-12-13.51.00.000000 3
32 2001-05-18-10.43.00.000000 4
32 2001-05-18-10.43.00.000000 5
32 2001-05-18-10.43.00.000000 6
32 2001-05-18-10.43.00.000000 7
32 2001-05-18-10.43.00.000000 8
33 1999-02-07-18.58.00.000000 1
33 1999-02-07-18.58.00.000000 2
33 1999-02-07-18.58.00.000000 3
33 1999-09-30-14.05.00.000000 4
33 1999-09-30-14.05.00.000000 5
33 1999-09-30-14.05.00.000000 6
33 2001-09-18-12.48.00.000000 7
33 2001-09-18-12.48.00.000000 8
34 1999-05-29-15.57.00.000000 1
34 1999-05-29-15.57.00.000000 2
35 2001-03-19-18.38.00.000000 1
35 2001-03-19-18.38.00.000000 2
35 2001-03-28-15.49.00.000000 3
35 2001-03-28-15.49.00.000000 4
36 1999-06-22-11.42.00.000000 1
36 1999-10-30-15.25.00.000000 2
36 1999-10-30-15.25.00.000000 3
36 1999-10-30-15.25.00.000000 4
36 2000-01-27-10.17.00.000000 5
36 2000-11-04-09.06.00.000000 6
37 1999-01-11-09.51.00.000000 1
37 1999-01-11-09.51.00.000000 2
37 1999-01-11-09.51.00.000000 3
37 2000-11-25-17.53.00.000000 4
37 2000-11-25-17.53.00.000000 5
37 2000-12-01-17.21.00.000000 6
37 2000-12-01-17.21.00.000000 7
37 2001-10-21-16.49.00.000000 8
38 1997-10-11-17.15.00.000000 1
38 1997-10-11-17.15.00.000000 2
38 1997-10-11-17.15.00.000000 3
38 1997-10-11-17.15.00.000000 4
38 1997-10-11-17.15.00.000000 5
38 1997-10-11-17.15.00.000000 6
39 2000-03-09-13.46.00.000000 1
39 2000-03-09-13.46.00.000000 2
39 2001-01-09-16.22.00.000000 3
39 2001-01-09-16.22.00.000000 4
39 2001-01-09-16.22.00.000000 5
39 2001-01-09-16.22.00.000000 6
39 2001-07-03-14.12.00.000000 7
40 1998-07-27-17.39.00.000000 1
40 1999-01-27-09.36.00.000000 2
40 1999-06-12-17.18.00.000000 3
40 1999-06-12-17.18.00.000000 4
40 2000-05-17-14.17.00.000000 5
40 2001-04-08-15.39.00.000000 6
40 2001-09-30-10.26.00.000000 7
40 2001-09-30-10.26.00.000000 8
41 1998-06-05-10.06.00.000000 1
41 1998-06-05-10.06.00.000000 2
41 1998-06-05-10.06.00.000000 3
41 1998-08-23-09.39.00.000000 4
41 1998-08-23-09.39.00.000000 5
41 1999-12-01-18.42.00.000000 6
41 1999-12-01-18.42.00.000000 7
41 1999-12-01-18.42.00.000000 8
41 2001-03-30-15.26.00.000000 9
41 2001-03-30-15.26.00.000000 10
41 2001-11-15-15.33.00.000000 11
42 2000-06-22-12.16.00.000000 1
42 2000-06-22-12.16.00.000000 2
42 2001-01-13-15.03.00.000000 3
42 2001-01-13-15.03.00.000000 4
42 2001-08-19-14.18.00.000000 5
42 2001-08-19-14.18.00.000000 6
42 2001-08-19-14.18.00.000000 7
42 2001-08-19-14.18.00.000000 8
43 1998-07-07-11.29.00.000000 1
43 1999-01-22-15.46.00.000000 2
43 2000-08-04-12.16.00.000000 3
43 2001-03-17-14.18.00.000000 4
43 2001-03-17-14.18.00.000000 5
43 2001-03-17-14.18.00.000000 6
44 1999-11-03-09.32.00.000000 1
44 2001-05-26-17.23.00.000000 2
44 2001-07-18-12.59.00.000000 3
44 2001-10-23-10.04.00.000000 4
44 2001-10-23-10.04.00.000000 5
44 2001-10-23-10.04.00.000000 6
44 2001-10-23-10.04.00.000000 7
44 2001-11-09-16.18.00.000000 8
45 2000-03-19-10.31.00.000000 1
45 2000-03-19-10.31.00.000000 2
45 2000-03-19-10.31.00.000000 3
45 2001-07-14-11.36.00.000000 4
287 record(s) selected.
Any suggestions?
You can use the count window function to fetch cid's when they have more than 1 row.
select cid,when,cid_when_rank
from (
SELECT cid, when, ROW_NUMBER() OVER(PARTITION BY cid ORDER BY when ASC) AS cid_when_rank
,COUNT(*) OVER(PARTITION BY cid) as cnt
FROM yrb_purchase
) t
where cnt > 1
Edit: Based on OP's comment,
select cid,when,cid_when_rank
from (
SELECT cid, when, ROW_NUMBER() OVER(PARTITION BY cid ORDER BY when ASC) AS cid_when_rank
,COUNT(*) OVER(PARTITION BY cid) as cnt
FROM (SELECT DISTINCT cid, when FROM yrb_purchase) tmp
) t
where cnt > 1
Using count(*) as a window function is a very good solution. One way that might return results faster is exists:
select p.*
from yrb_purchase p
where exists (select 1 from yrb_purchase p2 where p2.when <> p.when);
Of course, if you need the row number as well, then the overhead for the count is probably immeasurable.

trying to get user assign to under another user

I am trying to write a sql query in which I want all the list of uid assigned under another uid from the below given table
uid rid assTo
1 1 NULL
2 2 1
3 1 2
6 11 3
7 11 1
17 11 1
18 11 1
19 11 1
21 11 1
22 2 1
23 11 22
24 2 22
25 10 24
26 10 24
27 11 26
28 11 26
29 10 24
30 11 3
31 11 29
32 11 29
33 11 29
34 11 29
35 11 29
36 11 29
37 11 29
38 11 29
39 11 29
40 11 29
41 11 29
47 11 2
48 11 2
50 11 26
51 11 2
52 11 26
53 11 29
55 11 1
56 11 1
57 11 652
68 11 652
70 11 652
71 11 652
72 11 2
74 1 1
75 11 2
76 11 652
80 11 652
86 11 652
87 11 1
88 11 26
89 11 29
I want all the list of uid assigned to another uid i.e. designated in assto column in above table
How can I implement it?
My query
select u1.assignto 'assignto',u2.userid 'userid'
from users u1 join users u2
on u1.assignto=u2.userid
My desired output is that when i search uid = 1
i should the list of the uid available in the above table.
IN solution:
select * from tablename
where uid in (select assTo from tablename)
Self JOIN solution:
select distinct t1.*
from tablename t1
JOIN tablename t2 ON t1.uid= t2.assTo
EXISTS solution (similar to IN):
select *
from tablename t1
where exists (select 1 from tablename t2
where t2.assTo = t1.uid)