t-sql视图中的Case语句返回错误的记录数

时间:2014-09-18 15:42:45

标签: sql sql-server-2008

当我使用case语句在字段中创建数据时,我有一个返回错误记录数的视图。

SELECT a.[Line Id]

返回正确的记录数。

我希望能够使用以下case语句,但它返回错误的记录数

SELECT 
    CASE 
       WHEN a.[Line Id] LIKE '%FIX%' 
         THEN 'Fixed'
         ELSE 'Variable'  
    END AS 'Fixed/Variable'

我有条不紊地删除了视图的其他部分,并确定这是混乱的地方。这种行为有解释吗?

提前致谢。

CREATE VIEW [dbo].[vNew File]
AS
   SELECT distinct 
      [Year]
      ,a.[Co #] + '-LO' + a.[Organization] as 'Receiver'
      ,CASE 
          WHEN a.[Line Id] LIKE '%FIX%' 
            THEN 'Fixed'
            ELSE 'Variable'  
       END AS 'Fixed/Variable'
      ,CASE 
          WHEN RTRIM(LTRIM([Line ID])) = 'SYSXXXXVVSCHG' 
            THEN 'IA-100000'
          WHEN RTRIM(LTRIM([Line ID])) = 'SYSXXXXFIXCHG' 
            THEN 'IA-100040'
          WHEN RTRIM(LTRIM([Line ID])) = 'SYSXXXRFIXCHG' 
            THEN 'IA-031061'
          WHEN RTRIM(LTRIM([Line ID])) = 'SYSXXXUFIXCHG' 
            THEN 'IA-100020'
          WHEN RTRIM(LTRIM([Line ID])) = 'SYSXXXUVVSCHG' 
            THEN 'IA-100010'
          WHEN LEN(a.Activity) > 1 
            THEN 'IA-' + LTRIM(RTRIM(a.Activity))
          ELSE
            'IA-' + LTRIM(RTRIM(b.ActivityNumber))
       end AS 'Activity'
      , CASE 
                WHEN a.[A] = 'A' THEN 'Actual' 
            ELSE 
                'Budget' 
            END 
            AS 'Dataset'
            ,'Dollar' as Value
            , convert(float,a.Jan) as 'Jan'
            , convert(float,a.Feb) as 'Feb'
            , convert(float,a.Mar) as 'Mar'
            , convert(float,a.Apr) as 'Apr'
            , convert(float,a.May) as 'May'
            , convert(float,a.Jun) as 'Jun'
            , convert(float,a.Jul) as 'Jul'
            , convert(float,a.Aug) as 'Aug'
            , convert(float,a.Sep) as 'Sep'
            , convert(float,a.Oct) as 'Oct'
            , convert(float,a.Nov) as 'Nov'
            , convert(float,a.Dec) as 'Dec'
            ,case
                When len(a.[Activity]) < 1 then a.Company + '-LP' + a.[Cost Center]
                when a.[Activity] = '700148' then '00001-LPXXXXCH.9999' --+ b.[OrgUnit]
            ELSE
                 '00001-LPXXXXNFT.9999'
            END 
            as  'Provider'
   FROM 
       [DPEs].[dbo].[April YTD New DPE File Actuals] a 
   JOIN 
       DPEs.dbo.SBUToActivity b ON LTRIM(RTRIM(a.[Cost Center])) = LTRIM(RTRIM(b.[OrgUnit]))
   WHERE  
       [YEAR] = 2014
    GO

0 个答案:

没有答案