Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7956109
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:53:48+00:00 2026-06-04T03:53:48+00:00

Suppose I have a SQL table called AT_Devices with each record representing a piece

  • 0

Suppose I have a SQL table called AT_Devices with each record representing a piece of hardware. I have a second table called AT_Event_History which describes “events” that occur for pieces of hardware. An event can consist of something like the piece of equipment being lost, destroyed, retired, etc. Each event has a corresponding status code.

I’ve written some SQL to return the code of the most recent event for each record in AT_Devices. I have a business rule that if the device has no events on record, the status code should be 0.

I’ve written my SQL query to return 0 in this case, but for some reason, it is returning NULL. Why?

SELECT atDeviceHistory.StatusCodeNotNull AS StatusCode0  
 FROM dbo.AT_Devices atd LEFT OUTER JOIN 
(
    SELECT DeviceID,
     ParentCode,
     (CASE WHEN (StatusCode IS NOT NULL) THEN StatusCode ELSE 0 END) AS  
     StatusCodeNotNull,  
     WhenEntered AS StatusDate
FROM AT_Event_History as A
WHERE A.ParentCode=0 
AND A.WhenEntered >= (SELECT MAX(WhenEntered) 
                      FROM AT_Event_History AS B 
                      WHERE A.DeviceID=B.DeviceID AND ParentCode=0)
 ) atDeviceHistory ON atd.DeviceID=atDeviceHistory.DeviceID
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T03:53:50+00:00Added an answer on June 4, 2026 at 3:53 am

    Since you are doing a left join, if there are no matching records a null value will be returned for anything referencing the atDeviceHistory columns. If you do not want to return records with no match, then change this to an inner join. I would also recommend changing your case statement to a COALESCE:

    SELECT DeviceID,
         ParentCode,
         COALESCE(StatusCode ,0) AS StatusCodeNotNull,   
         WhenEntered AS StatusDate
    FROM AT_Event_History as A
    

    UPDATE:

    Try this:

    SELECT COALESCE(atDeviceHistory.StatusCode,0)  AS StatusCode0  
     FROM dbo.AT_Devices atd LEFT OUTER JOIN 
    (
        SELECT DeviceID,
         ParentCode,
         StatusCode  
         WhenEntered AS StatusDate
    FROM AT_Event_History as A
    WHERE A.ParentCode=0 
    AND A.WhenEntered >= (SELECT MAX(WhenEntered) 
                          FROM AT_Event_History AS B 
                          WHERE A.DeviceID=B.DeviceID AND ParentCode=0)
     ) atDeviceHistory ON atd.DeviceID=atDeviceHistory.DeviceID
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have a myTest.sql scripts, which contains thousands of create table blahblah statements.
Suppose you have a table in SQL: Prices ------ 13.99 14.00 52.00 52.00 52.00
Suppose I have a string 'nvarchar(50)', which is for example the T-SQL string segment
Suppose I have table first_table which has an FK to table second_table table second_table
suppose I have following sql table objid firstname lastname active 1 test test 0
I have a sql table images which has columns id , imagePath , caption``count
Suppose I have a table called Transaction and another table called Price. Price holds
Suppose I have a table called Names ID | Name ---+-------------- 1 | Bob
Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a PL/SQL function that selects one value from a table. If

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.