Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向现有的T-SQL查询中添加一些伪值?_Sql_Sql Server_Tsql - Fatal编程技术网

如何向现有的T-SQL查询中添加一些伪值?

如何向现有的T-SQL查询中添加一些伪值?,sql,sql-server,tsql,Sql,Sql Server,Tsql,我正在使用SQL Server 2014,我有以下T-SQL查询: USE MyDatabase SELECT [Property] ,[Pax] ,[Mth] ,'FY1718' as [ID] FROM Table1 WHERE [Mth] between '2017-07-01' and '2018-06-01' UNION ALL SELECT [Property] ,[Pax] ,[Mth] ,'FY1

我正在使用SQL Server 2014,我有以下T-SQL查询:

USE MyDatabase
SELECT [Property]
       ,[Pax]
       ,[Mth]
       ,'FY1718' as [ID]
FROM Table1
WHERE [Mth] between '2017-07-01' and '2018-06-01'
UNION ALL
SELECT [Property]
       ,[Pax]
       ,[Mth]
       ,'FY1819' as [ID]
FROM Table2
WHERE [Mth] between '2018-07-01' and '2019-06-01'
输出的摘录:

Property    Pax   Mth           ID
 TRO        120   2017-07-01    FY1718
 EBC        95    2018-08-01    FY1819
我想创建两个具有以下值的虚拟条目:

EBC,  0,  2017-09-01, FY1718
GHY,  0,  2017-10-01, FY1718
我的最终输出应该如下所示:

 Property   Pax   Mth           ID
 TRO        120   2017-07-01    FY1718
 EBC        95    2018-08-01    FY1819
 EBC        0     2017-09-01    FY1718
 GHY        0     2017-10-01    FY1718
如何在最终输出中添加这些行?

使用UNION ALL

SELECT [Property]
       ,[Pax]
       ,[Mth]
       ,'FY1718' as [ID]
FROM Table1
WHERE [Mth] between '2017-07-01' and '2018-06-01'
UNION ALL
SELECT [Property]
       ,[Pax]
       ,[Mth]
       ,'FY1819' as [ID]
FROM Table2
WHERE [Mth] between '2018-07-01' and '2019-06-01'
union all
SELECT 'EBC'
       ,0
       ,'2017-09-01'
       ,'FY1819'
union all
SELECT 'GHY'
       ,0
       ,'2017-10-01'
       ,'FY1819'

联合所有…价值观。。。
SELECT [Property],
   [Pax],
   [Mth],
   'FY1718' AS [ID] FROM   Table1 WHERE  [Mth] BETWEEN '2017-07-01' AND '2018-06-01'
UNION ALL
SELECT [Property],
   [Pax],
   [Mth],
   'FY1819' AS [ID]
FROM   Table2
WHERE  [Mth] BETWEEN '2018-07-01' AND '2019-06-01'
UNION ALL       
SELECT *
FROM   (
       VALUES ('EBC', 0, '2017-09-01', 'FY1818'),
       ('GHY', 0, '2017-10-01', 'FY1818')
   ) DummyTable([Property], [Pax], [Mth], [ID])