Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Can I make a temp table with a union all select?

Author  Topic 

crugerenator
Posting Yak Master

126 Posts

Posted - 2008-03-26 : 18:37:17
I'm having trouble creating a temp table out of a select statement that uses multipe union alls.

Here's what I have, I'm trying to get the results of this query into a temp table...

select
parent,
(select cst_id from co_customer (nolock) where cst_key = Parent) as cst_id,
(select cst_name_cp from co_customer (nolock) where cst_key = Parent) as cst_name_cp,
(select org_total_assets_ext from dbo.co_organization_ext where org_cst_key_ext = parent) as Parent_Total_assets,
sum(own_assets) as Total_child_own_assets

from
(
Select parent,
Child,
(select org_own_assets_ext from dbo.co_organization_ext where org_cst_key_ext = child) as Own_assets

from
(Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,1) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,1) is not null
union all

Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,2) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,2) is not null
union all

Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,3) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,3) is not null
union all

Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,4) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,4) is not null
union all

Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,5) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,5) is not null
union all

Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,6) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,6) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,7) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,7) is not null )as c
) as d

group by parent

having sum(own_assets) <> (select org_total_assets_ext from dbo.co_organization_ext where org_cst_key_ext = parent)

nr
SQLTeam MVY

12543 Posts

Posted - 2008-03-26 : 21:36:47
select ...
into #a
from ...
union all
select ...

but in your case as you already have a derived table I think you can just

select
parent,
(select cst_id from co_customer (nolock) where cst_key = Parent) as cst_id,
(select cst_name_cp from co_customer (nolock) where cst_key = Parent) as cst_name_cp,
(select org_total_assets_ext from dbo.co_organization_ext where org_cst_key_ext = parent) as Parent_Total_assets,
sum(own_assets) as Total_child_own_assets
into #temptbl
from
...


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

crugerenator
Posting Yak Master

126 Posts

Posted - 2008-03-27 : 14:37:25
Perfect, thanks. I was trying to put the into #temp line after the union alls.

Thanks again!
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-27 : 15:59:50
Also, I think you can replace whole derived table c with
SELECT		c.Cst_key AS Child,
dbo.return_org_parent(c.cst_key, 0, x.Number) AS Parent
FROM co_customer AS c WITH (NOLOCK)
INNER JOIN master..spt_values AS x ON x.Type = 'p'
WHERE c.cst_type = 'Organization'
AND c.cst_delete_flag = 0
AND x.Number BETWEEN 1 AND 7
AND dbo.return_org_parent(c.cst_key, 0, x.Number) IS NOT NULL



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-03-27 : 16:21:18
quote:
Originally posted by crugerenator

select
parent,
(select cst_id from co_customer (nolock) where cst_key = Parent) as cst_id,
(select cst_name_cp from co_customer (nolock) where cst_key = Parent) as cst_name_cp,
(select org_total_assets_ext from dbo.co_organization_ext where org_cst_key_ext = parent) as Parent_Total_assets,
sum(own_assets) as Total_child_own_assets
from
(
Select parent,
Child,
(select org_own_assets_ext from dbo.co_organization_ext where org_cst_key_ext = child) as Own_assets
from
(Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,1) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,1) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,2) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,2) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,3) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,3) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,4) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,4) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,5) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,5) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,6) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,6) is not null
union all
Select Cst_key as Child,
dbo.return_org_parent(cst_key,0,7) as Parent
from co_customer (nolock)
where cst_type = 'Organization'
and cst_delete_flag = 0
and dbo.return_org_parent(cst_key,0,7) is not null )as c
) as d
group by parent
having sum(own_assets) <> (select org_total_assets_ext from dbo.co_organization_ext where org_cst_key_ext = parent)


Hrmm... I think you can rewrite the whole shebang as following

SELECT q.Parent,
w.cst_ID AS cst_ID,
w.cst_Name_Cp AS cst_Name_Cp,
SUM(ISNULL(y2.org_total_assets_ext, 0)) AS Parent_Total_assets,
SUM(ISNULL(y1.org_own_assets_ext, 0)) AS Total_child_own_assets
FROM (
SELECT c.Cst_Key AS Child,
dbo.Return_Org_Parent(c.cst_Key, 0, x.Number) AS Parent
FROM co_Customer AS c WITH (NOLOCK)
INNER JOIN master..spt_values AS x ON x.Type = 'p'
WHERE c.cst_Type = 'Organization'
AND c.cst_Delete_Flag = 0
AND x.Number BETWEEN 1 AND 7
) AS q
LEFT JOIN dbo.co_organization_ext AS y1 ON y1.org_cst_key_ext = q.Child
LEFT JOIN dbo.co_organization_ext AS y2 ON y2.org_cst_key_ext = q.Parent
LEFT JOIN co_Customer AS w WITH (NOLOCK) ON w.cst_Key = q.Parent
WHERE q.Parent IS NOT NULL
GROUP BY q.Parent,
w.cst_ID,
w.cst_Name_Cp
HAVING SUM(ISNULL(y1.org_own_assets_ext, 0)) <> SUM(ISNULL(y2.org_total_assets_ext, 0))



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-01 : 02:41:59
Well, did my suggestions help at all?



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-08 : 02:53:05
No answer yet...



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page

nr
SQLTeam MVY

12543 Posts

Posted - 2008-04-08 : 04:56:32
Think the OP got what he thought he needed and went away.

==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2008-04-08 : 04:59:12
Most probably yes. I agree with you.
I just thought it would be nice to have some feedback if the rewritten query returned same data as original query.



E 12°55'05.25"
N 56°04'39.16"
Go to Top of Page
   

- Advertisement -