0

我正在尝试为符合以下 ID(626、1985、2511)的所有申请人打印所有规则。

它打印具有 626 作为第一条规则的申请人,但不打印只有 1985 和 2511 而没有 626 的申请人)。任何想法?

select DISTINCT AD1.AppID,

MAX(CASE WHEN (APDR1.SETTINGID= 626) THEN APDR1.reviewruletext    ELSE ' ' END)AS 'Rule1',
MAX(CASE WHEN (APDR2.SETTINGID= 1985)THEN APDR2.reviewruletext    ELSE ' ' END)AS 'Rule2', 
MAX(CASE WHEN (APDR8.SETTINGID= 2511) THEN APDR8.reviewruletext    ELSE ' ' END)AS 'Rule8' 


from  appdetail  ad1

LEFT OUTER JOIN AppPartyDetailRule   APDR1 on
  apdr1.AppID=ad1.AppID AND 
 apdr1.AppDetailID= ad1.AppDetailID and
 apdr1.AppDetailSavePointID= AD1.AppDetailSavePointID AND
 ad1.AppDetailSavePointID=0 AND
 APDR1.SettingID= 626 



LEFT OUTER JOIN AppPartyDetailRule APDR2on
 ad1.appid= apdr2.appid and 
 ad1.appdetailID= apdr2.appdetailID and 
 ad1.appdetailsavepointid= apdr2.appdetailsavepointid and
 ad1.appdetailsavepointid= 0 AND
 APDR2.SettingID= 1985


LEFT OUTER JOIN AppPartyDetailRule APDR8 on
 ad1.appid= apdr8.appid and 
 ad1.appdetailID= apdr8.appdetailID and 
 ad1.appdetailsavepointid= apdr8.appdetailsavepointid and
 ad1.appdetailsavepointid= 0 AND
 APDR8.SettingID= 2511 


 group by AD1.AppID,....  etc. (more fields)

表结构为:
AppID、AppDetailID、AppDetailsavepointID 构成 AD1 表中的主键,该表使用上面的 PK 链接到 APDR 几次,每次它为 settingID 提取不同的值。

期望的输出是:AppID 审查规则文本1,审查规则文本2,审查规则文本8

review Rule Text1代表626,2代表1985,8代表2511,每个代表某条规则

4

1 回答 1

1

我认为您可以将查询简化为:

select AD1.AppID,
       MAX(CASE WHEN (APDR1.SETTINGID = 626) THEN APDR1.reviewruletext ELSE ''
           END) AS 'Rule1',
       MAX(CASE WHEN (APDR1.SETTINGID = 1985) THEN APDR1.reviewruletext ELSE ''
           END) AS 'Rule2',  
       MAX(CASE WHEN (APDR1.SETTINGID = 2511) THEN APDR1.reviewruletext ELSE ''
           END) AS 'Rule8'
from appdetail ad1 LEFT OUTER JOIN
     AppPartyDetailRule APDR1
     on apdr1.AppID=ad1.AppID AND 
        apdr1.AppDetailID= ad1.AppDetailID and
        apdr1.AppDetailSavePointID= AD1.AppDetailSavePointID AND
        ad1.AppDetailSavePointID=0
group by ad1.appID
where APDR1.SettingID in (626, 1985, 2511)
于 2013-02-20T22:05:16.613 回答