0

我正在尝试拉某个项目,除非第一项为空。

SELECT  
   FM.FileNumber
   ,FM.ClientsFileNumber AS 'Clients File Number'
   ,FM.FileID AS FileID
   ,S.Name AS Status
   ,FA.ReceivedDate AS 'Contract Fr Seller' --550, 238
   ,FA1.ReceivedDate AS 'Deed To Seller'    --266, 561
   ,FA2.ReceivedDate AS 'Deed Fr Seller'    --40, 629
   ,FA3.ReceivedDate AS 'HUDTo Seller'                     --269, 587
   ,FA4.ReceivedDate AS 'HUDFr Seller'                      --274, 637
   ,FA5.ReceivedDate AS 'Sent HUD to Buyer'          --268
   ,FA6.ReceivedDate AS 'Receive Buyer Signed Docs'          --273
   ,FA7.ReceivedDate AS 'Receive Buyer Funds'                      --119
   ,FA8.ReceivedDate AS 'Closing Completed'                          --272
   ,FA9.ReceivedDate AS 'FC Deed' --344
   -- ,n.Body as Notes
FROM   
   FileMain FM
LEFT OUTER JOIN      
   FileActions FA ON FA.FileID = FM.FileID AND FA.ActionDefID IN (238,550)
                     AND FA.Live = 1 AND FA.ReceiveCoordinatorTypeID = 2
                    --AND FA.ReceivedDate > '2013-03-01'
                    AND FA.ReceivedDate IS NOT NULL
                    AND FA.ActionDefID IS NOT NULL

基本上我试图让它选择FA.ActioNDefID = 230,除非那是NULL,然后我希望它拉FA.ActionDefID = 550,其他多项选择也是如此。

下面是我目前得到的,现在后面的总是238,除非它是空的,基本上我只想得到这些文件号中的1个,如果238为空,那么只拉550。

在此处输入图像描述

4

1 回答 1

2

我建议像这样重写你的查询,应该更具可读性

SELECT
   ...
  , [Contract Fr Seller] = (SELECT TOP 1 FA.ReceivedDate 
                     FROM FileActions FA 
                     WHERE FA.FileID = FM.FileID
                          AND  FA.ActionDefID IN (238,550)
                          AND FA.Live = 1
                          AND FA.ReceiveCoordinatorTypeID = 2 
                     ORDER BY FA.ActionDefID  -- This will make sure 238 on the top if exist
                      )                                 
FROM FileMain FM
...
于 2013-11-08T18:55:21.287 回答