0

I have a query that using a join and I want to change that query using a subquery not join. But using a subquery I'm getting a rows with a null values. How can I display a result without a null values? is there another way to enhance my query ?

This is my query with subquery.

select ri.id_room,ri.id_item,
(select r.room_name from tbl_rooms r where r.id = ri.id_room and r.id_property = 1)  as 'Room Name',
(select r.sortvalue from tbl_rooms r where r.id = ri.id_room and r.id_property = 1) as 'Sort Value',
(select 'Room Status' = case when is_active = '1' then 'Active' else 'INACTIVE' end from tbl_rooms r where r.id = ri.id_room and r.id_property = 1),
(select i.itemname from tbl_items i where i.id = ri.id_item) as 'Item Code'
 from tbl_roomitems ri

Output:

Output of first query

and this is my query using join.

select ri.id_room, r.room_name, r.sortvalue,'Status' = case when is_active = '1' then 'Active' 
else 'INACTIVE' end from tbl_roomitems ri
left outer join tbl_rooms r on ri.id_room = r.id
where ri.id_item = 1 and r.id_property = 1
order by r.sortvalue, r.room_name

Output:

Output of second query

Gerry
  • 73
  • 6
  • [Why should I not upload images of code/data/errors when asking a question?](https://meta.stackoverflow.com/q/285551/3404097) – philipxy Mar 07 '23 at 02:45
  • Please before considering posting: Pin down code issues via [mre]. Read manuals/references & google error messages & many clear, concise & precise phrasings of your question/problem/goal, with & without your particular names/strings/numbers, 'site:stackoverflow.com' & tags; read many answers. SO/SE search is poor & literal & unusual, read the help. Google re googling/searching, including Q&A at [meta] & [meta.se]. [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/3404097) [ask] [Help] Reflect research in posts. – philipxy Mar 07 '23 at 02:46
  • Debug questions require a [mre]--cut & paste & runnable code including initialization; desired & actual output (including verbatim error messages); tags & versions; clear specification & explanation. For SQL include DDL & tabular initialization code. For debug that includes the least code you can give that is code that you show is OK extended by code that you show is not OK. – philipxy Mar 07 '23 at 02:48
  • should I remove the images right now? – Gerry Mar 07 '23 at 02:56
  • Please act on all the feedback. (Click on 'edit'.) This post is here forever. You can also click to agree that this is a duplicate of the suggested link. (LEFT JOIN returns INNER JOIN rows UNION ALL unmatched left table rows extended by NULLs. Always know what INNER JOIN you want as part of an OUTER JOIN. After a LEFT JOIN a WHERE, INNER JOIN or HAVING that requires a right [sic] table column to be not NULL removes any rows with introduced NULLs, ie leaves only INNER JOIN rows, ie "turns OUTER JOIN into INNER JOIN". You have that. This is a faq.) – philipxy Mar 07 '23 at 05:17

0 Answers0