我可以在SQL Server Management Studio中以交互方式工作,但在视图中需要这些结果。我对T-SQL有些新意。是否有一种简单的方法可以将它放在一个表(而不是一个视图)中并让它每天早上按计划运行?
Declare @Tempresults TABLE
(
ID varchar(100),
MinLine varchar(100),
MaxLine varchar(100)
)
INSERT INTO @Tempresults (ID, MinLine, MaxLine)
Select PACK_ID, Min(LINE) as MinLine, MAX(Line) as MaxLine
from DESC
group by PACK_ID
Declare @Tempresults2 TABLE
(
ID2 varchar(100),
MinLine2 varchar(100),
MaxLine2 varchar(100),
FirstEntry varchar(500)
)
INSERT INTO @Tempresults2 (ID2, MinLine2, MaxLine2, FirstEntry)
select ID, MinLine, MaxLine, PACK_DESC as FirstEntry
from @Tempresults b, DESC a
where b.ID = a.PACK_ID and b.MinLine = a.line
Declare @Tempresults3 TABLE
(
ID3 varchar(100),
MinLine3 varchar(100),
MaxLine3 varchar(100),
LastEntry varchar(500)
)
INSERT INTO @Tempresults3 (ID3, MinLine3, MaxLine3, LastEntry)
select ID, MinLine, MaxLine, PACK_DESC as LastEntry
from @Tempresults b, DESC a
where b.ID = a.PACK_ID and b.MaxLine = a.line
select a.id2 as ID, b.lastentry as MaxDesc, a.firstentry as MinDesc
from @Tempresults2 a, @Tempresults3 b
Where a.ID2 = b.ID3
group by ID2, FirstEntry, LastEntry
答案 0 :(得分:0)
这有用吗?
SELECT
a.id2 AS ID,
MaxDesc,
MinDesc
FROM
(
SELECT
PACK_ID AS ID,
MinLine,
MaxLine,
d.PACK_DESC AS MaxDesc,
a.PACK_DESC AS MinDesc
FROM
(
SELECT
PACK_ID,
MIN(LINE) AS MinLine,
MAX(Line) AS MaxLine
FROM
DESC
GROUP BY
PACK_ID
) b INNER JOIN
DESC a ON
b.ID = a.PACK_ID AND
b.MinLine = a.line INNER JOIN
DESC d ON
b.ID = d.PACK_ID AND
b.MaxLine = d.line
)
GROUP BY
ID2, FirstEntry, LastEntry