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. Union on a join

Union on a join

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

    I got myself in a pickle here. I started letting customers delete items in the store. But I copied the item into a separate table. I should of added a column status instead. But anyways, on my report for total items sold in the year, I did a join on the product table, to get the current cost, images, etc. So now that some items have been deleted, the record is no longer in the product table, but in the recycle table. I tried a union all, but it results in 2 records, in which the 2nd is null. I tried just union as well, but it produces an reader error. I'm not sure how to proceed on this, or what to words to use to do a search here. I'm stumped! [EDIT] Maybe I should of done a if exist around the join, and just switch tables.

        Dim query As String = \_
        "DECLARE @startDate AS DATE; " & \_
        "DECLARE @stopDate AS DATE; " & \_
        "SET @startDate = DATETIMEFROMPARTS(@Year, 1, 1, 0, 0, 0, 0); " & \_
        "SET @stopDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 23, 59, 59, 999); "
    
        query +=
        "SELECT " & \_
        " coH.PartNumber, " & \_
        " coH.ManPartNumber, " & \_
        " SUM(coH.Qty) as TotalQty, " & \_
        " CAST(SUM(coH.Qty \* coh.Cost) AS Decimal(10,2)) as TotalCost, " & \_
        " CAST(SUM(coH.Qty \* coh.Price) AS Decimal(10,2)) as TotalPrice, " & \_
        " pI.Cost, " & \_
        " pI.Price, " & \_
        " pI.ShortDescription, " & \_
        " pI.LongDescription, " & \_
        " pI.PostageImage, " & \_
        " pI.Thumbnail, " & \_
        " pI.VendorID, " & \_
        " pI.VendorName, " & \_
        " pI.Weight, " & \_
        " pI.ShipHeight, " & \_
        " pI.ShipWidth, " & \_
        " pI.ShipDepth, " & \_
        " pI.LimitedItem, " & \_
        " vI.Address1, " & \_
        " vI.Address2, " & \_
        " vI.City, " & \_
        " vI.StateCode, " & \_
        " vI.CountryCode, " & \_
        " vI.ZipCode, " & \_
        " vI.ContactName, " & \_
        " vI.VendorPhone " & \_
        " FROM CompletedOrdersCartHistory as coH " & \_
        " LEFT JOIN PRODUCTINFO AS pI " & \_
        " ON coH.PartNumber = pI.PartNumber " & \_
        " LEFT JOIN VendorInfo AS vI " & \_
        " ON pI.VendorID = vI.VendorID " & \_
        " WHERE coh.OrderDate > @startDate " & \_
        " AND coh.OrderDate < @stopDate " & \_
        " AND coh.PartNumber = @PartNumber " & \_
        " GROUP BY " & \_
        " coH.PartNumber, " & \_
        " coH.ManPartNumber,  " & \_
        " pI.Cost,  " & \_
        " pI.Price, " & \_
        " p
    
    Richard DeemingR 1 Reply Last reply
    0
    • J jkirkerx

      I got myself in a pickle here. I started letting customers delete items in the store. But I copied the item into a separate table. I should of added a column status instead. But anyways, on my report for total items sold in the year, I did a join on the product table, to get the current cost, images, etc. So now that some items have been deleted, the record is no longer in the product table, but in the recycle table. I tried a union all, but it results in 2 records, in which the 2nd is null. I tried just union as well, but it produces an reader error. I'm not sure how to proceed on this, or what to words to use to do a search here. I'm stumped! [EDIT] Maybe I should of done a if exist around the join, and just switch tables.

          Dim query As String = \_
          "DECLARE @startDate AS DATE; " & \_
          "DECLARE @stopDate AS DATE; " & \_
          "SET @startDate = DATETIMEFROMPARTS(@Year, 1, 1, 0, 0, 0, 0); " & \_
          "SET @stopDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 23, 59, 59, 999); "
      
          query +=
          "SELECT " & \_
          " coH.PartNumber, " & \_
          " coH.ManPartNumber, " & \_
          " SUM(coH.Qty) as TotalQty, " & \_
          " CAST(SUM(coH.Qty \* coh.Cost) AS Decimal(10,2)) as TotalCost, " & \_
          " CAST(SUM(coH.Qty \* coh.Price) AS Decimal(10,2)) as TotalPrice, " & \_
          " pI.Cost, " & \_
          " pI.Price, " & \_
          " pI.ShortDescription, " & \_
          " pI.LongDescription, " & \_
          " pI.PostageImage, " & \_
          " pI.Thumbnail, " & \_
          " pI.VendorID, " & \_
          " pI.VendorName, " & \_
          " pI.Weight, " & \_
          " pI.ShipHeight, " & \_
          " pI.ShipWidth, " & \_
          " pI.ShipDepth, " & \_
          " pI.LimitedItem, " & \_
          " vI.Address1, " & \_
          " vI.Address2, " & \_
          " vI.City, " & \_
          " vI.StateCode, " & \_
          " vI.CountryCode, " & \_
          " vI.ZipCode, " & \_
          " vI.ContactName, " & \_
          " vI.VendorPhone " & \_
          " FROM CompletedOrdersCartHistory as coH " & \_
          " LEFT JOIN PRODUCTINFO AS pI " & \_
          " ON coH.PartNumber = pI.PartNumber " & \_
          " LEFT JOIN VendorInfo AS vI " & \_
          " ON pI.VendorID = vI.VendorID " & \_
          " WHERE coh.OrderDate > @startDate " & \_
          " AND coh.OrderDate < @stopDate " & \_
          " AND coh.PartNumber = @PartNumber " & \_
          " GROUP BY " & \_
          " coH.PartNumber, " & \_
          " coH.ManPartNumber,  " & \_
          " pI.Cost,  " & \_
          " pI.Price, " & \_
          " p
      
      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      Since you're using a relatively recent version of MS SQL Server, something like this should work:

      DECLARE @startDate AS DATE;
      DECLARE @stopDate AS DATE;

      SET @startDate = DATETIMEFROMPARTS(@Year, 1, 1, 0, 0, 0, 0);
      SET @stopDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 23, 59, 59, 999);

      WITH cteProductInfo As
      (
      SELECT
      PartNumber,
      Cost,
      Price,
      ShortDescription,
      LongDescription,
      PostageImage,
      Thumbnail,
      VendorID,
      VendorName,
      Weight,
      ShipHeight,
      ShipWidth,
      ShipDepth,
      LimitedItem
      FROM
      PRODUCTINFO

      UNION ALL
      
      SELECT
          PartNumber,
          Cost,
          Price,
          ShortDescription,
          LongDescription,
          PostageImage,
          Thumbnail,
          VendorID,
          VendorName,
          Weight,
          ShipHeight,
          ShipWidth,
          ShipDepth,
          LimitedItem
      FROM
          PRODUCTINFO\_RECYCLEBIN
      

      ),
      cteOrders As
      (
      SELECT
      PartNumber,
      ManPartNumber,
      SUM(coH.Qty) as TotalQty,
      CAST(SUM(coH.Qty * coh.Cost) AS decimal(10, 2)) as TotalCost,
      CAST(SUM(coH.Qty * coh.Price) AS decimal(10, 2)) as TotalPrice
      FROM
      CompletedOrdersCartHistory
      GROUP BY
      PartNumber,
      ManPartNumber
      )
      SELECT
      coH.PartNumber,
      coH.ManPartNumber,
      coH.TotalQty,
      coH.TotalCost,
      coH.TotalPrice,
      pI.Cost,
      pI.Price,
      pI.ShortDescription,
      pI.LongDescription,
      pI.PostageImage,
      pI.Thumbnail,
      pI.VendorID,
      pI.VendorName,
      pI.Weight,
      pI.ShipHeight,
      pI.ShipWidth,
      pI.ShipDepth,
      pI.LimitedItem,
      vI.Address1,
      vI.Address2,
      vI.City,
      vI.StateCode,
      vI.CountryCode,
      vI.ZipCode,
      vI.ContactName,
      vI.VendorPhone
      FROM
      cteOrders as coH
      LEFT JOIN cteProductInfo AS pI
      ON coH.PartNumber = pI.PartNumber
      LEFT JOIN VendorInfo AS vI
      ON pI.VendorID = vI.VendorID
      WHERE
      coh.OrderDate > @startDate
      AND
      coh.OrderDate < @stopDate
      AND
      coh.PartNumber = @PartNumber
      ORDER BY
      coH.PartNumber
      ;


      "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      J 2 Replies Last reply
      0
      • Richard DeemingR Richard Deeming

        Since you're using a relatively recent version of MS SQL Server, something like this should work:

        DECLARE @startDate AS DATE;
        DECLARE @stopDate AS DATE;

        SET @startDate = DATETIMEFROMPARTS(@Year, 1, 1, 0, 0, 0, 0);
        SET @stopDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 23, 59, 59, 999);

        WITH cteProductInfo As
        (
        SELECT
        PartNumber,
        Cost,
        Price,
        ShortDescription,
        LongDescription,
        PostageImage,
        Thumbnail,
        VendorID,
        VendorName,
        Weight,
        ShipHeight,
        ShipWidth,
        ShipDepth,
        LimitedItem
        FROM
        PRODUCTINFO

        UNION ALL
        
        SELECT
            PartNumber,
            Cost,
            Price,
            ShortDescription,
            LongDescription,
            PostageImage,
            Thumbnail,
            VendorID,
            VendorName,
            Weight,
            ShipHeight,
            ShipWidth,
            ShipDepth,
            LimitedItem
        FROM
            PRODUCTINFO\_RECYCLEBIN
        

        ),
        cteOrders As
        (
        SELECT
        PartNumber,
        ManPartNumber,
        SUM(coH.Qty) as TotalQty,
        CAST(SUM(coH.Qty * coh.Cost) AS decimal(10, 2)) as TotalCost,
        CAST(SUM(coH.Qty * coh.Price) AS decimal(10, 2)) as TotalPrice
        FROM
        CompletedOrdersCartHistory
        GROUP BY
        PartNumber,
        ManPartNumber
        )
        SELECT
        coH.PartNumber,
        coH.ManPartNumber,
        coH.TotalQty,
        coH.TotalCost,
        coH.TotalPrice,
        pI.Cost,
        pI.Price,
        pI.ShortDescription,
        pI.LongDescription,
        pI.PostageImage,
        pI.Thumbnail,
        pI.VendorID,
        pI.VendorName,
        pI.Weight,
        pI.ShipHeight,
        pI.ShipWidth,
        pI.ShipDepth,
        pI.LimitedItem,
        vI.Address1,
        vI.Address2,
        vI.City,
        vI.StateCode,
        vI.CountryCode,
        vI.ZipCode,
        vI.ContactName,
        vI.VendorPhone
        FROM
        cteOrders as coH
        LEFT JOIN cteProductInfo AS pI
        ON coH.PartNumber = pI.PartNumber
        LEFT JOIN VendorInfo AS vI
        ON pI.VendorID = vI.VendorID
        WHERE
        coh.OrderDate > @startDate
        AND
        coh.OrderDate < @stopDate
        AND
        coh.PartNumber = @PartNumber
        ORDER BY
        coH.PartNumber
        ;


        "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

        That looks interesting! Ohhhhh, so that's how you union all the productinfo and productinfo_recyclebin together. Yes I do understand the SQL now. Wow, thanks for taking the time to look at it. I know it was a lot of code to look at and could get confusing. I'll give it a spin tomorrow. Thanks Richard!

        1 Reply Last reply
        0
        • Richard DeemingR Richard Deeming

          Since you're using a relatively recent version of MS SQL Server, something like this should work:

          DECLARE @startDate AS DATE;
          DECLARE @stopDate AS DATE;

          SET @startDate = DATETIMEFROMPARTS(@Year, 1, 1, 0, 0, 0, 0);
          SET @stopDate = DATETIMEFROMPARTS(@Year, @Month, @Day, 23, 59, 59, 999);

          WITH cteProductInfo As
          (
          SELECT
          PartNumber,
          Cost,
          Price,
          ShortDescription,
          LongDescription,
          PostageImage,
          Thumbnail,
          VendorID,
          VendorName,
          Weight,
          ShipHeight,
          ShipWidth,
          ShipDepth,
          LimitedItem
          FROM
          PRODUCTINFO

          UNION ALL
          
          SELECT
              PartNumber,
              Cost,
              Price,
              ShortDescription,
              LongDescription,
              PostageImage,
              Thumbnail,
              VendorID,
              VendorName,
              Weight,
              ShipHeight,
              ShipWidth,
              ShipDepth,
              LimitedItem
          FROM
              PRODUCTINFO\_RECYCLEBIN
          

          ),
          cteOrders As
          (
          SELECT
          PartNumber,
          ManPartNumber,
          SUM(coH.Qty) as TotalQty,
          CAST(SUM(coH.Qty * coh.Cost) AS decimal(10, 2)) as TotalCost,
          CAST(SUM(coH.Qty * coh.Price) AS decimal(10, 2)) as TotalPrice
          FROM
          CompletedOrdersCartHistory
          GROUP BY
          PartNumber,
          ManPartNumber
          )
          SELECT
          coH.PartNumber,
          coH.ManPartNumber,
          coH.TotalQty,
          coH.TotalCost,
          coH.TotalPrice,
          pI.Cost,
          pI.Price,
          pI.ShortDescription,
          pI.LongDescription,
          pI.PostageImage,
          pI.Thumbnail,
          pI.VendorID,
          pI.VendorName,
          pI.Weight,
          pI.ShipHeight,
          pI.ShipWidth,
          pI.ShipDepth,
          pI.LimitedItem,
          vI.Address1,
          vI.Address2,
          vI.City,
          vI.StateCode,
          vI.CountryCode,
          vI.ZipCode,
          vI.ContactName,
          vI.VendorPhone
          FROM
          cteOrders as coH
          LEFT JOIN cteProductInfo AS pI
          ON coH.PartNumber = pI.PartNumber
          LEFT JOIN VendorInfo AS vI
          ON pI.VendorID = vI.VendorID
          WHERE
          coh.OrderDate > @startDate
          AND
          coh.OrderDate < @stopDate
          AND
          coh.PartNumber = @PartNumber
          ORDER BY
          coH.PartNumber
          ;


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

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

          Worked like a charm! Thanks! Had to adjust a couple of things, but that got my mind into better understanding the SQL sample

          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