SQL 分组排序、CASE...WHEN...、是否为空 查询

时间:2023-03-08 23:02:24
SQL 分组排序、CASE...WHEN...、是否为空 查询

select  Id,CustomerCode,CustomerName,CreateId,CreateName,Phone,StatusName,(case when phone is not null and len(phone)=11 and  SUBSTRING(phone,1,1)='1'  then 0 else 1 end) IsSend  from (
SELECT  row_number() over (partition  by  t0.CustomerCode order by t2.Id) Id,t0.CustomerCode, t1.Name AS CustomerName, t2.Id AS CreateId, t2.Name AS CreateName,isnull(t1.Phone1,t1.Phone2 ) Phone,
                t3.Name AS StatusName
FROM      BasicSetup.CustomerCommissioner AS t0 INNER JOIN
                Logistics.Customer AS t1 ON t0.CustomerCode = t1.CustomerCode INNER JOIN
                HumanResource.Employee AS t2 ON t0.EmployeeId = t2.Id INNER JOIN
                BasicSetup.CustomerStatus AS t3 ON t3.Id = t1.StatusId
WHERE   (t0.CommissionerTypeId = 1) AND (t3.Name <> '待收款')
) Temp