Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
332 views
in Technique[技术] by (71.8m points)

sql server - Skip Union Statement when condition is not true within SQL Query

I am needing to automate a report to go out on a monthly basis that uses End of Month tables. Currently, I have to go in every month and adding in the new monthly table within the subquery in order for the data to be accurate. Is there a way to union an additional query if a condition is TRUE. The condition in question is the date. The 01/2021 table doesn't exist until 02/01/2021, so I don't want to read in the two lines until then because SQL fails when it reads in a non existing table. Code Below:

Select * FROM

(Select '12.2020' as [Starting Point], [CustomerID],[Days]
from dbo.EOM_Table_202012

UNION ALL
---Insert IF statement and jump over lines if date is not greater than 01/31/2021
Select '1.2021' as [Starting Point],[CustomerID],[Days]
from dbo.EOM_Table_202101) a

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...