简体   繁体   English

在SQL Server的查询中设置默认值

[英]set default value in query in sql server

I have a query like this: 我有这样的查询:

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

I am getting out put like this: 我出局像这样:

car_count   Location   Status_Name
25          fashion     requested

I want to set my status_Name always: Violated Request.. how can I do that? 我想始终设置我的status_Name :违规请求..我该怎么做? In my status table not having any status with Violated Request.. 在我的status表中,没有任何违反请求的状态。

You can just specify a string literal as a column, although I'm not sure why you would want when you can do this from the business logic or display layer instead: 您可以仅将字符串文字指定为列,尽管我不确定为什么可以从业务逻辑或显示层执行此操作时为什么要这么做:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM