SQL TOP 10使用IF在子查询中按计数排序

时间:2016-09-30 13:20:32

标签: sql sql-server

我查询了一些参数,并根据每次运行不同查询所需的参数。

例如:

 declare @daterangeint
 set @daterange = '7'

 If @date range !=0 (Select TOP 10 value,Count * from a where columna = ab group by value)
 ELSE (Select TOP 10 value,Count * from a where columnb = abcd group by value)

我需要为每个查询按降序排列前10个计数,否则会返回太多行。我知道你不能在查询和订单中使用order by似乎不能在它之外工作所以我将如何解决这个问题。

这是我的实际查询

declare @assignedto nvarchar(256)
declare @createdrange int

set @assignedto = 'user'
set @createdrange = '7'

IF (@createdrange != 0)  (
    SELECT TOP 10
        COUNT(sr.Id_9A505725_E2F2_447F_271B_9B9F4F0D190C) AS 'wicount',
        (u.Department_312201FE_C1B3_E95A_01DF_E132E9BD3EC9, 'No Department') as Department 
    FROM 
        MTV_System$WorkItem$Incident sr 
    LEFT JOIN 
        RelationshipView r ON r.SourceEntityId = sr.BaseManagedEntityId 
                           AND r.RelationshipTypeId = 'DFF9BE66-38B0-B6D6-6144-A412A3EBD4CE'   
                           AND r.IsDeleted ='0'
    LEFT JOIN  
        MTV_System$Domain$User u ON r.TargetEntityId = u.BaseManagedEntityId
    INNER JOIN 
        RelationshipView assr ON assr.SourceEntityId = sr.BaseManagedEntityId 
                              AND assr.RelationshipTypeId = '15E577A3-6BF9-6713-4EAC-BA5A5B7C4722'  
                              AND assr.IsDeleted ='0'
    INNER JOIN 
        MTV_System$Domain$User assu ON assr.TargetEntityId = assu.BaseManagedEntityId 
                                    AND assu.UPN_7641DFF7_7A20_DC04_FC1C_B6FA8715DA02 = @assignedto        
    WHERE  
        sr.ResolvedDate_D2A4C73F_01B8_29C5_895B_5BE4C3DFAC4E >= dateadd(day, datediff(day, 0, GetutcDate()) - @createdrange, 0) 
    GROUP BY 
        u.Department_312201FE_C1B3_E95A_01DF_E132E9BD3EC9
) ELSE (
    SELECT TOP 10
        COUNT(sr.Id_9A505725_E2F2_447F_271B_9B9F4F0D190C) AS 'wicount',
        COALESCE(u.Department_312201FE_C1B3_E95A_01DF_E132E9BD3EC9, 'No Department') as Department 
    FROM 
        MTV_System$WorkItem$Incident sr 
    LEFT JOIN 
        RelationshipView r ON r.SourceEntityId = sr.BaseManagedEntityId 
                           AND r.RelationshipTypeId = 'DFF9BE66-38B0-B6D6-6144-A412A3EBD4CE'  
                           AND r.IsDeleted ='0'
    LEFT JOIN  
        MTV_System$Domain$User u ON r.TargetEntityId = u.BaseManagedEntityId
    INNER JOIN 
        RelationshipView assr ON assr.SourceEntityId = sr.BaseManagedEntityId 
                              AND assr.RelationshipTypeId = '15E577A3-6BF9-6713-4EAC-BA5A5B7C4722'  
                              AND assr.IsDeleted ='0'
    INNER JOIN 
        MTV_System$Domain$User assu ON assr.TargetEntityId = assu.BaseManagedEntityId 
                                    AND assu.UPN_7641DFF7_7A20_DC04_FC1C_B6FA8715DA02 = @assignedto        
    WHERE 
        sr.Status_785407A9_729D_3A74_A383_575DB0CD50ED NOT IN ('2B8830B6-59F0-F574-9C2A-F4B4682F1681', 'BD0AE7C4-3315-2EB3-7933-82DFC482DBAF')
    GROUP BY
        u.Department_312201FE_C1B3_E95A_01DF_E132E9BD3EC9
)

2 个答案:

答案 0 :(得分:0)

包括要与其他where子句语句一起评估的过滤条件。

 Select TOP 10 value,Count * from a 
 where 
 (@daterange<>7 OR columna=abcd)
 AND
 (@daterange<>0 OR columna=ab)
 group by value

我更喜欢空值评估,因为它更容易理解。

INT @CreatedDateRange=NULL

 WHERE  
 (@CreatedDateRange IS NULL OR sr.ResolvedDate_D2A4C73F_01B8_29C5_895B_5BE4C3DFAC4E >=dateadd(day,datediff(day,0,GetutcDate())-@createdrange,0) 
 AND 
 (NOT @CreatedDateRange IS NULL OR sr.Status_785407A9_729D_3A74_A383_575DB0CD50ED NOT IN ('2B8830B6-59F0-F574-9C2A-F4B4682F1681','BD0AE7C4-3315-2EB3-7933-82DFC482DBAF')

答案 1 :(得分:0)

只需将切换条件添加到Where子句中,并只使用一个查询:

SELECT TOP 10 
  COUNT(sr.Id_9A505725_E2F2_447F_271B_9B9F4F0D190C) wicount,
       (u.Department_312201FE_C1B3_E95A_01DF_E132E9BD3EC9, 'No Department') Department 
FROM MTV_System$WorkItem$Incident sr 
LEFT JOIN RelationshipView r 
   ON r.SourceEntityId = sr.BaseManagedEntityId 
     AND r.RelationshipTypeId = 'DFF9BE66-38B0-B6D6-6144-A412A3EBD4CE' 
     AND  r.IsDeleted ='0'
LEFT JOIN  MTV_System$Domain$User u 
   ON r.TargetEntityId = u.BaseManagedEntityId
JOIN RelationshipView assr 
   ON assr.SourceEntityId = sr.BaseManagedEntityId 
      AND assr.RelationshipTypeId = '15E577A3-6BF9-6713-4EAC-BA5A5B7C4722'
      AND  assr.IsDeleted ='0'
JOIN MTV_System$Domain$User assu 
   ON assr.TargetEntityId = assu.BaseManagedEntityId 
     AND assu.UPN_7641DFF7_7A20_DC04_FC1C_B6FA8715DA02 = @assignedto        
WHERE (@createdrange = '7' And
      sr.ResolvedDate_D2A4C73F_01B8_29C5_895B_5BE4C3DFAC4E 
         >= dateadd(day,datediff(day,0,GetutcDate())-@createdrange,0)) Or
      (@createdrange != '7' And
      sr.Status_785407A9_729D_3A74_A383_575DB0CD50ED 
         NOT IN ('2B8830B6-59F0-F574-9C2A-F4B4682F1681',
                 'BD0AE7C4-3315-2EB3-7933-82DFC482DBAF'))
GROUP BY u.Department_312201FE_C1B3_E95A_01DF_E132E9BD3EC9
Order By [Whatever ordering you want to determine top 10]