用于识别仓库级别的递归查询

时间:2014-11-13 18:31:19

标签: sql sql-server sql-server-2008 select sql-server-2008-r2

Sql Server 2008

我有以下表格

location_tbl

locid   locname
-----------------
019136  Hyderabad
119137  Vijayawada
219137  Mumbai
319137  Bangalore

warehouse_tbl

wh_id   whname               whaddr                    whdesc   locid
-----------------------------------------------------------------------------    
020143  Attapur Branch       Attapur, Hyderabad        Desc     019136
120149  Medchal Warehouse    Medchal, Hyderabad        Desc     019136
2221144 Hardware Store       Store, Mumbai             Desc     219137

WH_Locations

whLocId  whLocName      whLocDesc   parentId    wh_id
------------------------------------------------------
149     WHS1                        0            020143
150     Sec1                        149          020143
151     Sec2                        149          020143
152     Slot1                       149          020143
154     Sec2Sub1                    151          020143
155     Warehouse1                  0            120149
157     Warehouse2                  0            120149
158     Store1                      157          120149
159     Store2                      157          120149
161     WHS2                        0            2221144
152     Sec2Sub2                    154          020143

预期结果

whLocId    whname   WH_Name           Sec_Name1  Sec_Name2  Sec_Name3  Sec_Name4
----------------------------------------------------------------------------
149     Hyderabad   Attapur Branch    WHS1        NULL      NULL         NULL        
155     Hyderabad   Medchal Warehouse Warehouse1  NULL      NULL         NULL
157     Hyderabad   Medchal Warehouse Warehouse2  NULL      NULL         NULL
161     Mumbai      Hardware Store    WHS2        NULL      NULL         NULL
152     Hyderabad   Attapur Branch    WHS1        Slot1     NULL         NULL
154     Hyderabad   Attapur Branch    WHS1        Sec2    Sec2Sub1 NULL                         
158     Hyderabad   Medchal Warehouse Warehouse2  Store2    NULL         NULL
159     Hyderabad   Medchal Warehouse Warehouse2  Store2    NULL         NULL         
150     Hyderabad   Attapur Branch    WHS1        Sec1      NULL         NULL         
151     Hyderabad   Attapur Branch    WHS1        Sec2      NULL         NULL 
154     Hyderabad   Attapur Branch    WHS1        Sec2    Sec2Subsub1  Sec2Sub2         

尝试过像

这样的事情
`SELECT    distinct c.whLocId ,a.locname, b.whname, d.whLocName AS Sec_name1,   c.whLocName     AS Sec_Name2,
e.whLocName AS Sec_Name3,f.whLocName AS Sec_Name4
FROM         dbo.location_tbl AS a INNER JOIN
                  dbo.warehouse_tbl AS b ON a.locid = b.locid INNER JOIN
                  dbo.WH_Locations AS c ON b.wh_id = c.wh_id INNER JOIN
                  dbo.WH_Locations AS d ON c.parentId = d.whLocId inner join
                  dbo.WH_Locations AS e on d.whlocid= e.parentid INNER JOIN
 dbo.WH_Locations AS f on f.whlocid= e.whlocid 
`

但是没有工作......在绑定前两列而不是重新绑定的列

时取得了一些成功

列名也应该被输出绑定我的意思是最后一级。在级别结束之后还需要保持null,直到最后一级。

任何帮助,提示都会有所帮助

由于

让我知道Font等的任何问题都会编辑它..

0 个答案:

没有答案