Solved

UNION ALL AND GROUP BY

Posted on 2011-03-04
4
361 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:chrismichalczuk
  • 2
4 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
Comment Utility
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
Comment Utility
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
Comment Utility
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:chrismichalczuk
Comment Utility
thanks derived table concept is good solution for what i needed to do
0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

I am showing a way to read/import the excel data in table using SQL server 2005... Suppose there is an Excel file "Book1" at location "C:\temp" with column "First Name" and "Last Name". Now to import this Excel data into the table, we will useā€¦
In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now