-2

我正在尝试使用每列的不同过滤器两次查看同一列。使用 SQL Server 08

例如:在我调用 DPO 的列中,我只想显示工作代码 abc 和 xyz 。标记为 Work Needed 的列需要显示除工作代码 abc 和 xyz 之外的所有内容。

下面是我的查询的一部分减去一些表以缩短它,这就是我从 3 个表中提取的原因。

SELECT 

WkRoDesc.Job_Code AS [Work Needed], 
WkRoDesc.Job_Code AS [DPO]

FROM vhstock, WkInvReg, WkRoDesc

WHERE vhstock.no = WkInvReg.Stock_No AND WkInvReg.Ro_Number = WkRoDesc.Ro_Number
4

2 回答 2

0

像这样的东西?或者你想对行做更多的事情?

编辑:针对 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

希望它适用于您的系统!

于 2013-09-05T20:32:26.253 回答
0

那这个呢?

SELECT 
CASE WHEN WkRoDesc.Job_Code in ('abc','xyz') then NULL ELSE WkRoDesc.Job_Code END AS [Work Needed], 
CASE WHEN WkRoDesc.Job_Code in ('abc','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
于 2013-09-06T10:14:13.253 回答