0

我将
Table1 (profileName,asset status,asset_Name,BaseName)
Table 2(TITLE,Role etc)Title 链接到 table1 并使用配置文件名称
Table 3(Base_Name,Role_Id)Base 链接到 table1 并使用 BaseName
Table 4(Role ID,Rolename)角色 ID 链接到 table3 并使用 Role_Id)

现在我想从表 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

任何帮助,将不胜感激。

4

1 回答 1

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子句,因为没有执行聚合函数

于 2012-12-24T16:52:02.163 回答