?
Solved

UNION ALL AND GROUP BY

Posted on 2011-03-04
4
Medium Priority
?
388 Views
Last Modified: 2012-05-11
Is it possible to run in the same sql 2000 query a union all and group by statement

what I am doing is creating a view from 4 different tables which hold a siteid in them.
then i want to get a unique list of them but dont really want to do this in 2 views

create view zAllSiteid

select call_siteid as siteid from tbl_calls
union all
select  inventory_siteid as siteid from tbl_inventory
union all
select orders_siteid as siteid from tbl_Orders


then I would do a


select siteid from zAllSite
group by siteid
order by siteid

can i do this in one query?





0
Comment
Question by:Chris Michalczuk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 35035293
try this

Select x.siteid from(
select siteid from zAllSite
group by siteid ) x

order by x.siteid
0
 
LVL 9

Assisted Solution

by:mayank_joshi
mayank_joshi earned 2000 total points
ID: 35035311
try the concept of derived table :-

select siteid from (select call_siteid as siteid from tbl_calls
union all
select  inventory_siteid as siteid from tbl_inventory
union all
select orders_siteid as siteid from tbl_Orders)  derived_table
group by siteid
order by siteid

0
 
LVL 9

Accepted Solution

by:
mayank_joshi earned 2000 total points
ID: 35035331
select  derived_table.siteid from (select call_siteid as siteid from tbl_calls
union all
select  inventory_siteid as siteid from tbl_inventory
union all
select orders_siteid as siteid from tbl_Orders)  derived_table
group by  derived_table.siteid
order by  derived_table.siteid

Open in new window

0
 

Author Closing Comment

by:Chris Michalczuk
ID: 35035643
thanks derived table concept is good solution for what i needed to do
0

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article will provide a solution for an error that might occur installing a new SQL 2005 64-bit cluster. This article will assume that you are fully prepared to complete the installation and describes the error as it occurred durin…
INTRODUCTION: While tying your database objects into builds and your enterprise source control system takes a third-party product (like Visual Studio Database Edition or Red-Gate's SQL Source Control), you can achieve some protection using a sing…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question