I have two questions...
1. I have this long query where I want to get certain information out of and placed into a temp table. Is this the most efficient way or should I use a CTE?
SELECT n.DOC, ISNULL(pendinfo1, 0) AS PendInFO1, ISNULL(pendinfo2,0) AS pendinfo2, ISNULL(pendinfo3,0) AS pendinfo3,
ISNULL(pendFOover1,0) AS pendFOover1,ISNULL(pendFOover2,0) AS pendFOover2,ISNULL(pendFOover3,0) AS pendFOover3
into #temppd
FROM Natdocfile AS n
Left Join
---Pending in FO1
(Select t.doc, ISNULL(COUNT(t.clmn ), 0)as pendinfo1
from t2dibpend t
where DIB_MVT_SEQ_NUM IS NULL and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)a
on n.doc = a.doc
Left Join
--Pending in FO2
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendInFO2, max(dib_mvt_seq_num) as MaxFO2
From t2DibPend AS t
where(DIB_MVT_TYP='r') and not (org_id like 'R%' or org_id like 'S%' or org_id like 'V%' or
org_id like 'P%') and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)b
on n.doc=b.doc
LEFT JOIN
--Pending in FO3
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendInFO3, max(dib_mvt_seq_num)as MaxFO3
From t2DibPend t
where (DIB_MVT_TYP='t')
group by t.doc)c
on n.doc = c.doc
LEFT JOIN
--Pending in FO1 > 300
(Select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendFOover1
from t2dibpend t
where DIB_MVT_SEQ_NUM IS NULL and app_rcpdt< dateadd(dd,-300,getdate()) and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)d
on n.doc = d.doc
LEFT JOIN
--Pending in FO2 > 300
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendFOover2, max(dib_mvt_seq_num) as MaxoverFO2
From t2DibPend AS t
where(DIB_MVT_TYP='r') and not (org_id like 'R%' or org_id like 'S%' or org_id like 'V%' or
org_id like 'P%') and app_rcpdt< dateadd(dd,-300,getdate()) and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)f
on n.doc=f.doc
LEFT JOIN
--Pending in FO3 > 300
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendFOover3, max(dib_mvt_seq_num)as MaxoverFO3
From t2DibPend t
where (DIB_MVT_TYP='t')and app_rcpdt< dateadd(dd,-300,getdate()) and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)g
on n.doc=g.doc
2. I didn't know how to combine all three of the queries together so I did them separately and in the other table I'm adding them up to get what I want. So in the next query I'm doing this to get what I want:
(SELECT t.doc, SUM(t.pendinfo1 + t.pendinfo2 + t.pendinfo3) AS pendinfo
FROM (SELECT *
FROM #temppd) t
GROUP BY t.doc)d
ON n.doc = d.doc
LEFT JOIN
(SELECT t.doc, sum(t. PendFOover1 + t.PendFOover2 + t.PendFOover3) as pendfo300
FROM (SELECT *
FROM #temppd) t
GROUP BY t.doc)f
ON n.doc = f.doc
Is there an easier way to combine all? It does give me what I want.
1. I have this long query where I want to get certain information out of and placed into a temp table. Is this the most efficient way or should I use a CTE?
SELECT n.DOC, ISNULL(pendinfo1, 0) AS PendInFO1, ISNULL(pendinfo2,0) AS pendinfo2, ISNULL(pendinfo3,0) AS pendinfo3,
ISNULL(pendFOover1,0) AS pendFOover1,ISNULL(pendFOover2,0) AS pendFOover2,ISNULL(pendFOover3,0) AS pendFOover3
into #temppd
FROM Natdocfile AS n
Left Join
---Pending in FO1
(Select t.doc, ISNULL(COUNT(t.clmn ), 0)as pendinfo1
from t2dibpend t
where DIB_MVT_SEQ_NUM IS NULL and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)a
on n.doc = a.doc
Left Join
--Pending in FO2
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendInFO2, max(dib_mvt_seq_num) as MaxFO2
From t2DibPend AS t
where(DIB_MVT_TYP='r') and not (org_id like 'R%' or org_id like 'S%' or org_id like 'V%' or
org_id like 'P%') and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)b
on n.doc=b.doc
LEFT JOIN
--Pending in FO3
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendInFO3, max(dib_mvt_seq_num)as MaxFO3
From t2DibPend t
where (DIB_MVT_TYP='t')
group by t.doc)c
on n.doc = c.doc
LEFT JOIN
--Pending in FO1 > 300
(Select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendFOover1
from t2dibpend t
where DIB_MVT_SEQ_NUM IS NULL and app_rcpdt< dateadd(dd,-300,getdate()) and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)d
on n.doc = d.doc
LEFT JOIN
--Pending in FO2 > 300
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendFOover2, max(dib_mvt_seq_num) as MaxoverFO2
From t2DibPend AS t
where(DIB_MVT_TYP='r') and not (org_id like 'R%' or org_id like 'S%' or org_id like 'V%' or
org_id like 'P%') and app_rcpdt< dateadd(dd,-300,getdate()) and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)f
on n.doc=f.doc
LEFT JOIN
--Pending in FO3 > 300
(select t.doc, ISNULL(COUNT(t.clmn ), 0) as PendFOover3, max(dib_mvt_seq_num)as MaxoverFO3
From t2DibPend t
where (DIB_MVT_TYP='t')and app_rcpdt< dateadd(dd,-300,getdate()) and t.clmn in (select clmn from specnew.dbo.people where completedt is null)
group by t.doc)g
on n.doc=g.doc
2. I didn't know how to combine all three of the queries together so I did them separately and in the other table I'm adding them up to get what I want. So in the next query I'm doing this to get what I want:
(SELECT t.doc, SUM(t.pendinfo1 + t.pendinfo2 + t.pendinfo3) AS pendinfo
FROM (SELECT *
FROM #temppd) t
GROUP BY t.doc)d
ON n.doc = d.doc
LEFT JOIN
(SELECT t.doc, sum(t. PendFOover1 + t.PendFOover2 + t.PendFOover3) as pendfo300
FROM (SELECT *
FROM #temppd) t
GROUP BY t.doc)f
ON n.doc = f.doc
Is there an easier way to combine all? It does give me what I want.