在sql server中查询中设置默认值

时间:2013-12-16 05:27:28

标签: sql sql-server

我有这样的查询:

SELECT  
   Count(*) 
   , Location_tbl.LocName as Location
   , Status_tbl.Status_Name
FROM         
   Transaction_tbl 
LEFT JOIN
   Location_tbl ON Transaction_tbl.Locid = Location_tbl.Locid  
LEFT JOIN 
   Status_tbl ON Transaction_tbl.Status = Status_tbl.Status
WHERE       
   Transaction_tbl.Status='3' and
   Transaction_tbl.Locid IN (5) and 
   DATEDIFF(n, Transaction_tbl.Paydate, GETDATE()) > 10
GROUP BY 
   Location_tbl.LocName, Status_tbl.Status_Name

我这样出去了:

car_count   Location   Status_Name
25          fashion     requested

我想设置status_Name始终:违规请求..我该怎么做?在我的status表中没有违反请求的任何状态..

1 个答案:

答案 0 :(得分:3)

您可以将字符串文字指定为列,但我不确定您何时可以从业务逻辑或显示层执行此操作:

SELECT  
   Count(*) 
   , Location_tbl.LocName as Location
   , 'Violated Request' AS Status_Name
FROM         
   Transaction_tbl 
LEFT JOIN
   Location_tbl ON Transaction_tbl.Locid = Location_tbl.Locid  
LEFT JOIN 
   Status_tbl ON Transaction_tbl.Status = Status_tbl.Status
WHERE       
   Transaction_tbl.Status='3' and
   Transaction_tbl.Locid IN (5) and 
   DATEDIFF(n, Transaction_tbl.Paydate, GETDATE()) > 10
GROUP BY 
   Location_tbl.LocName, Status_tbl.Status_Name