视图返回不同的行数,具体取决于我从中选择的内容

时间:2014-12-05 04:53:44

标签: tsql

我遇到以下两种查询生成不同行数的情况:

SELECT *
FROM [some view that uses group by]
WHERE [_Account]='acc1' AND [_TradeDate]='2014-08-11'

SELECT field1
FROM [some view that uses group by]
WHERE [_Account]='acc1' AND [_TradeDate]='2014-08-11'

请注意,唯一的区别是我选择的是field1而不是* 怎么可能呢?

以下是[some view that uses group by]的定义:

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [some view that uses group by]
AS
    WITH stage1 AS (
        SELECT
            p.XMLRecordInternalID,
            p.ExpandTaskExecutionGUID AS _ExpandTaskExecutionGUID,
            CONVERT(VARCHAR(100),RTRIM(p.AccountNumber)) _Account,  
            CONVERT(DATE,ISNULL(i.ResolvedDate,p.PositionTradeDate)) _TradeDate,
            Norma.svf_TryToOSISymbol(
                COALESCE(
                    (SELECT Symbol FROM Norma.[Input_PEN,InstrumentAliases] WHERE Alias=LTRIM(RTRIM(p.SecuritySymbol))),
                    LTRIM(RTRIM(p.SecuritySymbol))
                ) 
            ) AS _Instrument,
            CONVERT(MONEY,p.TradeQuantity) AS _Q,
            CONVERT(MONEY,p.ClosingPrice) AS _ClosingPrice,

            ROW_NUMBER() OVER (
                PARTITION BY 
                    p.AccountNumber, 
                    p.SecuritySymbol, 
                    CONVERT(DATE, ISNULL(i.ResolvedDate,p.PositionTradeDate)), 
                    p.TradeQuantity, 
                    p.ClosingPrice, 
                    p.AccountType 
                ORDER BY CONVERT(DATE, ISNULL(i.ResolvedDate,p.PositionTradeDate))
            ) DupCount,

            p.PositionTradeDate,
            p.OfficeCode,
            p.CorrespondentCode,
            p.AccountNumber,
            p.AccountType,
            p.CurrencyCode,
            p.SecuritySymbol,
            p.SecurityCode,
            p.ClosingPrice,
            p.TradeQuantity
        FROM Norma.[Expanded_Apex Positions VSEC] p
        LEFT OUTER JOIN ClearingFiles.XMLRecord x
        ON x.XMLRecordInternalID = p.XMLRecordInternalID
        LEFT OUTER JOIN ClearingFiles.DownloadedFileInfo i
        ON i.DownloadedFileInfoInternalID = x.DownloadedFileInfoInternalID
    ), stage2 AS (
        SELECT  _ExpandTaskExecutionGUID,
                [_Account] ,
                CONVERT(BIT, CASE WHEN DupCount=1 THEN 1 ELSE 0 END ) AS _Included,
                CONVERT(VARCHAR(MAX), 
                    CASE 
                        WHEN DupCount<>1 THEN 'Duplicate'
                        ELSE NULL
                    END
                ) AS _ExcludeReason,
                [_TradeDate] ,
                [_Instrument] ,
                [_Q] ,
                [_ClosingPrice],
                DupCount,
                PositionTradeDate ,
                OfficeCode ,
                CorrespondentCode ,
                AccountNumber ,
                AccountType ,
                CurrencyCode ,
                SecuritySymbol ,
                SecurityCode ,
                ClosingPrice ,
                TradeQuantity 
        FROM stage1
    )
    SELECT  [_ExpandTaskExecutionGUID],
            [_Account],
            [_Included],
            [_TradeDate],
            [_Instrument],
            SUM([_Q]) AS [_Q],
            [_ClosingPrice],
            MAX([_ExcludeReason]) _ExcludeReason,

            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(DupCount) END DupCount,
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(PositionTradeDate) END PositionTradeDate, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(OfficeCode) END OfficeCode, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(CorrespondentCode) END CorrespondentCode, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(AccountNumber) END AccountNumber, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(AccountType) END AccountType, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(CurrencyCode) END CurrencyCode, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(SecuritySymbol) END SecuritySymbol, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(SecurityCode) END SecurityCode, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(ClosingPrice) END ClosingPrice, 
            CASE WHEN COUNT(*)>1 THEN NULL ELSE MAX(TradeQuantity) END TradeQuantity 

    FROM stage2
    GROUP BY [_ExpandTaskExecutionGUID],
        [_Account],
        [_Included],
        [_TradeDate],
        [_Instrument],
        [_ClosingPrice]
GO

0 个答案:

没有答案