使用案例和类似的陈述没有重复的结果

时间:2015-05-15 13:29:57

标签: sql sql-server sql-server-2000

我正在尝试使用case和like声明。当我运行以下查询时,它会在特定列上的某些行上复制结果。我正在使用SQL Server 2000。

SELECT DISTINCT [rjvn_pound_reference]
    ,t_reference
    ,t_street_name
    ,t_zone_name
    ,(
        CASE 
            WHEN rjvn_note LIKE '%CORRESPONDENCE%'
                THEN 'Y'
            ELSE 'N'
            END
        ) AS CorrespondenceReceived
    ,(
        CASE 
            WHEN rjvn_note LIKE '%review form complete%'
                THEN 'Y'
            ELSE 'N'
            END
        ) AS REVIEWFORMCOMPLETE
    ,(
        CASE 
            WHEN rjvn_note LIKE '%Manually issued NTO - Drive off%'
                THEN 'Y'
            ELSE 'N'
            END
        ) AS ManuallyissuedNTO
    ,(
        CASE 
            WHEN rjvn_note LIKE '%Manually issued NTK - Drive Off%'
                THEN 'Y'
            ELSE 'N'
            END
        ) AS ManuallyissuedNTK
    ,(
        CASE 
            WHEN rjvn_note LIKE '%Drive off- Final reminder sent%'
                THEN 'Y'
            ELSE 'N'
            END
        ) AS finalReminderSent
FROM [ICPS].[dbo].[removal_job_vehicle_notes] AS a
INNER JOIN tickets t ON (a.rjvn_pound_reference = t.t_number)
INNER JOIN dbo.ticket_hold_record thr ON (t.t_number = thr.thr_system_ref)
WHERE thr_hold_type = '2207555'
    AND t.t_contract IN ('18','1')

结果:

805464  XL05512211  Selby (60-100)  Abbey Walk  N   N   Y   N   N
805464  XL05512211  Selby (60-100)  Abbey Walk  N   Y   N   N   N
805464  XL05512211  Selby (60-100)  Abbey Walk  Y   N   N   N   N

但是我希望这个显示在单行而不是:

805464  XL05512211  Selby (60-100)  Abbey Walk  Y   Y   Y   N   N.

UPDATED :
944786  XL07075730  Selby (60-100)  Abbey Walk  N   N   N   N   Y   
but I should get
944786  XL07075730  Selby (60-100)  Abbey Walk  N   N   Y   N   Y   

该记录的我的实际表数据:

rjvn_pound_reference    rjvn_time                 rjvn_userid      rjvn_note
944786                  2014-01-08 10:38:17.000   JAMIED           Vehicle Drove Away During PCN Issue
944786                  2014-01-08 14:22:21.000   ADRIANJ          Correspondent Scanned
944786                  2014-02-05 15:44:53.000   ELLIED           Manually issued NTO drive off
944786                  2015-03-12 10:16:53.000   KATIEB           Drive off- Final reminder sent

--manually issued NTO drive off is displayed as 'N' instead of 'y'

3 个答案:

答案 0 :(得分:1)

因为查询中的列引用不合格(以指示从哪个表返回它们),并且因为我们没有要对其进行排序的模式定义。我们无法分辨rjvn_note列来自哪个表格。

就获取“重复”行而言,最可能的解释是JOIN,存在一对多关联,并且正在返回多行。这是预期的行为。

要“折叠”行,您可以在查询结尾添加 GROUP BY 。您还可以使用汇总函数(例如 MIN() MAX() )来“挑选”折叠行中表达式返回的值。

DISTINCT 关键字(与查询中一样)在SELECT列表中的整个表达式集上运行。这将消除完全重复的行。

