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.
| 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...selectparent,(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_assetsfrom(Select parent,Child, (select org_own_assets_ext from dbo.co_organization_ext where org_cst_key_ext = child) as Own_assetsfrom(Select Cst_key as Child,dbo.return_org_parent(cst_key,0,1) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,1) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,2) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,2) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,3) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,3) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,4) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,4) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,5) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,5) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,6) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,6) is not nullunion allSelect Cst_key as Child,dbo.return_org_parent(cst_key,0,7) as Parentfrom 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 dgroup by parenthaving 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 #afrom ...union allselect ...but in your case as you already have a derived table I think you can justselectparent,(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_assetsinto #temptblfrom...==========================================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. |
 |
|
|
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! |
 |
|
|
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 ParentFROM 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" |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-03-27 : 16:21:18
|
quote: Originally posted by crugerenator selectparent,(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_assetsfrom(Select parent,Child, (select org_own_assets_ext from dbo.co_organization_ext where org_cst_key_ext = child) as Own_assetsfrom(Select Cst_key as Child,dbo.return_org_parent(cst_key,0,1) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,1) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,2) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,2) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,3) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,3) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,4) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,4) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,5) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,5) is not nullunion all Select Cst_key as Child,dbo.return_org_parent(cst_key,0,6) as Parentfrom co_customer (nolock)where cst_type = 'Organization'and cst_delete_flag = 0 and dbo.return_org_parent(cst_key,0,6) is not nullunion allSelect Cst_key as Child,dbo.return_org_parent(cst_key,0,7) as Parentfrom 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 dgroup by parenthaving 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 followingSELECT 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_assetsFROM ( 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 qLEFT JOIN dbo.co_organization_ext AS y1 ON y1.org_cst_key_ext = q.ChildLEFT JOIN dbo.co_organization_ext AS y2 ON y2.org_cst_key_ext = q.ParentLEFT JOIN co_Customer AS w WITH (NOLOCK) ON w.cst_Key = q.ParentWHERE q.Parent IS NOT NULLGROUP BY q.Parent, w.cst_ID, w.cst_Name_CpHAVING 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" |
 |
|
|
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" |
 |
|
|
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" |
 |
|
|
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. |
 |
|
|
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" |
 |
|
|
|
|
|
|
|