以下代码基本上创建了一个具有两列和十二行的表。 尽管最终产品还不错,但我想通过使用第一个select语句的别名替换别名来消除创建临时表的需要,然后我可以在第二个select语句中引用该别名。
如果您查看代码-第一条select语句将创建一个包含一行和十二列的表。然后将结果另存为tmptblTTM。然后,我从该表中提取数据到第二个选择查询中,将其转置,然后将其另存为第二个表MX。我想直接在第二个选择查询中引用第一个选择查询的结果,而完全跳过对tmptblTTM的需要。
DECLARE @queryDate1 datetime SET @queryDate1 = '03/31/2019';
DECLARE @queryDate2 datetime set @queryDate2 = EOMONTH(@queryDate1,-1);
DECLARE @queryDate3 datetime SET @queryDate3 = EOMONTH(@queryDate1,-2);
DECLARE @queryDate4 datetime set @queryDate4 = EOMONTH(@queryDate1,-3);
DECLARE @queryDate5 datetime SET @queryDate5 = EOMONTH(@queryDate1,-4);
DECLARE @queryDate6 datetime set @queryDate6 = EOMONTH(@queryDate1,-5);
DECLARE @queryDate7 datetime SET @queryDate7 = EOMONTH(@queryDate1,-6);
DECLARE @queryDate8 datetime set @queryDate8 = EOMONTH(@queryDate1,-7);
DECLARE @queryDate9 datetime SET @queryDate9 = EOMONTH(@queryDate1,-8);
DECLARE @queryDate10 datetime set @queryDate10 = EOMONTH(@queryDate1,-9);
DECLARE @queryDate11 datetime SET @queryDate11 = EOMONTH(@queryDate1,-10);
DECLARE @queryDate12 datetime set @queryDate12 = EOMONTH(@queryDate1,-11);
SELECT
convert(varchar(10),@queryDate12,101) as [M1]
,convert(varchar(10),@queryDate11,101) as [M2]
,convert(varchar(10),@queryDate10,101) as [M3]
,convert(varchar(10),@queryDate9,101) as [M4]
,convert(varchar(10),@queryDate8,101) as [M5]
,convert(varchar(10),@queryDate7,101) as [M6]
,convert(varchar(10),@queryDate6,101) as [M7]
,convert(varchar(10),@queryDate5,101) as [M8]
,convert(varchar(10),@queryDate4,101) as [M9]
,convert(varchar(10),@queryDate3,101) as [M10]
,convert(varchar(10),@queryDate2,101) as [M11]
,convert(varchar(10),@queryDate1,101) as [M12]
INTO ga_financial.dbo.tmpTblTTM
CREATE TABLE tblMX (MX varchar (max), MDY date)
INSERT INTO tblMX
SELECT
'M1' AS [MX], M1 AS date FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M2', M2 AS date FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M3', M3 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M4', M4 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M5', M5 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M6', M6 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M7', M7 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M8', M8 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M9', M9 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M10', M10 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M11', M11 AS [Date] FROM ga_financial.dbo.tmpTblTTM
UNION ALL SELECT 'M12', M12 AS [Date] FROM ga_financial.dbo.tmpTblTTM
select * from tblMX
答案 0 :(得分:0)
您可以使用CTE:
with tmpTblTTM as (
<the code here>
SELECT'M1' AS [MX], M1 AS date FROM tmpTblTTM
UNION ALL SELECT 'M2', M2 AS date FROM tmpTblTTM
UNION ALL SELECT 'M3', M3 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M4', M4 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M5', M5 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M6', M6 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M7', M7 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M8', M8 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M9', M9 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M10', M10 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M11', M11 AS [Date] FROM tmpTblTTM
UNION ALL SELECT 'M12', M12 AS [Date] FROM tmpTblTTM;
这将保留您的原始查询。我更喜欢:
with tmpTblTTM as (
. . .
)
select v.*
from tmpTblTTM t cross apply
(values ('M1', t.m1), ('M2', t.m2), . . .
) v(mx, date);
union
不是必需的。