在某些情况下还有其他一些有用的选择。例如,使用 EXISTS 谓词(它的要求是测试相关表中是否存在行,而不会像JOIN操作那样生成“重复”或者,使用SELECT列表中的子查询。

因为列引用不合格,并且因为我们没有模式定义,所以我们真的只是在猜测。一些“也许试试这个”的猜测可能会证明是正确的,但它们只是猜测。

这是我对您需要对查询进行更改以获取指定结果集的“猜测”:

  • 抛弃DISTINCT关键字
  • 在CASE表达式
  • 周围添加MAX()聚合
  • 添加GROUP BY子句列出SELECT列表中的所有非聚合表达式

  • (可选)使用表别名限定所有列引用(这样做的几个很好的理由:作为对未来读者的帮助,并使语句与未来的失败隔离

SELECT a.rjvn_pound_reference
     , t.t_reference
     , t.t_street_name
     , t.t_zone_name
     , MAX(CASE WHEN a.rjvn_note LIKE   '%CORRESPONDENCE%'
                THEN 'Y' ELSE 'N' END
       ) AS CorrespondenceReceived
     , MAX(CASE WHEN a.rjvn_note LIKE   '%review form complete%'
                THEN 'Y' ELSE 'N' END
       ) AS REVIEWFORMCOMPLETE
     , MAX(CASE WHEN a.rjvn_note LIKE   '%Manually issued NTO - Drive off%'
                THEN 'Y' ELSE 'N' END
       ) AS ManuallyissuedNTO
     , MAX(CASE WHEN a.rjvn_note LIKE   '%Manually issued NTK - Drive Off%'
                THEN 'Y' ELSE 'N'END
       ) AS ManuallyissuedNTK
     , MAX(CASE WHEN a.rjvn_note LIKE   '%Drive off- Final reminder sent%'
                THEN 'Y' ELSE 'N' END
       ) AS finalReminderSent
  FROM [ICPS].[dbo].[removal_job_vehicle_notes] a
  JOIN [tickets] t
    ON t.t_number = a.rjvn_pound_reference
  JOIN [dbo].[ticket_hold_record] thr 
    ON thr_system_ref = t.t_number
 WHERE thr.thr_hold_type = '2207555'
   AND t.t_contract IN ('18','1')
 GROUP
    BY a.rjvn_pound_reference
     , t.t_reference
     , t.t_street_name
     , t.t_zone_name

答案 1 :(得分:0)

试试这个:

SELECT [RJVN_POUND_REFERENCE],
  T_REFERENCE,
  T_STREET_NAME,
  T_ZONE_NAME,
  DECODE(MAX(CASE WHEN RJVN_NOTE LIKE '%CORRESPONDENCE%' THEN 1 ELSE 0 END), 1, 'Y', 'N') AS CORRESPONDENCERECEIVED,
  DECODE(MAX(CASE WHEN RJVN_NOTE LIKE '%review form complete%' THEN 1 ELSE 0 END), 1, 'Y', 'N') AS REVIEWFORMCOMPLETE,
  DECODE(MAX(CASE WHEN RJVN_NOTE LIKE '%Manually issued NTO - Drive off%' THEN 1 ELSE 0 END), 1, 'Y', 'N') AS MANUALLYISSUEDNTO,
  DECODE(MAX(CASE WHEN RJVN_NOTE LIKE '%Manually issued NTK - Drive Off%' THEN 1 ELSE 0 END), 1, 'Y', 'N') AS MANUALLYISSUEDNTK,
  DECODE(MAX(CASE WHEN RJVN_NOTE LIKE '%Drive off- Final reminder sent%' THEN 1 ELSE 0 END), 1, 'Y', 'N') AS FINALREMINDERSENT
FROM [ICPS].[DBO].[REMOVAL_JOB_VEHICLE_NOTES] AS A
INNER JOIN TICKETS T
ON(A.RJVN_POUND_REFERENCE=T.T_NUMBER)
INNER JOIN DBO.TICKET_HOLD_RECORD THR
ON(T.T_NUMBER=THR.THR_SYSTEM_REF)
WHERE THR_HOLD_TYPE='2207555'
  AND T.T_CONTRACT IN('18', '1')
GROUP BY [RJVN_POUND_REFERENCE],
  T_REFERENCE,
  T_STREET_NAME,
  T_ZONE_NAME

答案 2 :(得分:0)

在你的案例陈述中使用Max和像@jarlh建议的分组应该适合你:

<key>com.apple.developer.team-identifier</key>
<string>SomeIdentifier</string>