Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Multiple queries as one table

Multiple queries as one table

Scheduled Pinned Locked Moved Database
databasesql-serversysadminhelp
6 Posts 4 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Morgs Morgan
    wrote on last edited by
    #1

    Hi guys, I have these three queries and i would like to join them as one table so that i can read from one table using my DataReader(). I don't want to use subqueries because certain queries will return more than one result and Sql Server does not like that one.

    (
    SELECT [number], [dateTime]
    FROM [Database].[dbo].[table]
    WHERE [dateTime]
    BETWEEN '2010-04-30 00:00:00' AND '2010-04-30 23:59:59' AND
    [number] NOT IN ('227000000', '2274000000', '2770000000')
    )
    (
    SELECT RIGHT ([number_pair], 11) AS [number_pair] FROM [Database].[dbo].[table]
    WHERE [number_pair] != '' AND [used] = '1' AND [number_pair] IN
    (
    SELECT [Database].[dbo].[table].[num_pair] FROM [Database].[dbo].[table]
    WHERE [dateTime] BETWEEN '2010-04-30 00:00:00' AND '2010-04-30 23:59:59' AND
    [number] NOT IN ('227000000', '2274000000', '2770000000')
    )
    )
    (
    SELECT column FROM [Database].[dbo].[table]
    WHERE [sent] = '1' AND
    [sent_date] BETWEEN '2010-04-30 00:00:00' AND '2010-04-30 23:59:59'
    )

    Please anyone help me :doh: Thanks, Morgs

    L 1 Reply Last reply
    0
    • M Morgs Morgan

      Hi guys, I have these three queries and i would like to join them as one table so that i can read from one table using my DataReader(). I don't want to use subqueries because certain queries will return more than one result and Sql Server does not like that one.

      (
      SELECT [number], [dateTime]
      FROM [Database].[dbo].[table]
      WHERE [dateTime]
      BETWEEN '2010-04-30 00:00:00' AND '2010-04-30 23:59:59' AND
      [number] NOT IN ('227000000', '2274000000', '2770000000')
      )
      (
      SELECT RIGHT ([number_pair], 11) AS [number_pair] FROM [Database].[dbo].[table]
      WHERE [number_pair] != '' AND [used] = '1' AND [number_pair] IN
      (
      SELECT [Database].[dbo].[table].[num_pair] FROM [Database].[dbo].[table]
      WHERE [dateTime] BETWEEN '2010-04-30 00:00:00' AND '2010-04-30 23:59:59' AND
      [number] NOT IN ('227000000', '2274000000', '2770000000')
      )
      )
      (
      SELECT column FROM [Database].[dbo].[table]
      WHERE [sent] = '1' AND
      [sent_date] BETWEEN '2010-04-30 00:00:00' AND '2010-04-30 23:59:59'
      )

      Please anyone help me :doh: Thanks, Morgs

      L Offline
      L Offline
      loyal ginger
      wrote on last edited by
      #2

      The three queries don't return the same number of columns so they can't be combined using "union". So first you need to make the three queries to return the same number of columns with corresponding columns having the same data types. Then you can use "union" or "union all" to combine them into one big table.

      M 1 Reply Last reply
      0
      • L loyal ginger

        The three queries don't return the same number of columns so they can't be combined using "union". So first you need to make the three queries to return the same number of columns with corresponding columns having the same data types. Then you can use "union" or "union all" to combine them into one big table.

        M Offline
        M Offline
        Morgs Morgan
        wrote on last edited by
        #3

        Hi Loyal, Thanks for your reply, well the problem is that these queries will return different number of columns and rows.e.g 1st query will return 3 columns, 2nd and 3rd will return 1 column

        J J 2 Replies Last reply
        0
        • M Morgs Morgan

          Hi Loyal, Thanks for your reply, well the problem is that these queries will return different number of columns and rows.e.g 1st query will return 3 columns, 2nd and 3rd will return 1 column

          J Offline
          J Offline
          J4amieC
          wrote on last edited by
          #4

          How do you expect to read from one table if they all have different number of columns? You can have 3 select queries in 1 stored procedure and still use DataReader. The DataReader class contains a NextReader() method which moves onto the next table in the resultset.

          M 1 Reply Last reply
          0
          • J J4amieC

            How do you expect to read from one table if they all have different number of columns? You can have 3 select queries in 1 stored procedure and still use DataReader. The DataReader class contains a NextReader() method which moves onto the next table in the resultset.

            M Offline
            M Offline
            Morgs Morgan
            wrote on last edited by
            #5

            Thanks alot, i will try this one

            1 Reply Last reply
            0
            • M Morgs Morgan

              Hi Loyal, Thanks for your reply, well the problem is that these queries will return different number of columns and rows.e.g 1st query will return 3 columns, 2nd and 3rd will return 1 column

              J Offline
              J Offline
              Johan Hakkesteegt
              wrote on last edited by
              #6

              That is not a problem. Simply determine which query returns the most columns, and add dummy columns to the other queries, such that each query returns the same amount of columns. use 0 for columns with numerical values, and '' for columns with text values. So for example:

              SELECT NumericalColumn1, NumericalColumn1, textColumn1
              FROM Table1
              WHERE blabla
              UNION ALL
              SELECT NumericalColumn1, 0, ''
              FROM Table2
              WHERE blabla
              UNION ALL
              SELECT 0, 0, textColumn1
              FROM Table3
              WHERE blabla

              My advice is free, and you may get what you paid for.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups