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. select data from two tables ...???

select data from two tables ...???

Scheduled Pinned Locked Moved Database
databasequestion
5 Posts 3 Posters 0 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.
  • V Offline
    V Offline
    vicky457
    wrote on last edited by
    #1

    Hi, I have two tables as following: Table1: AccountID Symbol Fee Price 1 AAA 10 150 1 AAA 20 250 1 BBB 30 350 2 AAA 40 450 2 CCC 50 550 3 DDD 60 650 4 EEE 70 750 6 HHH 15 250 Table2: AccountID Symbol Share Dollar 1 AAA 100 1100 1 BBB 200 2200 2 AAA 300 3300 2 AAA 400 4400 2 CCC 500 5500 3 DDD 600 6600 4 FFF 700 7700 5 GGG 800 8800 These two tables have two common fields: AccountId and Symbol. What I want to do is to get all the data from both tables based on the same accountid and the same symbol. If one table (A) doesn't contain the account or symbol in the other table (B), just fill the column in table (A) with blanks. The result should look like: AccountID Symbol Fee Price Share Dollar 1 AAA 10 150 100 1100 1 AAA 20 250 --- --- 1 BBB 30 350 200 2200 2 AAA 40 450 300 3300 2 AAA -- --- 400 4400 2 CCC 50 550 500 5500 3 DDD 60 650 600 6600 4 EEE 70 750 --- ---- 4 FFF -- --- 700 7700 5 GGG -- --- 800 8800 6 HHH 15 250 --- ---- The columns Fee and Price in Table1 don't have relationship with the columns Share and Dollar in Table2. All I need is to display data from both table based on Account and Symbol. And I need to do it using T-SQL. Any hint and suggestion would be appreciated. Thanks in advance!

    K J 2 Replies Last reply
    0
    • V vicky457

      Hi, I have two tables as following: Table1: AccountID Symbol Fee Price 1 AAA 10 150 1 AAA 20 250 1 BBB 30 350 2 AAA 40 450 2 CCC 50 550 3 DDD 60 650 4 EEE 70 750 6 HHH 15 250 Table2: AccountID Symbol Share Dollar 1 AAA 100 1100 1 BBB 200 2200 2 AAA 300 3300 2 AAA 400 4400 2 CCC 500 5500 3 DDD 600 6600 4 FFF 700 7700 5 GGG 800 8800 These two tables have two common fields: AccountId and Symbol. What I want to do is to get all the data from both tables based on the same accountid and the same symbol. If one table (A) doesn't contain the account or symbol in the other table (B), just fill the column in table (A) with blanks. The result should look like: AccountID Symbol Fee Price Share Dollar 1 AAA 10 150 100 1100 1 AAA 20 250 --- --- 1 BBB 30 350 200 2200 2 AAA 40 450 300 3300 2 AAA -- --- 400 4400 2 CCC 50 550 500 5500 3 DDD 60 650 600 6600 4 EEE 70 750 --- ---- 4 FFF -- --- 700 7700 5 GGG -- --- 800 8800 6 HHH 15 250 --- ---- The columns Fee and Price in Table1 don't have relationship with the columns Share and Dollar in Table2. All I need is to display data from both table based on Account and Symbol. And I need to do it using T-SQL. Any hint and suggestion would be appreciated. Thanks in advance!

      K Offline
      K Offline
      Krish KP
      wrote on last edited by
      #2

      SELECT a.AccountID, a.Symbol, a.Fee, a.Price, b.AccountID, b.Symbol, b.Share, b.Dollar FROM tableA a FULL OUTER JOIN tableB b IB a.AccountID = b.AccountID AND a.Symbol = b.Symbol

      Regards KP

      V 1 Reply Last reply
      0
      • V vicky457

        Hi, I have two tables as following: Table1: AccountID Symbol Fee Price 1 AAA 10 150 1 AAA 20 250 1 BBB 30 350 2 AAA 40 450 2 CCC 50 550 3 DDD 60 650 4 EEE 70 750 6 HHH 15 250 Table2: AccountID Symbol Share Dollar 1 AAA 100 1100 1 BBB 200 2200 2 AAA 300 3300 2 AAA 400 4400 2 CCC 500 5500 3 DDD 600 6600 4 FFF 700 7700 5 GGG 800 8800 These two tables have two common fields: AccountId and Symbol. What I want to do is to get all the data from both tables based on the same accountid and the same symbol. If one table (A) doesn't contain the account or symbol in the other table (B), just fill the column in table (A) with blanks. The result should look like: AccountID Symbol Fee Price Share Dollar 1 AAA 10 150 100 1100 1 AAA 20 250 --- --- 1 BBB 30 350 200 2200 2 AAA 40 450 300 3300 2 AAA -- --- 400 4400 2 CCC 50 550 500 5500 3 DDD 60 650 600 6600 4 EEE 70 750 --- ---- 4 FFF -- --- 700 7700 5 GGG -- --- 800 8800 6 HHH 15 250 --- ---- The columns Fee and Price in Table1 don't have relationship with the columns Share and Dollar in Table2. All I need is to display data from both table based on Account and Symbol. And I need to do it using T-SQL. Any hint and suggestion would be appreciated. Thanks in advance!

        J Offline
        J Offline
        jijoaresseriljose
        wrote on last edited by
        #3

        hi, This is the complete query to get your output......... SELECT accountid=case when isnull(a.AccountID,'')='' then b.accountid else a.accountid end, symbol=case when isnull(a.Symbol,'')='' then b.Symbol else a.Symbol end , a.Fee, a.Price, b.Share, b.Dollar FROM tableA a FULL outer JOIN tableB b on a.AccountID = b.AccountID AND a.Symbol = b.Symbol order by accountid,symbol regards

        jijo jose

        V 1 Reply Last reply
        0
        • J jijoaresseriljose

          hi, This is the complete query to get your output......... SELECT accountid=case when isnull(a.AccountID,'')='' then b.accountid else a.accountid end, symbol=case when isnull(a.Symbol,'')='' then b.Symbol else a.Symbol end , a.Fee, a.Price, b.Share, b.Dollar FROM tableA a FULL outer JOIN tableB b on a.AccountID = b.AccountID AND a.Symbol = b.Symbol order by accountid,symbol regards

          jijo jose

          V Offline
          V Offline
          vicky457
          wrote on last edited by
          #4

          Hi, Thank you very much! It works!

          1 Reply Last reply
          0
          • K Krish KP

            SELECT a.AccountID, a.Symbol, a.Fee, a.Price, b.AccountID, b.Symbol, b.Share, b.Dollar FROM tableA a FULL OUTER JOIN tableB b IB a.AccountID = b.AccountID AND a.Symbol = b.Symbol

            Regards KP

            V Offline
            V Offline
            vicky457
            wrote on last edited by
            #5

            Hi, Thank you for your help! I have gotten the answer from other site: SELECT IsNull(T1.AccountID, T2.AccountID) AS AccountID, ISNull(T1.Symbol, T2.Symbol) AS Symbol, T1.Fee, T1.Price, T2.Share, T2.Dollar FROM Table1 AS T1 FULL OUTER JOIN Table2 AS T2 ON T1.AccountID = T2.AccountID AND T1.Symbol = T2.Symbol ORDER BY AccountID, Symbol It's basically the same as the post from jose.

            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