使用没有重复结果的 case 和 like 语句
Using case and like statment without duplicate results
我正在尝试使用 case 和 like 语句。当我 运行 以下查询时,它在某些行上复制结果,而在特定列上它是 y 。我正在使用 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
我的实际 TABLE 该记录的数据:
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'
试试这个:
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
在您的案例陈述中使用 Max 并按照@jarlh 的建议进行分组应该适合您:
SELECT q1.[rjvn_pound_reference]
,q1.t_reference
,q1.t_street_name
,q1.t_zone_name
,(
CASE
WHEN q1.CorrespondenceReceived = 1
THEN 'Y'
ELSE 'N'
END
) CorrespondenceReceived
,(
CASE
WHEN q1.REVIEWFORMCOMPLETE = 1
THEN 'Y'
ELSE 'N'
END
) REVIEWFORMCOMPLETE
,(
CASE
WHEN q1.ManuallyissuedNTO = 1
THEN 'Y'
ELSE 'N'
END
) ManuallyissuedNTO
,(
CASE
WHEN q1.ManuallyissuedNTK = 1
THEN 'Y'
ELSE 'N'
END
) ManuallyissuedNTK
,(
CASE
WHEN q1.finalReminderSent = 1
THEN 'Y'
ELSE 'N'
END
) finalReminderSent
FROM (
SELECT [rjvn_pound_reference]
,t_reference
,t_street_name
,t_zone_name
,MAX(CASE
WHEN rjvn_note LIKE '%CORRESPONDENCE%'
THEN 1
ELSE 0
END) AS CorrespondenceReceived
,MAX(CASE
WHEN rjvn_note LIKE '%review form complete%'
THEN 1
ELSE 0
END) AS REVIEWFORMCOMPLETE
,MAX(CASE
WHEN rjvn_note LIKE '%Manually issued NTO - Drive off%'
THEN 1
ELSE 0
END) AS ManuallyissuedNTO
,MAX(CASE
WHEN rjvn_note LIKE '%Manually issued NTK - Drive Off%'
THEN 1
ELSE 0
END) AS ManuallyissuedNTK
,MAX(CASE
WHEN rjvn_note LIKE '%Drive off- Final reminder sent%'
THEN 1
ELSE 0
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'
)
GROUP BY [rjvn_pound_reference]
,t_reference
,t_street_name
,t_zone_name
) q1
因为查询中的列引用不合格(指示它们是从哪个 table返回的),并且因为我们没有模式定义通过整理...我们无法判断 table rjvn_note
列的来源。
就获得 "duplicate" 行而言,最可能的解释是 JOIN
,存在一对多关联,并且返回多行。这是预期的行为。
至"collapse"行,您可以在查询末尾添加GROUP BY
。您还可以使用聚合函数(如 MIN()
或 MAX()
)到 "pick out" 表达式返回的值来自折叠的行。
DISTINCT
关键字(在您的查询中)作用于 [=72] 中的 整个 表达式集=] 列表。这将消除完全重复的行。
在某些情况下,还有其他一些有用的替代方法。例如,用作 EXISTS
谓词(它的要求是测试相关 table 中是否存在一行,而不产生 "duplicates"就像 JOIN
操作一样。或者,在 SELECT
列表中使用子查询。
因为列引用不合格,而且因为我们没有模式定义,所以我们真的只是猜测。有些 "maybe try this" 猜测可能是正确的,但它们只是猜测。
这是我的"guess",您需要对查询进行更改以获得指定的结果集:
- 放弃
DISTINCT
关键字
- 在 CASE 表达式周围添加
MAX()
聚合
添加一个 GROUP BY
子句,列出 SELECT 列表中的所有非聚合表达式
(可选)用 table 别名限定所有列引用(这样做的几个很好的理由:作为对未来读者的帮助,并使语句免受未来失败的影响
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
我正在尝试使用 case 和 like 语句。当我 运行 以下查询时,它在某些行上复制结果,而在特定列上它是 y 。我正在使用 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
我的实际 TABLE 该记录的数据:
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'
试试这个:
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
在您的案例陈述中使用 Max 并按照@jarlh 的建议进行分组应该适合您:
SELECT q1.[rjvn_pound_reference]
,q1.t_reference
,q1.t_street_name
,q1.t_zone_name
,(
CASE
WHEN q1.CorrespondenceReceived = 1
THEN 'Y'
ELSE 'N'
END
) CorrespondenceReceived
,(
CASE
WHEN q1.REVIEWFORMCOMPLETE = 1
THEN 'Y'
ELSE 'N'
END
) REVIEWFORMCOMPLETE
,(
CASE
WHEN q1.ManuallyissuedNTO = 1
THEN 'Y'
ELSE 'N'
END
) ManuallyissuedNTO
,(
CASE
WHEN q1.ManuallyissuedNTK = 1
THEN 'Y'
ELSE 'N'
END
) ManuallyissuedNTK
,(
CASE
WHEN q1.finalReminderSent = 1
THEN 'Y'
ELSE 'N'
END
) finalReminderSent
FROM (
SELECT [rjvn_pound_reference]
,t_reference
,t_street_name
,t_zone_name
,MAX(CASE
WHEN rjvn_note LIKE '%CORRESPONDENCE%'
THEN 1
ELSE 0
END) AS CorrespondenceReceived
,MAX(CASE
WHEN rjvn_note LIKE '%review form complete%'
THEN 1
ELSE 0
END) AS REVIEWFORMCOMPLETE
,MAX(CASE
WHEN rjvn_note LIKE '%Manually issued NTO - Drive off%'
THEN 1
ELSE 0
END) AS ManuallyissuedNTO
,MAX(CASE
WHEN rjvn_note LIKE '%Manually issued NTK - Drive Off%'
THEN 1
ELSE 0
END) AS ManuallyissuedNTK
,MAX(CASE
WHEN rjvn_note LIKE '%Drive off- Final reminder sent%'
THEN 1
ELSE 0
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'
)
GROUP BY [rjvn_pound_reference]
,t_reference
,t_street_name
,t_zone_name
) q1
因为查询中的列引用不合格(指示它们是从哪个 table返回的),并且因为我们没有模式定义通过整理...我们无法判断 table rjvn_note
列的来源。
就获得 "duplicate" 行而言,最可能的解释是 JOIN
,存在一对多关联,并且返回多行。这是预期的行为。
至"collapse"行,您可以在查询末尾添加GROUP BY
。您还可以使用聚合函数(如 MIN()
或 MAX()
)到 "pick out" 表达式返回的值来自折叠的行。
DISTINCT
关键字(在您的查询中)作用于 [=72] 中的 整个 表达式集=] 列表。这将消除完全重复的行。
在某些情况下,还有其他一些有用的替代方法。例如,用作 EXISTS
谓词(它的要求是测试相关 table 中是否存在一行,而不产生 "duplicates"就像 JOIN
操作一样。或者,在 SELECT
列表中使用子查询。
因为列引用不合格,而且因为我们没有模式定义,所以我们真的只是猜测。有些 "maybe try this" 猜测可能是正确的,但它们只是猜测。
这是我的"guess",您需要对查询进行更改以获得指定的结果集:
- 放弃
DISTINCT
关键字 - 在 CASE 表达式周围添加
MAX()
聚合 添加一个
GROUP BY
子句,列出 SELECT 列表中的所有非聚合表达式(可选)用 table 别名限定所有列引用(这样做的几个很好的理由:作为对未来读者的帮助,并使语句免受未来失败的影响
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