self-join

How to do an upper recursive self-join in SQL Server?

佐手、 提交于 2019-12-11 03:15:26
问题 How can I do a recursive self-join in SQL Server ? I have a table like this: TableID | ParentID 1 | NULL 2 | 1 3 | 1 4 | 3 5 | NULL 6 | 4 7 | 6 I want to get the following results based on given TableID to get all the ParentsID related to the TableID , let's say I want to get all the parents for the TableID = 6 : TableID 6 4 3 1 I'm stuck on this and i don't know how to get the result in SQL Query ... Hope to tell me the SQL Query to get the previous data 回答1: It should be ; WITH MyQuery

Rewrite self join to JPQL

假如想象 提交于 2019-12-11 02:55:50
问题 I need to convert this self join to JPQL: SELECT s1.* FROM site AS s1 JOIN (SELECT site_type, MAX(last_update_date) AS LastUpdate FROM site WHERE site.last_update_date > "2011-02-27 16:57:53" GROUP BY site_type) AS s2 ON s1.site_type = s2.site_type AND s1.last_update_date = s2.LastUpdate ORDER BY s1.last_update_date DESC Edit: the solution: SELECT s1 FROM Site s1 WHERE s1.lastUpdateDate = ( SELECT MAX(s2.lastUpdateDate) FROM Site s2 WHERE s1.siteType = s2.siteType) AND s1.lastUpdateDate >

SQL move data from rows to cols

為{幸葍}努か 提交于 2019-12-11 02:40:47
问题 Sorry for the bad title - I simply do not know what to call the thing I want to do. Here it goes: In MS SQL Server 2008 I have a temp table with 4000+ rows created with the WITH statement looking like this: ID (varchar) DATE (int) AB1135000097 | 20151221 AB1135000097 | 20160119 AB1135000097 | 20160219 AB1135001989 | 20120223 AB1135001989 | 20120323 AB1135001989 | 20120423 . . . I want to pair the data in date-ranges based on DATE. AB1135000097 | 20151221 | 20160119 AB1135000097 | 20160119 |

Self join tutorial #10 on sqlzoo

旧街凉风 提交于 2019-12-11 02:33:35
问题 I have tried http://sqlzoo.net/wiki/Self_join Self Join Session for the #10 # 10 : Find the routes involving two buses that can go from Craiglockhart to Sighthill.Show the bus no. and company for the first bus, the name of the stop for the transfer,and the bus no. and company for the second bus. Here is my code: SELECT a.num, a.company, trans1.name , c.num, c.company FROM route a JOIN route b ON (a.company = b.company AND a.num = b.num) JOIN ( route c JOIN route d ON (c.company = d.company

self join query

笑着哭i 提交于 2019-12-10 16:33:03
问题 Consider the following table: mysql> select * from phone_numbers; +-------------+------+-----------+ | number | type | person_id | +-------------+------+-----------+ | 17182225465 | home | 1 | | 19172225465 | cell | 1 | | 12129876543 | home | 2 | | 13049876543 | cell | 2 | | 15064223454 | home | 3 | | 15064223454 | cell | 3 | | 18724356798 | home | 4 | | 19174335465 | cell | 5 | +-------------+------+-----------+ I'm trying to find those people who have home phones but not cells. This query

MYSQL: Avoiding cartesian product of repeating records when self-joining

一笑奈何 提交于 2019-12-10 09:31:58
问题 There are two tables: table A and table B. They have the same columns and the data is practically identical . They both have auto-incremented IDs, the only difference between the two is that they have different IDs for the same records. Among the columns, there is an IDENTIFIER column which is not unique , i.e. there are (very few) records with the same IDENTIFIER in both tables. Now, in order to find a correspondence between the IDs of table A and the IDs of table B, I have to join these two

Mysql Self Join to find a parent child relationship in the same table

混江龙づ霸主 提交于 2019-12-09 21:29:05
问题 Im trying to calculate the amount of money won by all the offspring of a male race horse (Sire) over a time period. Listed by the Sire with the most amount of money won. I run the query and get the result Im after with one problem, I cant display the sires name, only their ID. SELECT `horses`.`SireID` AS `SireID` , `horses`.`HorseName` AS `Sire Name`, COUNT( `runs`.`HorsesID` ) AS `Runs` , COUNT( CASE WHEN `runs`.`Finish` =1 THEN 1 ELSE NULL END ) AS `Wins` , CONCAT( FORMAT( ( COUNT( CASE

how do I add a foreign key pointing to the same table using phpMyAdmin?

南楼画角 提交于 2019-12-09 16:55:21
问题 I have an existing InnoDB table which already has foreign keys pointing to different tables. But when I try to create a foreign key pointing to the Primary index, I get an error (check data type). The table is User with User_Id as the Primary. I want a foreign key Manager_ID which is a FK to User_Id. Both of INT Both of Length 10 Unsigned... But I still get a data check error...? 回答1: Make sure that Manager_ID is not set to NOT NULL . You have to allow nulls on that field, as the top-most

INNER JOIN same table

一世执手 提交于 2019-12-08 23:42:32
问题 I am trying to get some rows from the same table. It's a user table: user has user_id and user_parent_id . I need to get the user_id row and user_parent_id row. I have coded something like this: SELECT user.user_fname, user.user_lname FROM users as user INNER JOIN users AS parent ON parent.user_parent_id = user.user_id WHERE user.user_id = $_GET[id] But it doesn't show the results. I want to display user record and its parent record. 回答1: I think the problem is in your JOIN condition. SELECT

Select rows in a self-relationship where all children meet a condition

本秂侑毒 提交于 2019-12-08 11:18:58
问题 I am working with a single table (called documents ) with the following fields: id , parent_id , and status . The parent_id field refers to the id field in the same table. The status field is of type ENUM('submitted', 'accepted', 'rejected') . I would like to select all documents that have no children where status = 'accepted' . My first attempt looked like this: SELECT DISTINCT `documents`.* FROM (`documents`) LEFT OUTER JOIN `documents` children_documents ON `documents`.`id` = `children