I have a table that does not contain a unique column
id | fid | ver | dir
1 || 1 || 1 || 3
2 || 2 || 1 || 2
3 || 3 || 1 || 3
4 || 3 || 2 || 3
5 || 4 || 1 || 4
6 || 5 || 1 || 5
My question is how can I select the ID of latest fid (based from ver) in dir = 3
I tried (SELECTidFROMtableWHEREdir='3' GROUP BYfidORDER BYversion) but it gives me
id
1
3
which is not the result I want to get. Here is the result I expect to have:
id
1
4
select id
from myTable t1
where dir = 3
and not exists (
select 1
from myTable t2
where t1.version < t2.version
and t1.dir = t2.dir
and t1.fid = t2.fid
)
order by version
SELECT MAX(id) FROM table WHERE dir = '3' GROUP BY fid ORDER BY version
Try group by Version instead,
SELECT id FROM table WHERE dir='3' GROUP BY version
Related
In the below table, I want to check the following using mysql query.
Any row_x of c1 is in between the row_x and row_x+1 of c2? So the result for the below table should be 2. Because 2 in c1 is in between 1 and 3 of c2
C1 || C2 ||
---------------
2 || 1 ||
5 || 3 ||
6 || 4 ||
7 || 5 ||
You can use math in the WHERE clause so:
select * from table where c1 between c2 and (c2 + 1)
should work.
https://www.db-fiddle.com/f/hE2oswGaEcJe9h8ZSdjURV/0
I am trying to implement a logic in Redshift Spectrum where my original table looks like below:
Records in the student table:
1 || student1 || Boston || 2019-01-01
2 || student2 || New York || 2019-02-01
3 || student3 || Chicago || 2019-03-01
1 || student1 || Dallas || 2019-03-01
Records in the incremental table studentinc looks like below:
1 || student1 || SFO || 2019-04-01
4 || student4 || Detroit || 2019-04-01
By joining both student and studentinc tables, I am trying to get the latest set of records which should look like below:
2 || student2 || New York || 2019-02-01
3 || student3 || Chicago || 2019-03-01
1 || student1 || SFO || 2019-04-01
4 || student4 || Detroit || 2019-04-01
I have got this solution by doing UNION of both student and studentinc, then querying the result of union based on max(modified_ts). However, this solution isn't good for huge tables, is there a better solution which works by joining both the tables?
1. Using Spark-SQL you can achieve this by using not in and union
scala> var df1 = Seq((1 ,"student1","Boston " , "2019-01-01" ),(2 ,"student2","New York" , "2019-02-01"),(3 ,"student3","Chicago " , "2019-03-01" ),(1 ,"student1","Dallas " , "2019-03-01")).toDF("id","name","country","_date")
register as temp table
scala> df1.registerTempTable("temp1")
scala> sql("select * from temp1") .show
+---+--------+--------+----------+
| id| name| country| _date|
+---+--------+--------+----------+
| 1|student1|Boston |2019-01-01|
| 2|student2|New York|2019-02-01|
| 3|student3|Chicago |2019-03-01|
| 1|student1|Dallas |2019-03-01|
+---+--------+--------+----------+
2nd DataFrame
scala> var df3 = Seq((1 , "student1", "SFO", "2019-04-01"),(4 , "student4", "Detroit", "2019-04-01")).toDF("id","name","country","_date")
scala> df3.show
+---+--------+-------+----------+
| id| name|country| _date|
+---+--------+-------+----------+
| 1|student1| SFO|2019-04-01|
| 4|student4|Detroit|2019-04-01|
+---+--------+-------+----------+
performing not in with union clause
scala> sql("select * from (select * from temp1 where id not in (select id from temp2 ) )tt") .union(df3).show
+---+--------+--------+----------+
| id| name| country| _date|
+---+--------+--------+----------+
| 2|student2|New York|2019-02-01|
| 3|student3|Chicago |2019-03-01|
| 1|student1| SFO|2019-04-01|
| 4|student4| Detroit|2019-04-01|
+---+--------+--------+----------+
2nd using Spark Dataframe this is faster than IN query becoz IN performs a row-wise operation.
scala> df1.join(df3,Seq("id"),"left_anti").union (df3).show
+---+--------+--------+----------+
| id| name| country| _date|
+---+--------+--------+----------+
| 2|student2|New York|2019-02-01|
| 3|student3|Chicago |2019-03-01|
| 1|student1| SFO|2019-04-01|
| 4|student4| Detroit|2019-04-01|
+---+--------+--------+----------+
Hope it helps you. let me know if you have any query related to the same
I would recommend window functions:
select s.*
from (select s.*,
row_number() over (partition by studentid order by date desc) as seqnum
from ((select s.* from student
) union all
(select i.* from incremental
from incremental
)
) s
) s
where seqnum = 1;
Note: The union all requires that the columns be exactly the same and in the same order. You may need to list out the columns if they are not the same.
I have 2 different tables in my database.
For eg; In table 1, named table1 , it has the following data:
||===============================||
|| ID | DATE ||
===================================
|| 1 | 2nd Jan ||
===================================
|| 2 | 4th Apr ||
===================================
And lets say in table 2, named table2, it has the following data:
||===============================||
|| ID | NAME ||
===================================
|| 1 | John ||
===================================
|| 2 | Pam ||
===================================
Now, both these table's (ID) is NOT THE SAME.
What I want to display is:
||===============================||===============================||
|| ID | NAME || ID | DATE ||
====================================================================
|| 1 | John || NULL | NULL ||
====================================================================
|| 2 | Pam || NULL | NULL ||
====================================================================
|| NULL | NULL || 1 | 2nd Jan ||
====================================================================
|| NULL | NULL || 2 | 4th Apr ||
====================================================================
So what I tried these mySQL statements:
select a.id, a.date, b.id, b.name from table1 a, table2 b
But this doesn't give me the correct display, it combines the result.
I also tried left join, it also combines the results.
What am I doing wrong? Please help me.
Thanks for reading.
select a.id, a.date, NULL id, NULL name from table1 a
UNION ALL
select NULL id, NULL date, b.id, b.name from table2 b
Just try above code.
Hope this will helps.
You can accomplish that with a 'fake' outer join:
select a.id, a.date, b.id, b.name
from table1 a
full outer join
table2 b
on 1 = 0;
I'm sorry for my bad title, because I confuse to choose the title. But I hope the description can explain it. I want to select value from relation table which have foreign key to two tables although the data of relation table is null, so there's my table
I have three tables :
Student
==========
id || name
===============
1 || Rooney
2 || Carrick
3 || Smalling
4 || De Gea
Then :
Item
==========
id || Title
===============
1 || Pre-Test
2 || Post-Test
3 || Final-Test
Then a table for many to many relation
Score
==========
id || student_id || item_id || Score
=====================================
1 || 1 || 1 || 100
2 || 1 || 2 || 80
3 || 2 || 1 || 90
4 || 2 || 3 || 85
5 || 3 || 2 || 80
6 || 3 || 3 || 90
7 || 4 || 1 || 95
And I want to get result like this :
Result
==========
score_id || student_name || item_name || Score
================================================
1 || Rooney || Pre-Test || 100
2 || Rooney || Post-Test || 80
NULL || Rooney || Final-Test|| NULL
3 || Carrick || Pre-Test || 90
NULL || Carrick || Post-Test || NULL
4 || Carrick || Final-Test|| 85
NULL || Smalling || Pre-Test || NULL
5 || Smalling || Post-Test || 80
6 || Smalling || Final-Test|| 90
7 || De Gea || Pre-Test || 95
NULL || De Gea || Post-Test || NULL
NULL || De Gea || Final-Test|| NULL
I have search some tutorial from another forum for outer join and try it in my database, but the query didn't show the null value. Thanks,
*)Edited
I've try query with union and join, but the result didn't show the null values
SELECT score.id AS score_id, student.name, item.title, score.score
FROM student
RIGHT JOIN score ON student.id = score.student_id
RIGHT JOIN item ON score.item_id = item.id
UNION
SELECT score.id AS score_id, student.name, item.title, score.score
FROM item
RIGHT JOIN score ON score.item_id = item.id
RIGHT JOIN student ON score.student_id = student.id
ORDER BY score_id
LIMIT 0 , 30
and
SELECT score.id AS score_id, student.name AS student_name, item.title AS item_title, score.score
FROM student
LEFT JOIN score ON student.id = score.student_id
LEFT JOIN item ON score.item_id = item.id
LIMIT 0 , 30
change to right join still didn't show the null values, here's the result :
score_id || student_name || item_title || score
1 || Rooney || Pre-Test || 100
2 || Rooney || Post-Test || 80
3 || Carrick || Pre-Test || 90
4 || Carrick || Final-Test || 85
5 || Smalling || Post-Test || 80
7 || Smalling || Final-Test || 90
8 || De Gea || Pre-Test || 95
use CROSS JOIN
SELECT
sc.id AS score_id,
s.name AS student_name,
i.title AS item_name,
sc.score
FROM student s
CROSS JOIN item i
LEFT JOIN score sc ON sc.item_id = i.id AND sc.student_id = s.id;
I have 2 tables
table1
ID Name
1 name1
2 name2
3 name3
4 name4
table2
ID Description Status
1 desc1 1
1 desc2 1
2 desc3 1
3 desc4 0
I need to get list of items {ID, Name} which are not in second table or are all stopped in second table. This items should be grouped.
in this example I should get
{1, name1},
{2, name2},
{4, name4}.
I believe this should get what you want:
var result = from p in table1
where table2.Any(x => x.ID == p.ID) == false
|| table2.Any(x => x.ID == p.ID && x.Status == 1)
select p;