2014-02-05 115 views
0

,我需要選擇SQL語句,從一個表

Select Bid Id,date 
From Bid where 
Status ='Won' as successful 

而且還

Select Bid Id, date 
From Bid where 
Status =Null as unsuccessful 

選擇兩種不同的價值大家好,我需要在我的表bid選擇兩種類型的地位和施展id我該怎麼做?

回答

0

您可以使用CASE

Select Bid, Id,date, 
    WonStatus = CASE WHEN Status ='Won' THEN 'Successful' ELSE 'Unsuccessful' END 
From Bid where 

,或者,如果你想Concat的所有行,第一個成功,然後不成功,使用UNION ALL

Select Bid, Id,date 
From Bid 
Where Status ='Won' as successful 


UNION ALL 

Select Bid, Id,date 
From Bid 
Where Status IS NULL AS unsuccessful 

編輯:「和如果想知道每個月有多少成功和多少成功?「

那麼這應該工作:

Select YEAR(date) AS B_Year, 
     MONTH(date) AS B_Month, 
     Count(*) AS Success_Count 
From Bid 
Where Status = 'Won' 
GROUP BY YEAR(date), MONTH(date) 
+0

,如果想知道的是有多少成功的,又有多少是每月 – user3246292

+0

成功@ user3246292:編輯我的答案。 –

0
SELECT Bid 
    , ID 
    , date 
    , CASE 
     WHEN status IS NULL THEN 'unsuccessful' 
     WHEN status = 'Won' THEN 'successful' 
WHERE status = 'Won' 
    OR status IS NULL