强类型数据集中子查询的计算

时间:2010-08-27 14:38:47

标签: .net sql sql-server subquery strongly-typed-dataset

以下背景:我有一个表值函数,它返回一个表,它是一个大型表上的一种过滤视图(视图不能参数化)。现在我需要在不同条件下的许多行计数值。是否可以在一个查询中获取所有计数值,而无需为每个条件查询此函数?

为了完整起见(它真的很重要,你可以阅读它),这是功能:

CREATE FUNCTION [dbo].[getRuleConfigBySetSlsRules] (
 @fiRuleSet AS TinyInt,
 @fiServiceLocations AS NVARCHAR(4000),
 @fiRules AS NVARCHAR(400)
)

RETURNS TABLE AS RETURN  (
 SELECT     ruleChangeTo.fiSet, ruleChangeTo.fiClaimStatus, ruleChangeTo.fiRule, ruleChangeTo.fiSL, ruleChangeTo.fiResult, 
                      dimClaimStatus.ClaimStatusName AS OldStatus, dimClaimStatus_1.ClaimStatusName AS NewStatus, locSL.SLName, ruleSet.SetName, 
                      ruleResult.ResultName, locMarketUnit.MarketUnitName, locCountry.CountryName, ruleRule.RuleKey, ruleRule.RuleName, ruleRule.RuleDescription, 
                      locGSP.WCMSName AS GSP_WCMSName, locGSP.Active AS GSP_Active, locGSP.WCMSKeyNumber AS GSP_WCMSKeyNumber, 
                      locSL.Active AS SL_Active, locSL.WCMSName AS SL_WCMSName, locSL.WCMSKeyNumber AS SL_WCMSKeyNumber, locSL.fiSLType, 
                      locSLType.SLTypeName, ruleChangeTo.fiClaimStatus_ChangeTo
FROM         locGSP INNER JOIN
                      locCountry ON locGSP.fiCountry = locCountry.idCountry INNER JOIN
                      locMarketUnit ON locCountry.fiMarketUnit = locMarketUnit.idMarketUnit INNER JOIN
                      locSL ON locGSP.idGSP = locSL.fiGSP INNER JOIN
                      ruleChangeTo ON locSL.idSL = ruleChangeTo.fiSL INNER JOIN
                      dimClaimStatus ON ruleChangeTo.fiClaimStatus = dimClaimStatus.idClaimStatus INNER JOIN
                      ruleResult ON ruleChangeTo.fiResult = ruleResult.idResult INNER JOIN
                      ruleRule ON ruleChangeTo.fiRule = ruleRule.idRule INNER JOIN
                      ruleSet ON ruleChangeTo.fiSet = ruleSet.idSet INNER JOIN
                      locSLType ON locSL.fiSLType = locSLType.idSLType INNER JOIN
                      dimClaimStatus AS dimClaimStatus_1 ON ruleChangeTo.fiClaimStatus_ChangeTo = dimClaimStatus_1.idClaimStatus  INNER JOIN
                       dbo.Split(@fiServiceLocations, ',') AS ServiceLocations ON ServiceLocations.Item = ruleChangeTo.fiSL INNER JOIN
                      dbo.Split(@fiRules, ',') AS Rules ON ruleChangeTo.fiRule = Rules.Item
WHERE (ruleChangeTo.fiSet = @fiRuleSet)
)

通过以下方式,我可以得到一个计数值,但实际上我需要在同一个查询上有30个条件(WHERE子句)。

alt text

在没有针对每个值的一个查询的强类型数据集中实现此目的的最佳方法是什么?我不明白。 这是必须使用各种状态条件及其计数值扩展(在上图之外)的查询:

SELECT     COUNT(*) AS CountValidReject
FROM         dbo.getRuleConfigBySetSlsRules(@fiSet, @ServiceLocations, @Rules) AS RuleConfigFilter
WHERE     (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 8)

要说明一点:我是否需要带有临时表或类似内容的存储过程,还是可以在数据集中创建和查询此SQL中的临时表?我不想为每个值调用 getRuleConfigBySetSlsRules

由于

修改: 这是最后一个查询,返回1行,其中有许多计数值作为列。再次感谢Steve给CASE WHEN提示。它非常有效。

