How merge multi statement into one Select statement in sql
I Tried


You should include what you tried in the question - any detail you can give helps us help you understand.


  How to merge multi statement into one select statement.


You don't say what output you actually expect so I'm only guessing here. I suspect you want the rows from each of those four queries to be output in one resultset, in which case you need the UNION set operator, see https://docs.microsoft.com/en-us/sql/t-sql/language-elements/set-operators-union-transact-sql for details.

For a generic example:

SELECT &lt;columns&gt; FROM &lt;tables_or_views&gt;
UNION ALL
SELECT &lt;columns&gt; FROM &lt;tables_or_views&gt;
UNION ALL
-- and so on...
UNION ALL
SELECT &lt;columns&gt; FROM &lt;tables_or_views&gt;
-- note: order by applied at the end to all outputs, not in individual SELECTs
ORDER BY &lt;columns&gt;


If you use UNION instead of UNION ALL and the queries output any duplicate rows, those duplicates will be filtered and only one output. This is basically done the same way as DISTINCT which involves a sort operation (which may not match the sort you have applied via ORDER BY) that for results of any considerable size will involve a dump to tempdb, so unless you know duplicates may be present and want them filtered out it is more efficient to use UNION ALL instead of just plain UNION.

Also unless you need a specific ordering, avoid specifying an ORDER BY clause as that will likely result in dropping the results to tempdb too.

More specifically for the queries you included above I suspect that there is a simpler way (from the query planners PoV so likely more efficient) using single SELECT with appropriate GROUPing and/or window functions instead of several statements UNIONed. You should describe more fully what you are actually trying to get out of this code, and neaten up the included code so it is more readable as I've tried to do a bit with the first example, and someone might help you do that.