I
I
Ilya2019-11-15 14:08:05
SQL
Ilya, 2019-11-15 14:08:05

How to deduce the fact of missing data?

Is there any way to output a string with NULLs (with anything) when there is no data?
I tried to make a temporary table with one record and make a right join to it, but I did not understand what to write in the condition after ON.
Update: My query always returns one record. In the "summary table" (after all joins) there are thousands of records. If no entry passes the where condition, then that should be returned 5dcea00a2bd9e805777202.png
instead of no entries.
Is it possible?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey c0re, 2019-11-15
@illaaa

It is necessary to lay the correct logic in the application, either processing the number of records, or with an exception.
and on the question - FULL OUTER JOIN

-- выбираем несуществующую запись
SELECT pt.pid, t.id, t.text FROM (SELECT 1 AS pid) pt
  FULL OUTER JOIN (
    SELECT 1 AS pid, tt.*
      FROM tbl tt
      -- условие запроса:
      WHERE tt.id = 10
  ) AS t ON pt.pid = t.pid
;

see example on dbfiddle
also LEFT OUTER JOIN , but put condition on request inside brackets ( ) in subquery, see dbfiddle :
-- выбираем несуществующую запись
SELECT pt.pid, t.id, t.text FROM (SELECT 1 AS pid) pt
  LEFT OUTER JOIN (
    SELECT 1 AS pid, tt.*
      FROM tbl tt
      -- условие запроса:
      WHERE tt.id = 10
  ) AS t ON pt.pid = t.pid
;

You can still UNION subquery with NULL , but it will always be inserted.
SELECT id, text FROM tbl
WHERE id = 10
UNION
SELECT NULL, NULL
;

example, above.

V
Vladimir Korotenko, 2019-11-15
@firedragon

The goal is generally not clear. Well, throw out the exception, or form your own sign.
In the general case, if you have no data, then according to the results, you simply have nothing to return and, accordingly, the result is NULL.
https://docs.microsoft.com/ru-ru/sql/t-sql/languag...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question