-2

I have written the following query wherein I am usig groupby clause on server column

select s.server, MAX(s.ipAddress) as ipAddress, 
        MAX(r.stacks->>"$[0].name") as stackName,
        MAX(a.aMessage) as aMessage
        from environments e
        inner join servers s
            on e.objectId  = s.environmentId
        inner join resources r
            on e.objectId  = r.environmentId     
        inner join audits a
            on a.id  = (select max(a.id) from audits a where a.logObjId = s.cAudit)  
        WHERE dateSubmitted BETWEEN NOW() - INTERVAL 90 DAY AND NOW()
        Group by s.server
        ORDER BY dateSubmitted; 

Howerver, server column may have NULL values with a valid ipAddress and stackName.

How to modify the query so that all NULL server column values are not missed out.

Expected Sample Data:

server  ipAddress   stackName   aMessage
NULL    NULL    Stack A Searching for IP pool
NULL    NULL    Stack B Message XYZ
NULL    NULL    Stack A Message ABC
9
  • I suspect it's your inner joins that are removing the nulls, not your group by. Consider switching those to LEFT OUTER JOIN instead.
    – JNevill
    Commented Jun 9, 2020 at 17:31
  • Please in code questions give a minimal reproducible example--cut & paste & runnable code, including smallest representative example input as code; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. Give the least code you can that is code that you show is OK extended by code that you show is not OK. (Debugging fundamental.) For SQL that includes DBMS & DDL (including constraints & indexes) & input as code formatted as a table. How to Ask Pause work on the overall goal, chop code to the 1st expression not giving what you expect & say what you expect & why.
    – philipxy
    Commented Jun 9, 2020 at 17:37
  • 1
    Agree with @philipxy in this case. Sample data would go a long way here, and since you have so many tables involved, sample data might be absolutely necessary to make your question reproducible. Commented Jun 9, 2020 at 17:40
  • Presumably your server nulls appear in the same rows as nulls in a column mentioned in an inner join equality, but for those equalities to be true & a row to be in the join, the columns must be non-null.
    – philipxy
    Commented Jun 9, 2020 at 17:44
  • 1
    Where? There are 4 tables and there is no sample data for them. What you need to do is to "dumb it down" (go one join at a time) and test it as you go... Commented Jun 9, 2020 at 18:10

1 Answer 1

0

It seems the INNER JOIN used to JOIN the table makes NULL value to removed from the result. So just modified the query. Try this one and see if you are able to see all the data of Server table so that NULL data also will come for Server column.

select s.server, MAX(s.ipAddress) as ipAddress, 
            MAX(r.stacks->>"$[0].name") as stackName,
            MAX(a.aMessage) as aMessage
            from servers s
            left join environments e
                on e.objectId  = s.environmentId
            left join resources r
                on e.objectId  = r.environmentId     
            left join audits a
                on a.id  = (select max(a.id) from audits a where a.logObjId = s.cAudit)  
            WHERE dateSubmitted BETWEEN NOW() - INTERVAL 90 DAY AND NOW()
            Group by s.server
            ORDER BY dateSubmitted; 
1
  • This is still not working as I see only one row with server as NULL
    – meallhour
    Commented Jun 9, 2020 at 17:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.