Sql,从一个表中选择两个不同的值

时间:2014-02-05 11:32:30

标签: sql-server

我需要选择

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

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

大家好我需要在我的表bid中为状态选择两种类型,并显示id我该怎么做?

2 个答案:

答案 0 :(得分:0)

您可以使用CASE

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

或者,如果要连接所有行,首先是成功的,然后是不成功的,请使用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) 

答案 1 :(得分:0)

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