像这样的东西?或者你想对行做更多的事情?
编辑:针对 SQL Server 兼容性进行了修改:
SELECT
CASE WHEN WkRoDesc.Job_Code = 'abc'
OR WkRoDesc.Job_Code = 'xyz'
THEN NULL ELSE WkRoDesc.Job_Code END AS WorkNeeded,
CASE WHEN WkRoDesc.Job_Code = 'abc'
OR WkRoDesc.Job_Code = 'xyz'
THEN WkRoDesc.Job_Code ELSE NULL END AS DPO
FROM vhstock, WkInvReg, WkRoDesc
WHERE vhstock.no = WkInvReg.Stock_No
AND WkInvReg.Ro_Number = WkRoDesc.Ro_Number
笔记:听起来你几乎也在寻找一个GROUP_CONCAT()
,但我想我会等着确定……
EDIT2:这在 SQL Server 中使用了一些丑陋的 XML 处理来执行类似 MySQL 的操作GROUP_CONCAT
。
SELECT
stuff((
select cast(',' as varchar(max)) + j.Job_Code
from WkRoDesc AS j
where j.Ro_Number = WkInvReg.Ro_Number
and NOT (j.Job_Code = 'abc' OR j.Job_Code = 'xyz')
order by j.Job_Code
for xml path('')
), 1, 1, '') AS WorkNeeded,
stuff((
select cast(',' as varchar(max)) + j.Job_Code
from WkRoDesc AS j
where j.Ro_Number = WkInvReg.Ro_Number
and (j.Job_Code = 'abc' OR j.Job_Code = 'xyz')
order by j.Job_Code
for xml path('')
), 1, 1, '') AS DPO
FROM vhstock, WkInvReg, WkRoDesc
WHERE vhstock.no = WkInvReg.Stock_No
AND WkInvReg.Ro_Number = WkRoDesc.Ro_Number
GROUP BY WkInvReg.Ro_Number
希望它适用于您的系统!