Solved

UNION ALL AND GROUP BY

Posted on 2011-03-04
4
380 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 500 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 500 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

691 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