join

SQL join two tables with specific condition

て烟熏妆下的殇ゞ 提交于 2020-05-15 04:54:27
问题 Table A structure: Table B structure: Above are two tables, TableB.TableARelationID is a relationID which used to map table A. Desired output: The desired result would be taking TableA.RecordID and TableB.Text, but only of Type 2 in table B, i.e. ignore Type 1 Below is the SQL query which I used: SELECT tablea.recordid, tableb.text FROM tablea LEFT JOIN tableb ON tablea.relationid = tableb.tablearelationid WHERE type = 2 But the above query would output: i.e. RecordID 1 was missing, as the

SQL join two tables with specific condition

拟墨画扇 提交于 2020-05-15 04:53:44
问题 Table A structure: Table B structure: Above are two tables, TableB.TableARelationID is a relationID which used to map table A. Desired output: The desired result would be taking TableA.RecordID and TableB.Text, but only of Type 2 in table B, i.e. ignore Type 1 Below is the SQL query which I used: SELECT tablea.recordid, tableb.text FROM tablea LEFT JOIN tableb ON tablea.relationid = tableb.tablearelationid WHERE type = 2 But the above query would output: i.e. RecordID 1 was missing, as the

Sub-query in MongoDB

ⅰ亾dé卋堺 提交于 2020-05-14 22:05:14
问题 I have two collections in MongoDB, one with users and one with actions. Users look roughly like: {_id: ObjectId("xxxxx"), country: "UK",...} and actions like {_id: ObjectId("yyyyy"), createdAt: ISODate(), user: ObjectId("xxxxx"),...} I am trying to count events and distinct users split by country. The first half of which is working fine, however when I try to add in a sub-query to pull the country I only get nulls out for country db.events.aggregate({ $match: { createdAt: { $gte: ISODate(

Sub-query in MongoDB

孤街醉人 提交于 2020-05-14 22:05:01
问题 I have two collections in MongoDB, one with users and one with actions. Users look roughly like: {_id: ObjectId("xxxxx"), country: "UK",...} and actions like {_id: ObjectId("yyyyy"), createdAt: ISODate(), user: ObjectId("xxxxx"),...} I am trying to count events and distinct users split by country. The first half of which is working fine, however when I try to add in a sub-query to pull the country I only get nulls out for country db.events.aggregate({ $match: { createdAt: { $gte: ISODate(

Laravel eloquent get the latest row of related table

冷暖自知 提交于 2020-05-13 06:06:24
问题 I have two tables, books , and chapters . One book has many chapters. Book model: public function chapters() { return $this->hasMany(Chapter::class); } Chapter model: public function book() { return $this->belongsTo(Book::class); } I want to get book list with their own latest chapter using single query like this: $books = Book::with(['authors', 'categories', 'chapters' => function($q) { $q->orderBy('updated_at', 'desc')->first(); }]->get(); But it doesn't work. Chapters return an empty array

dplyr: NSE in joins (by)

牧云@^-^@ 提交于 2020-05-12 04:28:26
问题 I had a hard time to figure out how I could join two tables using dplyr::left_join with NSE. The problem was that I could not supply the right value to 'by'. I think I have found a solution for now, but it feels like I am doing it in an extra complicated way. So, if you know an easier/more elegant solution, please let me know :) That's what I am doing: # some data df <- dplyr::tibble(x=1:10, y=LETTERS[1:10], z=LETTERS[11:20]) # some function test_fun <- function(df,id){ id <- rlang::enquo(id)

Hibernate relation with aggregation

♀尐吖头ヾ 提交于 2020-05-01 02:49:00
问题 @Entity public class Device { private long id; private String deviceName; //column D_NAME private Set<LoginDate> loginDates; } /** Audit table being filled by some other processes on the network */ @Entity public class LoginDate { private long id; //pk private String deviceName; //column D_NAME private Date loginDate; } My aim to display the last login date of the device on the report screens; I have implemented OneToMany relation between Device and LoginDate tables where it is using OrderBy

Hibernate relation with aggregation

删除回忆录丶 提交于 2020-05-01 02:48:24
问题 @Entity public class Device { private long id; private String deviceName; //column D_NAME private Set<LoginDate> loginDates; } /** Audit table being filled by some other processes on the network */ @Entity public class LoginDate { private long id; //pk private String deviceName; //column D_NAME private Date loginDate; } My aim to display the last login date of the device on the report screens; I have implemented OneToMany relation between Device and LoginDate tables where it is using OrderBy

Sum on Multiple Left Joins

荒凉一梦 提交于 2020-04-30 16:33:31
问题 I am trying to sum the total value of a column for a specific ID after multiple left joins. The below code gives me what I am looking for but across multiple rows, I need the value for T3.C_Amt and T4.E_Amt to be totaled. SELECT T1.ID, T2.Unique_ID, T3.C_Date, T3.C_Amount, T4.D_Date, T4.D_Amount FROM TABLE_1 T1 LEFT JOIN DATABASE1.TABLE_2 T2 ON T1.ID = T2.UNIQUE_ID LEFT JOIN DATABASE1.TABLE_3 T3 ON T2.Unique_ID = T3.Unique_ID AND T3.C_Date = '2019-04-11' LEFT JOIN DATABASE1.TABLE_4 T4 ON T2

Referencing a query result alias in a subquery

£可爱£侵袭症+ 提交于 2020-04-30 08:26:06
问题 This is mostly a SQL syntax / SQL capability question. Why does the following query NOT work: SELECT * from ( select m.*, p.type, from multipliers m inner join pushes p on m.push_id = p.id where p.type = 'CONSTANT' ) AS res1 where res1.push_id = ( select max(push_id) from res1 ); when the following completes without issue: SELECT * from ( select m.*, p.type, from multipliers m inner join pushes p on m.push_id = p.id where p.type = 'CONSTANT' ) AS res1 where res1.push_id = ( select max(push_id