MySQL filter out subset by group by - mysql

1A
a b c
1 1 6
1 1 7
2 1 8
2 2 2
2 2 9
B
a b c
1 1 7
2 2 9
I want to filter out a subset of A
a b c
1 1 6
2 2 2
I am intend to join two tables by group by column a, b
such that to select the value in column c is less than the c value in table B, which is the desired subset.
But don't know how to implement this.

Try this:
SELECT A.* FROM A INNER JOIN B
ON A.a=B.b AND A.c<B.c;
See MySQL Join Made Easy tutorial.

Related

Windows partition by with condition

I have a Dataframe like this
Sys_id
Id
A
4
A
5
A
6
A
100
A
2
A
3
A
4
A
5
A
6
A
7
B
100
B
2
B
3
B
4
B
5
B
6
B
100
I want to fetch the Id's between Id==100 how can I get that by partition using sys_id
I want an output like this
Sys_id
Id
A
2
A
3
A
4
A
5
A
6
A
7
B
2
B
3
B
4
B
5
B
6
I tried using
Windowspec=Window.partitionBy("sys_id").orderBy("timestamp")
df=df.withColum("id",(df.id==100).cast("int")
df=df.withColumn("next_id",lead('id',1).over(Windowspec))
Is there any alternative way to get the answer?

How to read and bind column?

I have many files, but I can not find how to bind column.
For example, files are followed
[1.txt]
ID Score
A 1
B 2
C 3
D 4
[2.txt]
ID Score
A 2
B 2
C 3
D 4
[3.txt]
ID Score
A 4
B 4
C 5
D 3
I want to make
A 1 2 4
B 2 2 4
C 3 3 5
D 4 4 3
You could use cbind() as follows:
df_final <- cbind(cbind(df1, df2["Score"]), df3["Score"])
df_final
ID Score Score Score
1 A 1 2 4
2 B 2 2 4
3 C 3 3 5
4 D 4 4 3
Note that if you were trying to match IDs between data frames which did not coincidentally have the order you want, then you would be asking more for a database style join. In this case, R offers the merge() function from baseR.
Demo

Mysql, order by "pattern"?

I want to order by type, by a pattern. Records now:
type name
1 a
2 b
1 c
4 d
4 e
3 f
2 g
3 h
my pattern is 2,4,3,1 so I would like to get:
2 b
2 g
4 d
4 e
3 f
3 h
1 a
1 c
use order by field (type,2,4,3,1). This will give you the correct result.

MySql subqueries and max or group by?

I have this table:
ID STUDENT CLASS QUESTION ANSWER TIME
1 1 1 1 c 12:30
2 1 1 1 d 12:36
3 1 1 2 a 12:38
4 2 1 1 b 11:24
5 2 1 1 c 11:26
6 2 1 3 d 11:35
7 2 3 3 b 11:24
I'm trying to write a query that does this:
For each STUDENT in a specific CLASS select the most recent ANSWER for each QUESTION.
So, choosing class "1" would return:
ID STUDENT CLASS QUESTION ANSWER TIME
2 1 1 1 d 12:36
3 1 1 2 a 12:38
5 2 1 1 c 11:26
6 2 1 3 d 11:35
I've tried various combinations of subqueries, joins, and grouping, but nothing is working. Any ideas?
You can use a sub-query to get most recent ANSWER per QUESTION, Then use this as a derived table and join back to the original table:
SELECT m.*
FROM mytable AS m
INNER JOIN (
SELECT STUDENT, QUESTION, MAX(`TIME`) AS mTime
FROM mytable
WHERE CLASS = 1
GROUP BY STUDENT, QUESTION
) AS d ON m.STUDENT = d.STUDENT AND m.QUESTION = d.QUESTION AND m.`TIME` = d.mTime
WHERE m.CLASS = 1
Demo here

Arranging Data according to their page number in reports

I got this problem regarding the pages the data is supposed to be placed.
This is how they look on the database.
CODE PAGE
---- ----
A 1
A 2
A 1
B 2
B 2
C 3
C 3
C 4
D 4
D 4
D 4
D 3
My desired output is
CODE PAGE
---- ----
A 1
A 1
A 1
B 2
B 2
C 3
C 3
C 3
D 4
D 4
D 4
D 4
How can I do this?
Works in SQL SERVER
select Code,Dense_Rank() Over (order by [Code]) Page from TableName
DEMO
like this:
select CODE,PAGE from <myTABLE>
order by PAGE,CODE