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. JOIN 3 Tables

JOIN 3 Tables

Scheduled Pinned Locked Moved Database
saleshelp
6 Posts 2 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.
  • R Offline
    R Offline
    Richard Berry100
    wrote on last edited by
    #1

    Hi I wonder if you could assist with a JOIN on three tables. I just cant see where I am going wrong. StkQty Join Stk History - WORKS

    SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], tHst.HstTot
    FROM stock_qty AS tStk
    LEFT JOIN (SELECT stock_history.Location + ' - ' + stock_history.StockCode AS [WP], sum(stock_history.Qty) as [HstTot]
    FROM stock_history
    GROUP BY stock_history.Location+ ' - ' + stock_history.StockCode) AS [tHst] ON tHst.WP =tStk.WH + ' - ' + tStk.StockCode
    GROUP BY tStk.WH + ' - ' + tStk.StockCode,tHst.HstTot

    StkQty Join SalesOrders - WORKS

    SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], tSO.SoTot
    FROM stock_qty AS tStk
    LEFT JOIN (SELECT sales_order_header.Wh + ' - ' + sales_order_detail.StockCode as [WP], sum(sales_order_detail.QtyDespatched) as [SoTot]
    FROM sales_order_detail, sales_order_header WHERE sales_order_detail.SalesOrder = sales_order_header.SalesOrder
    GROUP BY sales_order_header.Wh + ' - ' + sales_order_detail.StockCode) as tSO ON tSO.WP = tStk.WH + ' - ' + tStk.StockCode
    GROUP BY tStk.WH + ' - ' + tStk.StockCode,tSO.SoTot

    StQty JOIN StkHistory JOIN SalesOrders - FAIL Syntax Error (missing operator)...

    SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], tSO.SoTot, tHst.HstTot
    FROM stock_qty AS tStk
    LEFT JOIN (SELECT sales_order_header.Wh + ' - ' + sales_order_detail.StockCode as [WP], sum(sales_order_detail.QtyDespatched) as [SoTot]
    FROM sales_order_detail, sales_order_header WHERE sales_order_detail.SalesOrder = sales_order_header.SalesOrder
    GROUP BY sales_order_header.Wh + ' - ' + sales_order_detail.StockCode) as tSO ON tSO.WP = tStk.WH + ' - ' + tStk.StockCode
    LEFT JOIN (SELECT stock_history.Location + ' - ' + stock_history.StockCode AS [WP], sum(stock_history.Qty) as [HstTot]
    FROM stock_history
    GROUP BY stock_history.Location+ ' - ' + stock_history.StockCode) AS [tHst] ON tHst.WP =tStk.WH + ' - ' + tStk.StockCode
    GROUP BY tStk.WH + ' - ' + tStk.StockCode, tSO.SoTot, tHst.HstTot

    M 1 Reply Last reply
    0
    • R Richard Berry100

      Hi I wonder if you could assist with a JOIN on three tables. I just cant see where I am going wrong. StkQty Join Stk History - WORKS

      SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], tHst.HstTot
      FROM stock_qty AS tStk
      LEFT JOIN (SELECT stock_history.Location + ' - ' + stock_history.StockCode AS [WP], sum(stock_history.Qty) as [HstTot]
      FROM stock_history
      GROUP BY stock_history.Location+ ' - ' + stock_history.StockCode) AS [tHst] ON tHst.WP =tStk.WH + ' - ' + tStk.StockCode
      GROUP BY tStk.WH + ' - ' + tStk.StockCode,tHst.HstTot

      StkQty Join SalesOrders - WORKS

      SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], tSO.SoTot
      FROM stock_qty AS tStk
      LEFT JOIN (SELECT sales_order_header.Wh + ' - ' + sales_order_detail.StockCode as [WP], sum(sales_order_detail.QtyDespatched) as [SoTot]
      FROM sales_order_detail, sales_order_header WHERE sales_order_detail.SalesOrder = sales_order_header.SalesOrder
      GROUP BY sales_order_header.Wh + ' - ' + sales_order_detail.StockCode) as tSO ON tSO.WP = tStk.WH + ' - ' + tStk.StockCode
      GROUP BY tStk.WH + ' - ' + tStk.StockCode,tSO.SoTot

      StQty JOIN StkHistory JOIN SalesOrders - FAIL Syntax Error (missing operator)...

      SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], tSO.SoTot, tHst.HstTot
      FROM stock_qty AS tStk
      LEFT JOIN (SELECT sales_order_header.Wh + ' - ' + sales_order_detail.StockCode as [WP], sum(sales_order_detail.QtyDespatched) as [SoTot]
      FROM sales_order_detail, sales_order_header WHERE sales_order_detail.SalesOrder = sales_order_header.SalesOrder
      GROUP BY sales_order_header.Wh + ' - ' + sales_order_detail.StockCode) as tSO ON tSO.WP = tStk.WH + ' - ' + tStk.StockCode
      LEFT JOIN (SELECT stock_history.Location + ' - ' + stock_history.StockCode AS [WP], sum(stock_history.Qty) as [HstTot]
      FROM stock_history
      GROUP BY stock_history.Location+ ' - ' + stock_history.StockCode) AS [tHst] ON tHst.WP =tStk.WH + ' - ' + tStk.StockCode
      GROUP BY tStk.WH + ' - ' + tStk.StockCode, tSO.SoTot, tHst.HstTot

      M Offline
      M Offline
      Mycroft Holmes
      wrote on last edited by
      #2

      Richard I would like to make a suggestion - used IDs in all your sub queries, I presume your product records all have a ProductID. Then after you have the result you want join the result to your product table to get the descriptors. Joining on descriptors is not a good idea, joining on concatenated descriptors is even worse!

      Never underestimate the power of human stupidity RAH

      R 1 Reply Last reply
      0
      • M Mycroft Holmes

        Richard I would like to make a suggestion - used IDs in all your sub queries, I presume your product records all have a ProductID. Then after you have the result you want join the result to your product table to get the descriptors. Joining on descriptors is not a good idea, joining on concatenated descriptors is even worse!

        Never underestimate the power of human stupidity RAH

        R Offline
        R Offline
        Richard Berry100
        wrote on last edited by
        #3

        Hi Mycroft Thanks for the Advice - I agree but would have to re-write just about all the code in the app. The app was initially never intended to have multiple locations/warehouses - this requirement came later.... FRUSTRATION!!! So I have a stock_master table with all the product codes, and then a stock_qty table, which has all the stock codes for all the locations. So the only unique identifier I have is the stock_qty.WH + stock_qty.Product. Not sure if it would help if I added some sort of ID to this table, because the history table and sales order tables cant really share the same ID. Not sure if there is a way to add table columns with the concatenation of the location + product and use this, and if this could be done by some sort of trigger or something in the database as opposed to re-writing all the code in the app. I think I'm still stuck with getting these ugly queries to work.

        M 1 Reply Last reply
        0
        • R Richard Berry100

          Hi Mycroft Thanks for the Advice - I agree but would have to re-write just about all the code in the app. The app was initially never intended to have multiple locations/warehouses - this requirement came later.... FRUSTRATION!!! So I have a stock_master table with all the product codes, and then a stock_qty table, which has all the stock codes for all the locations. So the only unique identifier I have is the stock_qty.WH + stock_qty.Product. Not sure if it would help if I added some sort of ID to this table, because the history table and sales order tables cant really share the same ID. Not sure if there is a way to add table columns with the concatenation of the location + product and use this, and if this could be done by some sort of trigger or something in the database as opposed to re-writing all the code in the app. I think I'm still stuck with getting these ugly queries to work.

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          Oh shit - sorry to hear that. You might try creating views with calculated unique fields, I know it comes down to the same thing but it may be easier to work with the key fields rather than the concats. I would seriously look at a rewrite, you will spend more time pissing about trying to get a badly designed DB to work than you will on a rewrite. This I know, having done it a number of times and walked away from a contract where they refused to do the rewrite. SQL Server will allow you to add an identity field to an existing table, getting the foreign key in place is a bitch. If you can add the keys you can progressively rewrite the DB using those instead of a complete break.

          Never underestimate the power of human stupidity RAH

          R 1 Reply Last reply
          0
          • M Mycroft Holmes

            Oh shit - sorry to hear that. You might try creating views with calculated unique fields, I know it comes down to the same thing but it may be easier to work with the key fields rather than the concats. I would seriously look at a rewrite, you will spend more time pissing about trying to get a badly designed DB to work than you will on a rewrite. This I know, having done it a number of times and walked away from a contract where they refused to do the rewrite. SQL Server will allow you to add an identity field to an existing table, getting the foreign key in place is a bitch. If you can add the keys you can progressively rewrite the DB using those instead of a complete break.

            Never underestimate the power of human stupidity RAH

            R Offline
            R Offline
            Richard Berry100
            wrote on last edited by
            #5

            Thanks Mycroft - I think I am going to go with your suggestion - re-write. Do you have any pointers you could offer to handle the same stock code in various locations? Should I use an Identity field, or use Location+StockCode as the PK? My starting point should be the stock_master table, which at this stage only has unique stock items. So I could write some code to duplicate all these items across all the warehouses, and let SSE assign an ID value? I could then do away with the stock_qty table, since all this has is a location, stock_code and qty, so I could just add qty to the stock master table. Then for BatchHistory, StockHistory, SalesOrders, PurchaseOrders, WarehouseTransfer tables, I guess I could also write code to lookup the ID in the stock master and populate an ID field At the moment the app is using an MS Access DB which I am busy migrating to SSE because there are all sorts of problems with data not getting written to the database which I would assume could be due to a) Poor coding b) Concurrency problems with MS Access c) Possibly a bad network The company sells some pharmaceutical products which require batch traceability so I cant lose their data... So yeah, sure as hell paying my school fees on this one!!! And as for MS Access, lets just say we are not very good friends at the moment!

            M 1 Reply Last reply
            0
            • R Richard Berry100

              Thanks Mycroft - I think I am going to go with your suggestion - re-write. Do you have any pointers you could offer to handle the same stock code in various locations? Should I use an Identity field, or use Location+StockCode as the PK? My starting point should be the stock_master table, which at this stage only has unique stock items. So I could write some code to duplicate all these items across all the warehouses, and let SSE assign an ID value? I could then do away with the stock_qty table, since all this has is a location, stock_code and qty, so I could just add qty to the stock master table. Then for BatchHistory, StockHistory, SalesOrders, PurchaseOrders, WarehouseTransfer tables, I guess I could also write code to lookup the ID in the stock master and populate an ID field At the moment the app is using an MS Access DB which I am busy migrating to SSE because there are all sorts of problems with data not getting written to the database which I would assume could be due to a) Poor coding b) Concurrency problems with MS Access c) Possibly a bad network The company sells some pharmaceutical products which require batch traceability so I cant lose their data... So yeah, sure as hell paying my school fees on this one!!! And as for MS Access, lets just say we are not very good friends at the moment!

              M Offline
              M Offline
              Mycroft Holmes
              wrote on last edited by
              #6

              Your relationship with MS Access is going to get worse - sorry it really is a crap tool for database work. There are a number of schools of though for the distributed database. Some like to us GUIDs, others like the location/ID concatenation. There are some where you have a master server for such things as products, only HO can add a code etc. I don't even think there are good guidelines on which to use where. Personally I prefer the location/id solution, I know of at least 1 respected member here who would recommend the GUID path (PITA to read the ID) so it will be your choice. You should look into replication before you start, designing so the data can be consolidated from the start is always a GREAT idea. Depending on your business requirements I would opt for a parallel application and write a script that can migrate your data to the new structure. This allows you to completely redesign your structure getting rid of the crap design you have now. Don't finalise your DB design until the bulk of yor app development is done, then make sure your migration script works. It should be possible to migrate your data at any time and repeat until the dev is complete. Consider using a web based solution if the local speed is acceptable - eliminates the distributed problems but introduces others (uptime issues) and criticallity. I recommend not using the cloud if your data is both critical and confidential. Good Luck you have interesting time ahead of you.

              Never underestimate the power of human stupidity RAH

              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