SQL数据返回2行而不是1

时间:2017-05-02 19:17:30

标签: sql sql-server

我似乎无法让我的查询在1行上返回所需的数据。它确实返回所有想要的数据,但是在2个单独的行上。

完整查询如下:

SELECT DISTINCT  
   case when sh1.Comments = 'clermont-0041 (Depot)'  
             then CAST(sh1.ScanTime as VARCHAR)  
        else ' '  
   end as 'Pickup Scan Time',  
   case when sh1.Comments = 'clermont-0041 (Depot)'  
             then sh1.Comments  
        else ' '  
   end as 'Pickup Scan',  
  case when sh1.Comments = 'orlwarehouse01-0041 (Depot)'  
            then CAST(sh1.ScanTime as VARCHAR)  
       else ' '  
  end as 'Warehouse Scan Time',  
  case when sh1.Comments = 'orlwarehouse01-0041 (Depot)'  
            then sh1.Comments  
       else ' '  
  end as 'Warehouse Scan',  
sh1.MasterScanID
FROM 
   tblOrder o  
   join tblScanHistory sh1 on o.OrderID = sh1.MasterScanID  
   join tblOrder on sh1.MasterScanID = o.OrderID
WHERE  
   (sh1.Comments = 'clermont-0041 (Depot)'
   or sh1.Comments = 'orlwarehouse01-0041 (Depot)')  
   and sh1.MasterScanID IS NOT NULL  
   and sh1.Scan like 'X-0041-D%'  
   and o.OrderID = '4419250'
ORDER BY
   sh1.MasterScanID desc

结果集如下所示:

Pickup Scan Time    | Pickup Scan           | Warehouse Scan Time | Warehouse Scan              | MasterScanID
                    |                       | May  2 2017 12:01AM | orlwarehouse01-0041 (Depot) | 4419250.00 
May  1 2017 12:32PM | clermont-0041 (Depot) |                     |                             | 4419250.00                                                     

2 个答案:

答案 0 :(得分:2)

使用group bymax()汇总您的数据:

select 
    o.OrderID
  , [Pickup Scan Time] = max(case 
      when sh1.Comments = 'clermont-0041 (Depot)' 
        then convert(varchar(23),sh1.ScanTime,121) 
      end)
  , [Pickup Scan] =  max(case 
      when sh1.Comments = 'clermont-0041 (Depot)' 
        then sh1.Comments
      end)
  , [Warehouse Scan Time] = max(case 
      when sh1.Comments = 'orlwarehouse01-0041 (Depot)'
        then convert(varchar(23),sh1.ScanTime,121) 
      end)
  , [Warehouse Scan] = max(case 
      when sh1.Comments = 'orlwarehouse01-0041 (Depot)'
        then sh1.Comments
      end)
from tblOrder o  
  left join tblScanHistory sh1 
    on o.OrderID = sh1.MasterScanid
   and sh1.Scan like 'X-0041-D%'
   and sh1.Comments in ('clermont-0041 (Depot)','orlwarehouse01-0041 (Depot)')
where o.OrderID = '4419250'
group by o.OrderID 
order by o.OrderID desc

此外,最好使用方括号而不是单引号指定别名,因为它们是标识符,而不是字符串文字。

其他说明:

Bad habits to kick : declaring varchar without (length) - Aaron Bertrand - 您应该始终为所有varcharnvarchar变量/参数提供长度。

答案 1 :(得分:2)

只是为了与众不同......:P

您可以根据您要查找的评论加入扫描历史记录两次并删除案例陈述。

要保留订单,请将tblScanHistory上的限制条件移至连接,并使连接保持连接。

SELECT  CAST(sh1.ScanTime as VARCHAR(25))  [Pickup Scan Time]
              ,  sh1.Comments  [Pickup Scan]
              ,  CAST(sh2.ScanTime as VARCHAR(25))  [Warehouse Scan Time]
              ,  sh2.Comments  [Warehouse Scan]
              ,  sh1.MasterScanID
FROM tblOrder o  
LEFT join tblScanHistory sh1 
  on o.OrderID = sh1.MasterScanID  
 AND sh1.Comments = 'clermont-0041 (Depot)'
 AND sh1.Scan like 'X-0041-D%'  
LEFT join tblScanHistory sh2 
  on o.OrderID = sh2.MasterScanID  
 AND sh2.Comments = 'orlwarehouse01-0041 (Depot)'
 AND sh2.Scan like 'X-0041-D%'  
WHERE o.OrderID = '4419250'
ORDER BY
   sh1.MasterScanID desc