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. Adding Sales Totals from multiple tables

Adding Sales Totals from multiple tables

Scheduled Pinned Locked Moved Database
sql-serversaleshelp
6 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.
  • J Offline
    J Offline
    jkirkerx
    wrote on last edited by
    #1

    I'm trying to write better TSQL to reduce the amount of functions I have to do things. Here I'm trying to add the total amount of sales from a column called GrandTotal from multiple tables into a single result. First I'm not sure if this is even possible to do, but I think it can be done. I've come this far, but I get 2 results returned. I'm just trying to consolidate them into one. I tried SELECT SUM(TotalAmount) as the wrapper, but I get a syntax error I tried UNION ALL without the )( around around it, but I get an error on the last ) The tables are identical in columns

    DECLARE @StartDate AS Date;
    DECLARE @StopDate AS Date;
    SET @StartDate = CONVERT(CHAR(10),GETDATE(),101);
    SET @StopDate = CONVERT(CHAR(10),DATEADD(d,1, GETDATE()),101);

    SELECT
    (
    SELECT SUM(GrandTotal)
    FROM CompletedOrdersHistory
    WHERE OrderStatus='COMPLETED'
    AND OrderDate >= @StartDate
    AND OrderDate < @StopDate
    )
    UNION ALL
    (
    SELECT SUM(GrandTotal)
    FROM CompletedOrders
    WHERE OrderStatus='COMPLETED'
    AND OrderDate >= @StartDate
    AND OrderDate < @StopDate
    )

    M J 2 Replies Last reply
    0
    • J jkirkerx

      I'm trying to write better TSQL to reduce the amount of functions I have to do things. Here I'm trying to add the total amount of sales from a column called GrandTotal from multiple tables into a single result. First I'm not sure if this is even possible to do, but I think it can be done. I've come this far, but I get 2 results returned. I'm just trying to consolidate them into one. I tried SELECT SUM(TotalAmount) as the wrapper, but I get a syntax error I tried UNION ALL without the )( around around it, but I get an error on the last ) The tables are identical in columns

      DECLARE @StartDate AS Date;
      DECLARE @StopDate AS Date;
      SET @StartDate = CONVERT(CHAR(10),GETDATE(),101);
      SET @StopDate = CONVERT(CHAR(10),DATEADD(d,1, GETDATE()),101);

      SELECT
      (
      SELECT SUM(GrandTotal)
      FROM CompletedOrdersHistory
      WHERE OrderStatus='COMPLETED'
      AND OrderDate >= @StartDate
      AND OrderDate < @StopDate
      )
      UNION ALL
      (
      SELECT SUM(GrandTotal)
      FROM CompletedOrders
      WHERE OrderStatus='COMPLETED'
      AND OrderDate >= @StartDate
      AND OrderDate < @StopDate
      )

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

      Try - note that I named the sum fields.

      Select sum(GTotal) Grand
      From(
      SELECT
      (
      SELECT SUM(GrandTotal) as GTotal
      FROM CompletedOrdersHistory
      WHERE OrderStatus='COMPLETED'
      AND OrderDate >= @StartDate
      AND OrderDate < @StopDate
      )
      UNION ALL
      (
      SELECT SUM(GrandTotal) as GTotal
      FROM CompletedOrders
      WHERE OrderStatus='COMPLETED'
      AND OrderDate >= @StartDate
      AND OrderDate < @StopDate
      ))

      Personally I would normally include the common ID and use a Group By and then an inner join to where you need the Grand Totals

      Never underestimate the power of human stupidity RAH

      J 1 Reply Last reply
      0
      • M Mycroft Holmes

        Try - note that I named the sum fields.

        Select sum(GTotal) Grand
        From(
        SELECT
        (
        SELECT SUM(GrandTotal) as GTotal
        FROM CompletedOrdersHistory
        WHERE OrderStatus='COMPLETED'
        AND OrderDate >= @StartDate
        AND OrderDate < @StopDate
        )
        UNION ALL
        (
        SELECT SUM(GrandTotal) as GTotal
        FROM CompletedOrders
        WHERE OrderStatus='COMPLETED'
        AND OrderDate >= @StartDate
        AND OrderDate < @StopDate
        ))

        Personally I would normally include the common ID and use a Group By and then an inner join to where you need the Grand Totals

        Never underestimate the power of human stupidity RAH

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

        Thanks! I understand the example, but I still get the ")" error in which I don't understand. There's only 3 of them. The common ID and the GroupBy with the inner join is going to take me some time to experiment with. I'm not really sure where to start with that one.

        1 Reply Last reply
        0
        • J jkirkerx

          I'm trying to write better TSQL to reduce the amount of functions I have to do things. Here I'm trying to add the total amount of sales from a column called GrandTotal from multiple tables into a single result. First I'm not sure if this is even possible to do, but I think it can be done. I've come this far, but I get 2 results returned. I'm just trying to consolidate them into one. I tried SELECT SUM(TotalAmount) as the wrapper, but I get a syntax error I tried UNION ALL without the )( around around it, but I get an error on the last ) The tables are identical in columns

          DECLARE @StartDate AS Date;
          DECLARE @StopDate AS Date;
          SET @StartDate = CONVERT(CHAR(10),GETDATE(),101);
          SET @StopDate = CONVERT(CHAR(10),DATEADD(d,1, GETDATE()),101);

          SELECT
          (
          SELECT SUM(GrandTotal)
          FROM CompletedOrdersHistory
          WHERE OrderStatus='COMPLETED'
          AND OrderDate >= @StartDate
          AND OrderDate < @StopDate
          )
          UNION ALL
          (
          SELECT SUM(GrandTotal)
          FROM CompletedOrders
          WHERE OrderStatus='COMPLETED'
          AND OrderDate >= @StartDate
          AND OrderDate < @StopDate
          )

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

          Try to turn it into a CTE and see if that works:

          WITH sums AS (
          SELECT SUM(GrandTotal) GrandTotal
          FROM CompletedOrdersHistory
          WHERE OrderStatus='COMPLETED'
          AND OrderDate >= @StartDate
          AND OrderDate < @StopDate
          UNION ALL
          SELECT SUM(GrandTotal) GrandTotal
          FROM CompletedOrders
          WHERE OrderStatus='COMPLETED'
          AND OrderDate >= @StartDate
          AND OrderDate < @StopDate
          )
          SELECT Sum(GrandTotal) AS GrandTotal
          FROM sums

          Or use a subquery which is the same thing but different syntax.

          Wrong is evil and must be defeated. - Jeff Ello[^]

          J 1 Reply Last reply
          0
          • J Jorgen Andersson

            Try to turn it into a CTE and see if that works:

            WITH sums AS (
            SELECT SUM(GrandTotal) GrandTotal
            FROM CompletedOrdersHistory
            WHERE OrderStatus='COMPLETED'
            AND OrderDate >= @StartDate
            AND OrderDate < @StopDate
            UNION ALL
            SELECT SUM(GrandTotal) GrandTotal
            FROM CompletedOrders
            WHERE OrderStatus='COMPLETED'
            AND OrderDate >= @StartDate
            AND OrderDate < @StopDate
            )
            SELECT Sum(GrandTotal) AS GrandTotal
            FROM sums

            Or use a subquery which is the same thing but different syntax.

            Wrong is evil and must be defeated. - Jeff Ello[^]

            J Offline
            J Offline
            jkirkerx
            wrote on last edited by
            #5

            Common Table Expression Yet another way to build more complex queries, Interesting I had no idea I could express it like that, and would of never thought of looking for it in search. I was close on the first couple of tries. Thanks Jorgen! I did read up on those articles you gave me, and have been designing my new stuff in such a manner. Writing TSQL for reports can get more complex to produce the desired results.

            J 1 Reply Last reply
            0
            • J jkirkerx

              Common Table Expression Yet another way to build more complex queries, Interesting I had no idea I could express it like that, and would of never thought of looking for it in search. I was close on the first couple of tries. Thanks Jorgen! I did read up on those articles you gave me, and have been designing my new stuff in such a manner. Writing TSQL for reports can get more complex to produce the desired results.

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

              Glad to be able to help. :) Appreciative feed back is the best driving force there is!

              Wrong is evil and must be defeated. - Jeff Ello[^]

              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