April 27, 2016 at 3:54 pm
In a new t-sql 2012 query, I got the correct results to display by using several temp tables. However I am wondering if there is another way to come up with the same results and not use temp tables.
Here is the existing t-sql 2012 query:
Declare @personID int = 123999
SELECT t.testID ,t.name ,t.code ,ts.personID ,ts.date ,ts.scaleScore ,ts.result
INTO #ELDASummary
FROM Test t
join TestScore ts
on t.testID = ts.testID
and t.code = 'XXX2'
where personID = @personID
order by t.testID
select Test.parentID,Test.testID,Test.name
INTO #ELDACrossReference
FROM Test
join #ELDASummary on Test.parentID = #EldaSummary.testID
select #ELDACrossReference.parentID, #ELDACrossReference.testID,TestScore.personID,#ELDACrossReference.name ,TestScore.scaleScore,TestScore.result
into #ELDADetail
from TestScore TestScore
join #ELDACrossReference on #ELDACrossReference.testID = TestScore.testID
where personID = @personID
order by parentID,testID
select #ELDASummary.personID as personID,#ELDADetail.parentID as ELDADetailparentID, #ELDADetail.testID as ELDADetailtestID,#ELDASummary.name as ELDASummaryname
,#ELDASummary.Code as ELDASummaryCode,#ELDASummary.date as ELDASummarydate ,#ELDASummary.scaleScore as ELDASummaryscaleScore
,#ELDASummary.result as ELDASummaryresult,#ELDADetail.name as ELDADetailname
,#ELDADetail.scalescore as ELDADetailscalescore,#ELDADetail.result as ELDADetailresult
from #ELDASummary
join #ELDADetail on #ELDADetail.parentID = #ELDASummary.testid
order by personID,ELDADetailparentID, ELDADetailtestID
drop table #ELDASummary
drop table #ELDACrossReference
drop table #ELDADetail
Thus could you tell me how to rewrite this query using a cte, using several joins, subquery, and/or another way to accomplish the same result?
April 27, 2016 at 5:48 pm
Got table definitions? Sample data? (Create table and insert scripts)... can be completely fake, just representative.
April 27, 2016 at 6:11 pm
CTE's would work, as well as subqueries(same thing,really , just different format.
here's my version of your post as CTE's:
DECLARE @personID INT = 123999;
WITH ELDASummary
AS (SELECT t.testID,
t.NAME,
t.code,
ts.personID,
ts.date,
ts.scaleScore,
ts.result
FROM Test t
JOIN TestScore ts
ON t.testID = ts.testID
AND t.code = 'XXX2'
WHERE personID = @personID),
ELDACrossReference
AS (SELECT Test.parentID,
Test.testID,
Test.NAME
FROM Test
JOIN ELDASummary
ON Test.parentID = EldaSummary.testID),
ELDADetail
AS (SELECT ELDACrossReference.parentID,
ELDACrossReference.testID,
TestScore.personID,
ELDACrossReference.NAME,
TestScore.scaleScore,
TestScore.result
FROM TestScore TestScore
JOIN ELDACrossReference
ON ELDACrossReference.testID = TestScore.testID
WHERE personID = @personID)
SELECT ELDASummary.personID AS personID,
ELDADetail.parentID AS ELDADetailparentID,
ELDADetail.testID AS ELDADetailtestID,
ELDASummary.NAME AS ELDASummaryname,
ELDASummary.Code AS ELDASummaryCode,
ELDASummary.date AS ELDASummarydate,
ELDASummary.scaleScore AS ELDASummaryscaleScore,
ELDASummary.result AS ELDASummaryresult,
ELDADetail.NAME AS ELDADetailname,
ELDADetail.scalescore AS ELDADetailscalescore,
ELDADetail.result AS ELDADetailresult
FROM ELDASummary
JOIN ELDADetail
ON ELDADetail.parentID = ELDASummary.testid
ORDER BY personID,
ELDADetailparentID,
ELDADetailtestID
Lowell
April 28, 2016 at 9:16 am
What format is the most efficient for executing the best? subqueries, temp tables, or cte?
April 28, 2016 at 9:54 am
wendy elizabeth (4/28/2016)
What format is the most efficient for executing the best? subqueries, temp tables, or cte?
It depends. The best you can do is test the different options and see which works best regarding speed, scalability, use of resources, etc.
Once you've done the testing, test some more.
April 28, 2016 at 2:37 pm
Those ORDER BY clauses in your SELECT INTO queries aren't doing you any favors.
-- Itzik Ben-Gan 2001
Viewing 6 posts - 1 through 6 (of 6 total)
You must be logged in to reply to this topic. Login to reply