选择变量时查找计数

时间:2014-06-26 12:44:32

标签: sql sql-server tsql

    DECLARE @tmDate datetime
       ,@nBranchId int

SELECT  @tmDate = GETDATE()
       ,@nBranchId = 3483


SELECT  strMessage = CASE WHEN xpr.fRevaluationPrice <> d.fUpplupenRanta THEN
                    d.strInstrument +' in ' + f.strShortName + ' has not been saved. it currently has incorrect fixing in XP_Results: ' + CONVERT(varchar, xpr.fRevaluationPrice) + ' should be: ' + CONVERT(varchar, d.fUpplupenRanta)
                    ELSE ''
                    END     
FROM    dbo.Deals AS d
INNER JOIN dbo.XP_Results AS xpr ON 1=1
    AND xpr.strPaperId = CONVERT(varchar, d.nTransactionId)
    AND xpr.nInstrId = d.nInstrId
    AND xpr.tmDate = CONVERT(datetime, d.strMotpartKIkod) --'20140404'
INNER JOIN dbo.Folders AS f ON 1=1
    AND f.nId = d.nFolderId
WHERE 1=1
and d.nBranchId = @nBranchId
AND d.nInstrId = 11
AND d.nInternalTrans = 3
AND d.nIndex = 1

是否有可能以某种方式检查CASE中THEN和ELSE的计数? 现在的问题是选择了大约一千行并且它们都混淆了,我不知道每个行中有多少行

1 个答案:

答案 0 :(得分:1)

你可以把你的案子放在这样的计数中

SELECT COUNT(CASE WHEN xpr.fRevaluationPrice <> d.fUpplupenRanta THEN 1 ELSE null END)
SELECT COUNT(CASE WHEN xpr.fRevaluationPrice <> d.fUpplupenRanta THEN null ELSE 1 END)
FROM...