0

示例 CatTerm_id = 613

SELECT
     CatalogTerm_Id
    ,CatalogResolution_Id
    ,Catalogresolution.name
    ,CatalogRegulation.Threshold
    ,OptionItem.name
    ,ApprovalType =
          CASE
               WHEN OptionItem.name LIKE 'Corporate' THEN 1
               WHEN OptionItem.name LIKE 'GEO' THEN 2
               WHEN OptionItem.name LIKE 'Country/Area' THEN 3 ELSE 4
          END
FROM CatalogResolution, CatalogRegulation, OptionItem
WHERE CatalogTerm_Id = 613
     AND CatalogResolution.ID = CatalogResolution_id
     AND OptionItem.ID = CatalogRegulation.Level_Id
     AND type_id IN (645)
     AND CatalogResolution.IsDeleted = 0
     AND CatalogRegulation.IsDeleted = 0
ORDER BY CatalogTerm_Id

输出 :

613 1051

Country/Area    3

613 1053

Geo 2

613 1054

     Country/Area   3

输出:国家/地区。

但是显示器应该显示 GEO,然后我可以将它扩展到整个列表而不指定 CatTerm_id

SELECT TOP 1
     CatalogTerm_Id
    ,CatalogResolution_Id
    ,Catalogresolution.name
    ,CatalogRegulation.Threshold
    ,OptionItem.name
    ,ApprovalType =
          CASE
               WHEN OptionItem.name LIKE 'Corporate' THEN 1
               WHEN OptionItem.name LIKE 'GEO' THEN 1
               WHEN OptionItem.name LIKE 'Country/Area' THEN 1 ELSE 4
          END
FROM CatalogResolution, CatalogRegulation, OptionItem
WHERE CatalogTerm_Id = 613
     AND CatalogResolution.ID = CatalogResolution_id
     AND OptionItem.ID = CatalogRegulation.Level_Id
     AND type_id IN (645)
     AND CatalogResolution.IsDeleted = 0
     AND CatalogRegulation.IsDeleted = 0
ORDER BY CatalogTerm_Id

613 1051
国家/地区 1

这不是我想要的我想要的“GEO”

4

3 回答 3

0

最后我破解了它:)

;WITH  ApprovalLevel ( CatalogTerm_Id, 
CatalogResolution_Id, 
Name, 
ApprovalLevel)
AS
( SELECT CatalogTerm_Id, 
CatalogResolution_Id, 
OptionItem.Name,
RANK () Over (PARTITION By Catalogterm_id
                                Order By CatalogTerm_Id, 
                                         CatalogRegulation.Level_Id DESC )
                                As ApprovalLevel
                                            From CatalogResolution, 
                                                 CatalogRegulation,
                                                 OptionItem
                         where   
                         CatalogResolution.Id = CatalogResolution_id
                         AND OptionItem.Id = CatalogRegulation.Level_Id 
                         And Type_Id In (645)
                         AND CatalogResolution.IsDeleted = 0 
                         And CatalogRegulation.IsDeleted = 0
                                     )
                        (Select CatalogTerm_Id, CatalogResolution_Id, Name 
                        From ApprovalLevel
                        Where ApprovalLevel = 1 )
于 2013-07-25T08:43:37.193 回答
0

尝试按如下方式添加 order by 子句,然后从结果中选择前 1

按案例排序 ApprovalType
当 '公司' THEN 1
当 'GEO' THEN 1
当'国家/地区' THEN 1
结尾

于 2013-07-25T08:45:23.687 回答
0
Select  Name
FROM ( SELECT catalogterm_id,
OptionItem.Name,
Row_Number () Over (PARTITION By Catalogterm_id 
Order By CatalogTerm_Id, CatalogRegulation.Level_Id DESC )  
        As Approval
           FROM CatalogResolution, CatalogRegulation,  OptionItem 
 WHERE 
CatalogResolution.Id = CatalogResolution_id
AND OptionItem.Id = CatalogRegulation.Level_Id 
And Type_Id In (645)
AND CatalogResolution.IsDeleted = 0 
And CatalogRegulation.IsDeleted = 0 
                                ) Approval
                                                         WHERE Approval = 1                    And catalogterm_id = Cat.id ) 
                   AS [Approval Level]
于 2014-02-25T16:16:12.450 回答