如果不存在SQL,则查找值显示为零



我有类似的数据:

SerivceID   Service_Name            Serivce_Charges     FK_VendorID FK_ExpertID FK_CustomerID   BookingDate TimeSlot    current_Job GettingJobDate
4           Furniture & Home Decor  200                 1                2      1               08-11-2017  3              1         2018-02-04 13:57:49.733

在一行。现在我想计算哪个查询是:

select A_Services.Service_Name as [Service Name],count(Service_Name) as [Total Serve] 
from A_Services where FK_ExpertID = 2 group by Service_Name order by [Service Name]

它显示数据:

Service Name            Total Serve
Furniture & Home Decor  1

我想获取数据为:

Service Name            Total Serve
Furniture & Home Decor  1
Car Towing              0

对于您当前的数据不可能。

"汽车牵引"应该来自某个地方,计算机没有预设的服务名称。它怎么知道它是否以"拖曳"或"投掷"或"拖曳"结尾?

您需要在某个地方指定。

(您可以选择自己拥有的东西,但不能显示您没有的数据(


解决方案:

是否可以将同一服务连接到不同的供应商,专家或客户?

如果是,我建议您创建服务类型类别。(id,名称(

Id   Name
1    Furniture & Home Decor 
2    Car Towing          
3    Car Painting
4    Etc...
/*Name should be Unique*/

然后,您可以使用 ServiceType_fk

而不是使用 Service_name

使用该数据,您可以查询:

SELECT ST.Name AS [Service Name], COUNT(S.ServiceType_FK) AS [Total Serve] 
FROM ServiceType ST
LEFT JOIN A_Services S
ON S.ServiceType_FK = ST.Id
--WHERE S.FK_ExpertID = 2
GROUP BY ST.Name

返回您想要的东西。