T-sql 的 case 用法
阿新 • • 發佈:2018-11-09
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