T-sql 的 case 用法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u010690818/article/details/80193019
select  d.ParentId as 'UnionId', a.CompanyId ,a.ShopId
,(case when a.ShopId>0 then e.Text else d.Text end) as 'OrganizationName'
,a.JHCK as 'WarehouseId', g.Text as'WarehouseName' 
, b.ProductID as 'ProductId' ,f.Code as 'ProductCode',f.Text as 'ProductName'
,sum(b.Num) as'Num'
from [dbo].[Sys_EntryLibraryRecord] a
inner join [dbo].[Sys_EntryLibraryRecordProduct] b on a.Id=b.RecordID
left join [dbo].[TransferAcceptance] c on a.ObjID=c.Id
left join [dbo].[Sys_Organization] d on d.Id= a.CompanyId
left join [dbo].[Sys_Organization] e on e.Id= a.ShopId
left join [dbo].[Sys_Product]  f on f.Id= b.ProductID
left join [dbo].[Sys_Organization] g on g.Id= a.JHCK
where a.CreateTime >='2017-01-01' and a.CreateTime<='2019-01-01'
and a.Type=22
group by d.ParentId,a.CompanyId,a.ShopId
,(case when a.ShopId>0 then e.Text else d.Text end) 
,a.JHCK, g.Text, b.ProductID,f.Code,f.Text 

猜你喜欢

转载自blog.csdn.net/u010690818/article/details/80193019