Return data from multiple tables

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rafa³ Bielecki

    Return data from multiple tables

    Hi there,

    I have tables with such structure

    transaction_YYM M
    (idx,date,compa ny_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_070 1
    transaction_070 2
    transaction_070 3
    transaction_070 4
    and return all as one

    best regards
    Rafal


  • Dan Guzman

    #2
    Re: Return data from multiple tables

    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@bielecki .infowrote in message
    news:f2ubuk$i1o $1@nemesis.news .tpi.pl...
    Hi there,
    >
    I have tables with such structure
    >
    transaction_YYM M
    (idx,date,compa ny_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_070 1
    transaction_070 2
    transaction_070 3
    transaction_070 4
    and return all as one
    >
    best regards
    Rafal
    >

    Comment

    • Rafal Bielecki

      #3
      Re: Return data from multiple tables


      Uzytkownik "Dan Guzman" <guzmanda@nospa m-online.sbcgloba l.netnapisal w
      wiadomosci news:JOz4i.2288 9$JZ3.9830@news svr13.news.prod igy.net...
      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


      Comment

      • Erland Sommarskog

        #4
        Re: Return data from multiple tables

        Rafa³ Bielecki (rafal@bielecki .info) writes:
        I have tables with such structure
        >
        transaction_YYM M
        (idx,date,compa ny_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_070 1
        transaction_070 2
        transaction_070 3
        transaction_070 4
        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@sommarsk og.se

        Books Online for SQL Server 2005 at

        Books Online for SQL Server 2000 at

        Comment

        Working...