连接四个表以从左表中获取数据

时间:2012-12-24 16:30:42

标签: sql

我有 Table1 (profileName,asset status,asset_Name,BaseName)
Table 2(TITLE,Role etc)标题链接到table1,其中包含个人资料名称
Table 3(Base_Name,Role_Id) Base链接到table1,BaseName为 Table 4(Role ID,Rolename)角色ID通过Role_Id链接到table3

现在我想从表1中获取有关Role的数据。 例如

Table 1
Test-POD    AVAILABLE FOR SERVICE   XYLENE          Test-Base
Test-N      AVAILABLE FOR SERVICE   X1              Test-Base
Test-N      AVAILABLE FOR SERVICE   X1              Test-Base
Test-N      AVAILABLE FOR SERVICE   X1              Test-Base
Test-POD    UNAVAILABLE FOR SERVICE XYLENE          Test-Base
Test-POD    UNAVAILABLE FOR SERVICE XYLENE          Test-Base
Test-POD    UNAVAILABLE FOR SERVICE XYLENE          Test-Base

表2(具有Test-POD数据)

Test-POD    Role1   
Test-POD    Role2

表3(具有Test-N的角色ID)

Test-Base    95D90BD1-6A31-4CB7-895A-1CDF3FBA428D(Uid)
Test-Base    95D90BD1-6A31-4CB7-895A-1CDF3FBA428D
Test-Base    95D90BD1-6A31-4CB7-895A-1CDF3FBA428D

表4

Test-Role           95D90BD1-6A31-4CB7-895A-1CDF3FBA428D
Test-Role-POD1      DCFA3F6B-066E-4C1E-858C-652520B9BABD
Test-Role-POD       EF3E9A85-E709-4CD4-8F04-8570B3CBC2CC

想要根据所选角色进行过滤。请在下面查询

    SELECT 
      distinct(COUNT(Exception.asset_status)) AS CountOfasset_status1, 
      Exception.asset_name, Exception.asset_description
    FROM 
      (((dbo.SATV_REP_EXCEPTION Exception 
      Left outer Join dbo.SATV_PROCEDURE_ROLE Proc_Role ON 
       (Exception.profile_name = Proc_Role.TITLE and 
       Proc_Role.ROLE_NAME='Test-Role-POD1'))
     Left Outer Join dbo.SATV_COR_SCHEDULE_OCCURRENCE Sch_Occ on 
       Sch_Occ.BASE_NAME=Exception.BASE_NAME)
     Left outer Join dbo.SATT_ROLES Roles ON 
       (Roles.ROLE_PK=Sch_Occ.ROLE_FK and 
       Roles.ROLE_NAME='Test-Role-POD1'))
    WHERE 
      (Exception.ITEM_DATE > GETDATE() - 90) AND 
      (Exception.asset_status <> 'IN SERVICE') 
      AND Exception.BASE_NAME='Test-Base' 
      --AND (Proc_Role.ROLE_NAME='Test-Role-POD1' or Roles.ROLE_NAME='Test-Role-POD1')
    GROUP BY 
      Exception.asset_name, Exception.asset_description,
      ITEM_DATE 
    HAVING 
      (Exception.asset_name IS NOT NULL) AND 
      (asset_name <> '') 
      AND (COUNT(Exception.asset_status) > 2) 
    ORDER BY 
      CountOfasset_status1 DESC

任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

不确定你要做什么,但你可能想要:

(COUNT(distinct e.asset_status) > 2) 

这会给你带来问题:

GROUP BY 
    e.asset_name, 
    e.asset_description,
    ITEM_DATE 

SELECT 
    DISTINCT(COUNT(e.asset_status)) AS CountOfasset_status1, 
    e.asset_name, 
    e.asset_description

您需要在select子句中加入item_date,或将其从group by子句中删除

是的,我把你的桌子别名了:

FROM 
    (((dbo.SATV_REP_EXCEPTION e 
    LEFT OUTER JOIN dbo.SATV_PROCEDURE_ROLE p ON 
        (e.profile_name = p.TITLE AND 
        p.ROLE_NAME='Test-Role-POD1'))
    LEFT OUTER JOIN dbo.SATV_COR_SCHEDULE_OCCURRENCE o ON 
        o.BASE_NAME=e.BASE_NAME)
    LEFT OUTER JOIN dbo.SATT_ROLES r ON 
        (r.ROLE_PK=Sch_Occ.ROLE_FK AND 
        r.ROLE_NAME='Test-Role-POD1'))

另外,这个东西:

HAVING 
    (e.asset_name IS NOT NULL) AND 
    (asset_name <> '')  AND 

可以进入where子句,因为没有执行聚合函数