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. Difference in Sum(column) between two tables

Difference in Sum(column) between two tables

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

    Hi Using Microsoft.ACE.OLEDB.12.0 to access an MS Access 2007 database from a VB.net (2010) project.

    SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], sum(tStk.Qty) as [StkTot], IIF(tHst.HstTot is null,0, tHst.HstTot) AS [HstTot], sum(tStk.Qty) - IIF(tHst.HstTot is null,0, tHst.HstTot) AS [Difference]
    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 tStk.WH + ' - ' + tStk.StockCode =tHst.WP
    WHERE tStk.[difference] <> 0
    GROUP BY tStk.WH+ ' - ' + tStk.StockCode , tHst.HstTot

    I only want rows where the sum of the item qty is Different in the two tables The line:

    WHERE tStk.[difference] <> 0

    does not work. If I run the query from MS Access, it is asking for a value for the parameter tStk.[difference] I also tried

    WHERE sum(tStk.Qty) <> tHST.Qty

    but aggregate functions are not allowed in a WHERE clause.

    D J 2 Replies Last reply
    0
    • R Richard Berry100

      Hi Using Microsoft.ACE.OLEDB.12.0 to access an MS Access 2007 database from a VB.net (2010) project.

      SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], sum(tStk.Qty) as [StkTot], IIF(tHst.HstTot is null,0, tHst.HstTot) AS [HstTot], sum(tStk.Qty) - IIF(tHst.HstTot is null,0, tHst.HstTot) AS [Difference]
      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 tStk.WH + ' - ' + tStk.StockCode =tHst.WP
      WHERE tStk.[difference] <> 0
      GROUP BY tStk.WH+ ' - ' + tStk.StockCode , tHst.HstTot

      I only want rows where the sum of the item qty is Different in the two tables The line:

      WHERE tStk.[difference] <> 0

      does not work. If I run the query from MS Access, it is asking for a value for the parameter tStk.[difference] I also tried

      WHERE sum(tStk.Qty) <> tHST.Qty

      but aggregate functions are not allowed in a WHERE clause.

      D Offline
      D Offline
      David Mujica
      wrote on last edited by
      #2

      Try taking your existing query and make it a sub-query. Some thing like: Select * from ("put your query here") XX where XX.tStk.Qty <> tHST.Qty Remember to remove your "Where" clause from your inner query.

      R 1 Reply Last reply
      0
      • R Richard Berry100

        Hi Using Microsoft.ACE.OLEDB.12.0 to access an MS Access 2007 database from a VB.net (2010) project.

        SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], sum(tStk.Qty) as [StkTot], IIF(tHst.HstTot is null,0, tHst.HstTot) AS [HstTot], sum(tStk.Qty) - IIF(tHst.HstTot is null,0, tHst.HstTot) AS [Difference]
        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 tStk.WH + ' - ' + tStk.StockCode =tHst.WP
        WHERE tStk.[difference] <> 0
        GROUP BY tStk.WH+ ' - ' + tStk.StockCode , tHst.HstTot

        I only want rows where the sum of the item qty is Different in the two tables The line:

        WHERE tStk.[difference] <> 0

        does not work. If I run the query from MS Access, it is asking for a value for the parameter tStk.[difference] I also tried

        WHERE sum(tStk.Qty) <> tHST.Qty

        but aggregate functions are not allowed in a WHERE clause.

        J Offline
        J Offline
        Jorgen Andersson
        wrote on last edited by
        #3

        Maybe HAVING sum(tStk.Qty) <> tHST.Qty[^] would work better

        "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

        R 1 Reply Last reply
        0
        • J Jorgen Andersson

          Maybe HAVING sum(tStk.Qty) <> tHST.Qty[^] would work better

          "The ones who care enough to do it right care too much to compromise." Matthew Faithfull

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

          Awesome! Thanks Jörgen

          SELECT tStk.WH + ' - ' + tStk.StockCode as [WP], sum(tStk.Qty) as [StkTot], IIF(tHst.HstTot is null,0, tHst.HstTot) AS [HstTot], sum(tStk.Qty) - IIF(tHst.HstTot is null,0, tHst.HstTot) AS [Difference]
          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 tStk.WH + ' - ' + tStk.StockCode =tHst.WP
          GROUP BY tStk.WH+ ' - ' + tStk.StockCode , tHst.HstTot
          HAVING sum(tStk.Qty) <>tHst.HstTot

          I see that: Aggregate functions can be used as expressions only in the following: The select list of a SELECT statement (either a subquery or an outer query). A COMPUTE or COMPUTE BY clause. A HAVING clause.

          1 Reply Last reply
          0
          • D David Mujica

            Try taking your existing query and make it a sub-query. Some thing like: Select * from ("put your query here") XX where XX.tStk.Qty <> tHST.Qty Remember to remove your "Where" clause from your inner query.

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

            Thanks for your reply David. Not sure if I understood correctly, but kept getting 'syntax error'

            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