dbTalk Databases Forums  

Re: Return data from multiple tables

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Re: Return data from multiple tables in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dan Guzman
 
Posts: n/a

Default Re: Return data from multiple tables - 05-22-2007 , 05:42 AM






Quote:
I want to define query (maybe view, procedure):
select * from [???] where date>='2007-01-01' and date<='2007-04-30'
A UNION ALL query will combine multiple result sets:

SELECT *
FROM dbo.transaction_0701
UNION ALL
SELECT *
FROM dbo.transaction_0702
UNION ALL
SELECT *
FROM dbo.transaction_0703
UNION ALL
SELECT *
FROM dbo.transaction_0704

You can specify an explicit column list (a Best Practice) and encapsulate
the query in a view to facilitate reuse. You might also consider creating a
partitioned view (or a partitioned table if you are running SQL 2005
Enterprise Edition). See the Books Online for more information.


--
Hope this helps.

Dan Guzman
SQL Server MVP

"Rafał Bielecki" <rafal (AT) bielecki (DOT) info> wrote

Quote:
Hi there,

I have tables with such structure

transaction_YYMM
(idx,date,company_id,value)

where YYMM stands for 2digits year and month
I want to define query (maybe view, procedure):
select * from [???] where date>='2007-01-01' and date<='2007-04-30'
which will grab data from
transaction_0701
transaction_0702
transaction_0703
transaction_0704
and return all as one

best regards
Rafal



Reply With Quote
  #2  
Old   
Rafal Bielecki
 
Posts: n/a

Default Re: Return data from multiple tables - 05-22-2007 , 06:42 AM







Uzytkownik "Dan Guzman" <guzmanda (AT) nospam-online (DOT) sbcglobal.net> napisal w
wiadomosci news:JOz4i.22889$JZ3.9830 (AT) newssvr13 (DOT) news.prodigy.net...

Quote:
A UNION ALL query will combine multiple result sets:

SELECT *
FROM dbo.transaction_0701
UNION ALL
SELECT *
FROM dbo.transaction_0702
UNION ALL
SELECT *
FROM dbo.transaction_0703
UNION ALL
SELECT *
FROM dbo.transaction_0704

You can specify an explicit column list (a Best Practice) and encapsulate
the query in a view to facilitate reuse. You might also consider creating
a partitioned view (or a partitioned table if you are running SQL 2005
Enterprise Edition). See the Books Online for more information.
thank you Dan, your help is very important to me
Rafal




Reply With Quote
  #3  
Old   
Erland Sommarskog
 
Posts: n/a

Default Re: Return data from multiple tables - 05-22-2007 , 05:04 PM



Rafał Bielecki (rafal (AT) bielecki (DOT) info) writes:
Quote:
I have tables with such structure

transaction_YYMM
(idx,date,company_id,value)

where YYMM stands for 2digits year and month
I want to define query (maybe view, procedure):
select * from [???] where date>='2007-01-01' and date<='2007-04-30'
which will grab data from
transaction_0701
transaction_0702
transaction_0703
transaction_0704
and return all as one
In additions to Dan's response, I like to point out that from a logical
point of view, the above design is flawd. It's a lot easier to deal with a
single table. If there are enourmous volumes, it can still be motivated
with partitioning, but then we are talking enourmous values like tens
of millions of rows per month.



--
Erland Sommarskog, SQL Server MVP, esquel (AT) sommarskog (DOT) se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.