像上面一样,我需要创建ssrs报告。
IF OBJECT_ID ('RANDD') IS NOT NULL
DROP PROC RANDD
GO
--EXEC RANDD 1
CREATE PROC [dbo].RANDD
@Reg INT
AS
BEGIN
SET NOCOUNT ON
IF object_ID ('tempdb..#Chart')IS NOT NULL
DROP TABLE #Chart
CREATE TABLE #Chart
(
CurrentHr FLOAT ,
CurrentCyc FLOAT ,
RegID INT ,
Reg nvarchar(max)
)
INSERT INTO #Chart (CurrentHr, CurrentCyc,RegID,Reg )
VALUES
(120,10,@Reg,'Aircraft'),
(120,20,@Reg,'Aircraft'),
(100,30,@Reg,'Aircraft'),
(80,40,@Reg,'Aircraft'),
(60,50,@Reg,'Aircraft');
;with Cte AS
(
SELECT DISTINCT TOP 1
tReg.ID AS 'RegID'
,tReg.Reg AS 'Reg'
,(SELECT TOP 1 dbo.FormatedLifeCodeValue(tLifeCode_ID,tRegJourneyLogBookLifeCodeEvents.LifeTotal,1)
FROM tRegJourneyLogBookLifeCodeEvents
JOIN tRegJourneyLogBook ON tRegJourneyLogBook.ID = tRegJourneyLogBookLifeCodeEvents.tRegJourneyLogBook_ID
JOIN tRegJourney ON tRegJourney.ID = tRegJourneyLogBook.tRegJourney_ID
JOIN tLogBook ON tLogBook.ID = tRegJourneyLogBook.tLogBook_ID
AND tLogBook.tAsset_ID = tReg.tAsset_ID
JOIN tLifeCode ON tRegJourneyLogBookLifeCodeEvents.tLifeCode_ID = tLifeCode.ID
WHERE tLifeCode.RegJourneyHours = 1
ORDER BY tRegJourney.TakeOffTime DESC) AS 'Current Hours'
,(SELECT TOP 1 --dbo.FormatedLifeCodeValue(tLifeCode_ID,tRegJourneyLogBookLifeCodeEvents.LifeTotal,1)
CAST(ROUND(CAST(tRegJourneyLogBookLifeCodeEvents.LifeTotal /60 AS DECIMAL(10,2)),0)AS FLOAT)
FROM tRegJourneyLogBookLifeCodeEvents
JOIN tRegJourneyLogBook ON tRegJourneyLogBook.ID = tRegJourneyLogBookLifeCodeEvents.tRegJourneyLogBook_ID
JOIN tRegJourney ON tRegJourney.ID = tRegJourneyLogBook.tRegJourney_ID
JOIN tLogBook ON tLogBook.ID = tRegJourneyLogBook.tLogBook_ID
AND tLogBook.tAsset_ID = tReg.tAsset_ID
JOIN tLifeCode ON tRegJourneyLogBookLifeCodeEvents.tLifeCode_ID = tLifeCode.ID
WHERE tLifeCode.RegJourneyHours = 1
ORDER BY tRegJourney.TakeOffTime DESC) AS CurrentHours
,(SELECT TOP 1 dbo.FormatedLifeCodeValue(tLifeCode_ID,tRegJourneyLogBookLifeCodeEvents.LifeTotal,1)
--CAST((ROUND(tRegJourneyLogBookLifeCodeEvents.LifeTotal/60,0) )AS DECIMAL(10,0))
FROM tRegJourneyLogBookLifeCodeEvents
JOIN tRegJourneyLogBook ON tRegJourneyLogBook.ID = tRegJourneyLogBookLifeCodeEvents.tRegJourneyLogBook_ID
JOIN tRegJourney ON tRegJourney.ID = tRegJourneyLogBook.tRegJourney_ID
JOIN tLogBook ON tLogBook.ID = tRegJourneyLogBook.tLogBook_ID AND tLogBook.tAsset_ID = tReg.tAsset_ID
JOIN tLifeCode ON tRegJourneyLogBookLifeCodeEvents.tLifeCode_ID = tLifeCode.ID
WHERE tLifeCode.RegJourneyLandings = 1
ORDER BY tRegJourney.TakeOffTime DESC ) AS 'Current CyclesOrig'
,(SELECT TOP 1 CAST(tRegJourneyLogBookLifeCodeEvents.LifeTotal AS DECIMAL(10,0))
-- CAST((ROUND(tRegJourneyLogBookLifeCodeEvents.LifeTotal/60,0) )AS DECIMAL(10,0))
FROM tRegJourneyLogBookLifeCodeEvents
JOIN tRegJourneyLogBook ON tRegJourneyLogBook.ID = tRegJourneyLogBookLifeCodeEvents.tRegJourneyLogBook_ID
JOIN tRegJourney ON tRegJourney.ID = tRegJourneyLogBook.tRegJourney_ID
JOIN tLogBook ON tLogBook.ID = tRegJourneyLogBook.tLogBook_ID AND tLogBook.tAsset_ID = tReg.tAsset_ID
JOIN tLifeCode ON tRegJourneyLogBookLifeCodeEvents.tLifeCode_ID = tLifeCode.ID
WHERE tLifeCode.RegJourneyLandings = 1
ORDER BY tRegJourney.TakeOffTime DESC ) AS 'CurrentCycles'
FROM tRegJourneyLogBookLifeCodeEvents
JOIN tRegJourneyLogBook ON tRegJourneyLogBook.ID = tRegJourneyLogBookLifeCodeEvents.tRegJourneyLogBook_ID
JOIN tRegJourney ON tRegJourney.ID = tRegJourneyLogBook.tRegJourney_ID
JOIN tReg ON tReg.ID = tRegJourney.tReg_ID
JOIN tLogBook ON tLogBook.ID = tRegJourneyLogBook.tLogBook_ID AND tLogBook.tAsset_ID = tReg.tAsset_ID
WHERE tReg.ID IN (@Reg)
)
SELECT ROUND(CAST(CAST(Cte.CurrentCycles / CAST (1000 AS DECIMAL (10,3)) AS DECIMAL(9,6)) AS FLOAT ),0) AS CurrentCyc1 ,
ROUND(CAST(CAST(Cte.CurrentHours / CAST (1000 AS DECIMAL (10,3)) AS DECIMAL(9,6)) AS FLOAT ),0) AS CurrentHr1 ,RegID AS REGI,Reg AS RG
into #temp FROM Cte
update #temp SET REGI = 0 WHERE (REGI) >1
SELECT * into #temp2 FROM #temp
--select * from #temp2
select *, ROW_NUMBER() OVER(ORDER BY RegID) AS AA
into #temp3 from #temp2 RIGHT JOIN #Chart on #Chart.RegID = #temp2.REGI
update #temp3 SET CurrentCyc1 = 0 WHERE (AA) > 1
update #temp3 SET CurrentHr1 = 0 WHERE (AA) > 1
SELECT * FROM #temp3
--EXEC RANDD 1
Drop table #temp
DROP table #temp2
DROP table #temp3
SET NOCOUNT OFF
END
这是我的存储过程,其结果集将是。