self-join

Select records in table A with *only* certain corresponding records in table B

北城以北 提交于 2019-12-12 03:48:50
问题 For example, say you have a table of files and a logging table of transfer operations. CREATE TABLE files ( id INTEGER PRIMARY KEY -- various other columns ); CREATE TABLE transfers ( id INTEGER PRIMARY KEY, file_id INTEGER, status TEXT NOT NULL, -- various other columns FOREIGN KEY (file_id) REFERENCES files (id) ) A transfer operation can have various statuses -- 'succeeded' , 'failed' , 'in progress' , etc. One file can have many transfer operations -- in particular, if a transfer fails,

sybase/sql self join multiple rows

家住魔仙堡 提交于 2019-12-12 03:43:21
问题 Hi stackoverflow community, I'm trying to do a self join if the unique ID in Col 1 is the same. Table code: CREATE TABLE #table ( Unique_ID int, Product_code varchar(10) ) INSERT INTO #table (Unique_ID, Product_code) VALUES (1111111111, 1) INSERT INTO #table (Unique_ID, Product_code) VALUES (1111111111, 2) INSERT INTO #table (Unique_ID, Product_code) VALUES (1111111111, 3) INSERT INTO #table (Unique_ID, Product_code) VALUES (2222222222, 4) INSERT INTO #table (Unique_ID, Product_code) VALUES

Distance matrix for mysql, based on previous solution for haversine distance calculation

拜拜、爱过 提交于 2019-12-11 23:59:10
问题 This question is self-answered to help people that need to create distance matrix in MySQL, which supports queries such as "select id2 from Distances where id1=10 and type1=0 and type2=1 and distance<10" which would find objects of type 1 in vicinity of 10 from object 10 of type 0. The code assumes table structure similar to one described in Mysql Haversine distance calculation If you ever find yourself in need of a distance matrix in mysql, here's a View definition for it: select o1.object

Is this query irreducibly complex?

不打扰是莪最后的温柔 提交于 2019-12-11 19:45:44
问题 I have two MySQL database tables, described below. One table holds device information, and the other is a one-to-many log about each device. CREATE TABLE `device` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `name` VARCHAR(255) NOT NULL, `active` INT NOT NULL DEFAULT 1, INDEX (`active`) ); CREATE TABLE `log` ( `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY, `device_id` INT NOT NULL, `message` VARCHAR(255) NOT NULL, `when` DATETIME NOT NULL, INDEX (`device_id`) ); What I want to do is grab

Sql Server Self JOIN (pushing column values down)

那年仲夏 提交于 2019-12-11 19:19:26
问题 I am asked to do the following: "CycleStartDate needs to be the BillDate from the previous BillDate record. If a previous record does not exist, you should use the most recent CycleEndDate from the DataTime table" CycleStartDate and CycleEndDate are columns in a table called DataTime BillDate is a column in a table called BillingData This is the BillDate values: 2012-07-27 00:00:00.000 2012-07-27 00:00:00.000 2012-08-27 00:00:00.000 2012-08-27 00:00:00.000 2012-09-28 00:00:00.000 2012-09-28

convert criteria to detached criteria for self join

◇◆丶佛笑我妖孽 提交于 2019-12-11 12:33:21
问题 I would like to know if i can convert this criteria into a detached criteria. I am not understanding detached criteria correctly. can some one help. Criteria crit = sessionC.createCriteria(OP_DOCTOR_VISIT.class, "OPDV1"); crit.createAlias("OPDV1.OP_VISIT", "OPDV2", JoinType.LEFT_OUTER_JOIN, Restrictions.and(Restrictions.eq("OPDV2.FORM", "NEW"), Restrictions.ge("OPDV2.USER_DATETIME", fromdate), Restrictions.le("OPDV2.USER_DATETIME", todate))); crit.add(Restrictions.ge("OPDV1.USER_DATETIME",

Search for documents with the same value in Elasticsearch

白昼怎懂夜的黑 提交于 2019-12-11 11:34:53
问题 I have a schema that looks something like this: { "mappings": { "entity": { "properties": { "a": { "type": "text" }, "b": { "type": "text" } } } I want to find all the values of b which have a value of a which is shared by 2 or more entities: Querying against: [{"a": "a1", "b": "b1"}, {"a": "a1", "b": "b2"}, {"a": "a2", "b": "b3"}] Should return b1 and b2 . 回答1: You can do a terms aggregation on the a field with a min_doc_count of 2 and then add a top_hits sub-aggregation to find the matching

How can I improve this 4 count self join?

久未见 提交于 2019-12-11 09:32:22
问题 Suppose I have the following sample data set: emplid | Citizenship | 100001 | USA | 100001 | CAN | 100001 | CHN | 100002 | USA | 100002 | CHN | 100003 | USA | And I want to arrange it to show the citizenships for each employee in one row. We can assume that an employee has up to four citizenships. The output would look like this: emplid | Citizeship_1 | Citizenship_2 | Citizenship_3 100001 | USA | CHN | CAN 100002 | USA | CHN | 100003 | USA | | The only working solution I have been able to

MySQL complex self join

一个人想着一个人 提交于 2019-12-11 06:03:04
问题 I having a table like this: id | name -------- 1 | test 2 | test1 3 | test 4 | test1 5 | test2 I am trying to write a query that will give a result like this test | test1 | test2 ---------------------------- 1 | 2 | 5 3 | 4 | And it is very clear there will be only three name, I am trying to write the query, But I couldn't. Can someone help me 回答1: Well, just to appease the doubters. Note, I'm not seriously advocating this as a solution, because it's just not very scalable - and making it

Correlated Subquery in SQL

蓝咒 提交于 2019-12-11 03:56:41
问题 I have a database of radiology reports which I have mined for incidents of pulmonary nodules. Each patient has a medical record number and each procedure has a unique accession number. Therefore, a MRN can have multiple Accession numbers for difference procedures. Accession numbers are ascending, so if a patient has multiple accession numbers the largest accession number is the latest procedure. I need to: Identify the oldest (initial) study Find the next study which comes soonest after the