SELECT  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 5) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleAppliesApprovedInvalid,
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 5) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleAppliesApprovedReject,
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 5) AND (fiClaimStatus_ChangeTo = 7) THEN 1 ELSE NULL END) AS RuleAppliesApprovedReview, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleAppliesEscalatedInvalid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleAppliesEscalatedReject, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 7) THEN 1 ELSE NULL END) AS RuleAppliesEscalatedReview, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 1) THEN 1 ELSE NULL END) AS RuleAppliesEscalatedValid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 8) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleAppliesRejectInvalid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 8) AND (fiClaimStatus_ChangeTo = 1) THEN 1 ELSE NULL END) AS RuleAppliesRejectValid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 7) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleAppliesReviewInvalid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 7) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleAppliesReviewReject, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 7) AND (fiClaimStatus_ChangeTo = 1) THEN 1 ELSE NULL END) AS RuleAppliesReviewValid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 5) THEN 1 ELSE NULL END) AS RuleAppliesValidApproved, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleAppliesValidInvalid, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleAppliesValidReject, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 7) THEN 1 ELSE NULL END) AS RuleAppliesValidReview, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 5) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleApplNotApprovedInvalid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 5) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleApplNotApprovedReject, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 5) AND (fiClaimStatus_ChangeTo = 7) THEN 1 ELSE NULL END) AS RuleApplNotApprovedReview, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleApplNotEscalatedInvalid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleApplNotEscalatedReject, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 7) THEN 1 ELSE NULL END) AS RuleApplNotEscalatedReview, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 9) AND (fiClaimStatus_ChangeTo = 1) THEN 1 ELSE NULL END) AS RuleApplNotEscalatedValid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 8) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleApplNotRejectInvalid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 8) AND (fiClaimStatus_ChangeTo = 1) THEN 1 ELSE NULL END) AS RuleApplNotRejectValid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 7) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleApplNotReviewInvalid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 7) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleApplNotReviewReject, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 7) AND (fiClaimStatus_ChangeTo = 1) THEN 1 ELSE NULL END) AS RuleApplNotReviewValid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 5) THEN 1 ELSE NULL END) AS RuleApplNotValidApproved, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 2) THEN 1 ELSE NULL END) AS RuleApplNotValidInvalid, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 8) THEN 1 ELSE NULL END) AS RuleApplNotValidReject, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 7) THEN 1 ELSE NULL END) AS RuleApplNotValidReview, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 1) THEN 1 ELSE NULL END) AS AllValidApplies, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 1) THEN 1 ELSE NULL END) AS AllValidNotApplies, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 5) THEN 1 ELSE NULL END) AS AllApprovedApplies,          
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 5) THEN 1 ELSE NULL END) AS AllApprovedNotApplies, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 7) THEN 1 ELSE NULL END) AS AllReviewApplies, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 7) THEN 1 ELSE NULL END) AS AllReviewNotApplies, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 7) THEN 1 ELSE NULL END) AS AllRejectApplies, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 7) THEN 1 ELSE NULL END) AS AllRejectNotApplies, 
  COUNT(CASE WHEN (fiResult = 1) AND (fiClaimStatus = 9) THEN 1 ELSE NULL END) AS AllEscalatedApplies, 
  COUNT(CASE WHEN (fiResult = 2) AND (fiClaimStatus = 9) THEN 1 ELSE NULL END) AS AllEscalatedNotApplies
FROM         dbo.getRuleConfigBySlsRules(@ServiceLocations, @Rules) AS RuleConfig
WHERE     (fiSet = @fiSet)

2 个答案:

答案 0 :(得分:3)

如果您更具体并举例说明,这会有很大帮助。我不确定同一查询(WHERE子句)的“30条件”是什么意思或“强类型”与任何事情有什么关系。你能给出你想要的30件事中的两三个例子吗?

猜测你想要的东西是这样的:

SELECT
  COUNT(CASE WHEN (fiClaimStatus = 1) AND (fiClaimStatus_ChangeTo = 8)
        THEN 1 ELSE NULL END) AS myCount1,
  COUNT(CASE WHEN (fiClaimStatus = 2) AND (fiClaimStatus_ChangeTo = 8)
        THEN 1 ELSE NULL END) AS myCount2,
  COUNT(CASE WHEN (fiClaimStatus = 2) AND (fiClaimStatus_ChangeTo = 6)
        THEN 1 ELSE NULL END) AS myCount1,
  ...
FROM dbo.getRuleConfigBySetSlsRules(@fiSet, @ServiceLocations, @Rules) AS T

如果这不符合您的需求,请提供更多信息。

答案 1 :(得分:0)

我没有看到任何解决方法。将表值函数的结果放入临时表中,然后运行聚合查询。