1

this is the query

SELECT NAME,DATE, COUNT(*) AS ROW_COUNT 
FROM STG
WHERE 
DATE='2018-04-23' AND 
NAME='DRAFT'
GROUP BY NAME,DATE 

Requirement is if the query returns no rows then I should get output as 'TEST','TODAY'S DATE', 0 else if query return rows then the normal output which is expected.

1 Answer 1

1

Well, one method uses union all:

with t as (
      SELECT NAME, DATE, COUNT(*) AS ROW_COUNT 
      FROM STG
      WHERE DATE = '2018-04-23' AND NAME = 'DRAFT'
      GROUP BY NAME, DATE
     )
select *
from t
union all
select name, date, row_count
from (select 'TEST' as name, getdate() as date, 0 as row_count) x
where not exists (select 1 from t);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the swift response and help it worked perfectly!!

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.