简体   繁体   中英

SQL Query unable to return record

The following SQL works as expected if ProjectStatusType.Name is not null. Therefore I put the case statement which replace null value with 'Not specified'. What I am trying to do is to add a where statement to display ProjectStatusType.Name = 'Not Specified', but no data gets returned, although there is a record in database with null projectstatustype.name. Please advise; what is another way, or how can I fix the SQL query?

SELECT PersonResponsible.Name AS TeamLeaderName,
       CASE
           WHEN ProjectStatusType.Name IS NULL THEN 'Not Specified'
           ELSE COALESCE(ProjectStatusType.Name, '')
       END AS ProjectStatusName,
       Project.ProjectTitle AS Title,
       ProjectStatus.DateStatus,
       Project.ProjectId,
       Project.ContactName,
       BusinessDivision.Name AS BusinessUnit,
       BusinessUnit.Name AS WorkSection,
       ProjectSubGroup.Name AS ProjectSubGroupName,
       ProjectGroup.Name AS ProjectGroupName,
       Project.DateRequested
FROM BusinessUnit
INNER JOIN BusinessDivision ON BusinessUnit.BusinessDivisionId = BusinessDivision.BusinessDivisionId
INNER JOIN ProjectCode ON BusinessUnit.BusinessUnitId = ProjectCode.BusinessUnitId
RIGHT OUTER JOIN Project
INNER JOIN ProjectSubGroup ON Project.ProjectSubGroupId = ProjectSubGroup.ProjectSubGroupId
INNER JOIN ProjectGroup ON ProjectSubGroup.ProjectGroupId = ProjectGroup.ProjectGroupId ON ProjectCode.ProjectCodeId = Project.ProjectCodeId
LEFT OUTER JOIN PersonResponsible ON Project.PersonResponsibleId = PersonResponsible.PersonResponsibleId FULL
OUTER JOIN ProjectStatusType
INNER JOIN ProjectStatus ON ProjectStatusType.ProjectStatusTypeId = ProjectStatus.ProjectStatusTypeId
AND ProjectStatus.ProjectStatusId IN
  (SELECT MAX(ProjectStatusId) AS ProjectStatusId
   FROM ProjectStatus
   GROUP BY ProjectId) ON Project.ProjectId = ProjectStatus.ProjectId
WHERE ProjectStatus ProjectStatusType.Name ='Not Specified'

Two points:

First, a CASE checking for null seems redundant to a COALESCE expression.

This should be simpler:

SELECT COALESCE(ProjectStatusType.Name, 'Not Specified') AS ProjectStatusName

Second, comparing to ProjectStatusType.Name (the value in your table) is not the same as comparing to ProjectStatusName (the value you selected).

Try a WHERE clause closer to this:

WHERE ProjectStatusName = 'Not Specified'

Here is a great chart from Itzik Ben-Gan on how SQL server processes a query. The order is different than how we write T-SQL.

Thus the solution by rutter might miss the empty string.

If I was doing this type of business logic alot, I would write a function that would take both the empty string or null value and return a string 'unknown'. Call this function in the query and filter by 'unknown'.

The example below creates a temporary table, loads the table with data, and returns both the empty string and null value converted to the adjust name 'Not Specified'.

I hope this helps.

I always refer to the logical processing chart when queries do not work the way I think they should.

Good luck

John

-- 
-- Logical query processing
--

-- From, Where, Group By, Having, Select (expression, distince, top), order by

-- sample table
create table #status
( id int identity(1,1),
  name varchar(50) null
);

-- Sample data
insert into #status values
(''),
(null),
('www.craftydba.com');


-- Return rows with null or empty string
select 
    case when ltrim(name) = '' then 'Not Specified'
    else coalesce(name, 'Not Specified') end as adjusted_named, 
    name
from #status 
where 
  ltrim(name) = '' or 
  coalesce(name, 'Not Specified') = 'Not Specified';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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