本文介绍了我如何在t-sql 2008中获得以下输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有一个要求就像编写递归查询一样。在先前的功能连接的帮助下,在oracle 10g中解决了同样的问题。 这里我将发布我的表结构与预期的输出。 CREATE TABLE PROJWBS ( wbs_id int , parent_wbs_id int , wbs_name varchar ( 100 ), Proj_id int ) INSERT INTO PROJWBS VALUES ( 1000 , 0 ,' A', 1234 ) INSERT INTO PROJWBS VAL UES ( 1001 , 1000 ,' A.1', 1234 ) INSERT INTO PROJWBS VALUES ( 1002 , 1000 ,' B.1' , 1234 ) CREATE TABLE 任务( task_id int , task_code varchar ( 100 ), Start_date1 datetime , End_Date datetime , wbs_id int ) INSERT INTO 任务 VALUES ( 10023 ,' A1010',' 01-12-2012',' 03-25-2013', 1001 ) INSERT INTO 任务 VALUES ( 10025 ,' A1020',' 01-01-2012',' 03-10-2013', 1001 ) INSERT INTO 任务 VALUES ( 10026 ,' B1200',' 09-08-2012',' 04-30-2013', 1002 ) INSERT INTO 任务 VALUES ( 10027 ,' B1201',' 01-12-2012', ' 01-01-2013', 1002 ) 选择 * 来自 projwb 选择 * 来自任务 我的预期输出是: wbs_id wbs_anme sta rt_date End_Date 1000 A 1-Jan-12 30-Apr-13 1001 A.1 1-Jan-12 25-Mar-13 1002 B.1 8-Sep-12 30 -Apr-13 解决方案 正如其他人提到的那样A的结果不清楚。 假设这是一个错误你需要的是: 选择 wbs_id,wbs_name,startdate1 为 startdate,end_date 来自 projwbs 内部 加入任务 projwbs.wbs_id = task.wbs_id 希望这可以帮助你 我终于解决了这个问题。 i做了以下代码 wit h q as ( select wbs_id root_wbs_id,wbs_name root_wbs_name ,wbs_id, 0 级别 来自 projwbs union 全部 选择 q.root_wbs_id,q.root_wbs_name, p.wbs_id,q.level + 1 来自 projwbs p join q p.parent_wbs_id = q.wbs_id ) 选择 q.root_wbs_id wsb_id, q.root_wbs_name wbs_name, min(start_date1)start_date, max(end_date)end_date 来自 TASK t join q on q.wbs_id = t.wbs_id gro上 q.root_wbs_id,q.root_wbs_name Hi ,i got one requirement like to write the recursive query. the same issue is resolved in oracle 10g with the help of connect by prior functionality.here i am going to post my table structures with expected out put.CREATE TABLE PROJWBS(wbs_id int,parent_wbs_id int,wbs_name varchar(100),Proj_id int)INSERT INTO PROJWBS VALUES(1000,0,'A',1234)INSERT INTO PROJWBS VALUES(1001,1000,'A.1',1234)INSERT INTO PROJWBS VALUES(1002,1000,'B.1',1234)CREATE TABLE TASK(task_id int,task_code varchar(100),Start_date1 datetime,End_Date datetime,wbs_id int)INSERT INTO TASK VALUES(10023,'A1010','01-12-2012','03-25-2013',1001)INSERT INTO TASK VALUES(10025,'A1020','01-01-2012','03-10-2013',1001)INSERT INTO TASK VALUES(10026,'B1200','09-08-2012','04-30-2013',1002)INSERT INTO TASK VALUES(10027,'B1201','01-12-2012','01-01-2013',1002)select * from projwbselect * from taskmy expected output is: wbs_id wbs_anme start_date End_Date1000 A 1-Jan-12 30-Apr-131001 A.1 1-Jan-12 25-Mar-131002 B.1 8-Sep-12 30-Apr-13 解决方案 Hi,As others have mentioned the result for A is unclear.Assuming this is an error what you need to to is:Select wbs_id,wbs_name,startdate1 as startdate, end_datefrom projwbs inner join task on projwbs.wbs_id=task.wbs_idHope this helps youhi finally i resolved the issue my self.i did the following codewith q as( select wbs_id root_wbs_id, wbs_name root_wbs_name, wbs_id, 0 level from projwbs union all select q.root_wbs_id, q.root_wbs_name, p.wbs_id, q.level+1 from projwbs p join q on p.parent_wbs_id = q.wbs_id)select q.root_wbs_id wsb_id, q.root_wbs_name wbs_name, min(start_date1) start_date, max(end_date) end_datefrom TASK tjoin q on q.wbs_id = t.wbs_idgroup by q.root_wbs_id, q.root_wbs_name 这篇关于我如何在t-sql 2008中获得以下输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-18 10:19