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. Diffucult T-SQL question: showing sales data that was not sold

Diffucult T-SQL question: showing sales data that was not sold

Scheduled Pinned Locked Moved Database
tutorialquestiondatabasesales
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.
  • I Offline
    I Offline
    Izak
    wrote on last edited by
    #1

    Hi I want to create a report that lists the customers and all the items that they have not bought from us for a certain period. I have 3 relevant tables, CustomerMaster, InventoryMaster, SalesDetail. SalesDetail holds all the sale info, for example CustomerCode : 1AFR204 StockCode : FRA202 QtyInvoiced : 2 NetSalesValue: 1295 Year : 2006 Month : 7 The master tables holds all customers and inventory. I want the report to basicaly look like this: Ultra liquors Newton Park Not listed: Boland Cabernet Spier Sauvignon Blanc To be able to do this I need a recordset that consists of the stockcode and customercode of items not sold. Any ideas how to do this? I have been playing with not in subqueries. Its easy to display items not sold or customers that did not buy, seperately, but to combine them is a different animal. Thanks Izak

    E 1 Reply Last reply
    0
    • I Izak

      Hi I want to create a report that lists the customers and all the items that they have not bought from us for a certain period. I have 3 relevant tables, CustomerMaster, InventoryMaster, SalesDetail. SalesDetail holds all the sale info, for example CustomerCode : 1AFR204 StockCode : FRA202 QtyInvoiced : 2 NetSalesValue: 1295 Year : 2006 Month : 7 The master tables holds all customers and inventory. I want the report to basicaly look like this: Ultra liquors Newton Park Not listed: Boland Cabernet Spier Sauvignon Blanc To be able to do this I need a recordset that consists of the stockcode and customercode of items not sold. Any ideas how to do this? I have been playing with not in subqueries. Its easy to display items not sold or customers that did not buy, seperately, but to combine them is a different animal. Thanks Izak

      E Offline
      E Offline
      Eric Dahlvang
      wrote on last edited by
      #2

      SELECT CM.CustomerCode, IM.StockCode
      FROM CustomerMaster CM, InventoryMaster IM
      WHERE CM.CustomerCode NOT IN(SELECT SD.CustomerCode
      FROM SalesDetail SD
      WHERE SD.StockCode = IM.StockCode
      AND SD.Year = 2006
      AND (SD.Month >= 6 and SD.Month <= 7))
      ORDER BY CustomerCode

      --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

      I 1 Reply Last reply
      0
      • E Eric Dahlvang

        SELECT CM.CustomerCode, IM.StockCode
        FROM CustomerMaster CM, InventoryMaster IM
        WHERE CM.CustomerCode NOT IN(SELECT SD.CustomerCode
        FROM SalesDetail SD
        WHERE SD.StockCode = IM.StockCode
        AND SD.Year = 2006
        AND (SD.Month >= 6 and SD.Month <= 7))
        ORDER BY CustomerCode

        --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

        I Offline
        I Offline
        Izak
        wrote on last edited by
        #3

        Thanks! If I understand it correctly, the customermaster and the inventorymaster creates all possible combinations except for the combinations in the NOT IN clause? So... If customermaster has 200 records and inventorymaster has 100 records and the salesdetal has 750 distinct records. 200 * 100 = 20000 - 750 = 19250, the result will have 19250 possible sales combinations that did not happen. Am my logic correct? Thanks again Eric, this has been very helpfull! :) Izak

        E 1 Reply Last reply
        0
        • I Izak

          Thanks! If I understand it correctly, the customermaster and the inventorymaster creates all possible combinations except for the combinations in the NOT IN clause? So... If customermaster has 200 records and inventorymaster has 100 records and the salesdetal has 750 distinct records. 200 * 100 = 20000 - 750 = 19250, the result will have 19250 possible sales combinations that did not happen. Am my logic correct? Thanks again Eric, this has been very helpfull! :) Izak

          E Offline
          E Offline
          Eric Dahlvang
          wrote on last edited by
          #4

          Izak - wrote:

          Am my logic correct?

          Seems so to me.

          --EricDV Sig--------- Some problems are so complex that you have to be highly intelligent and well informed just to be undecided about them. - Laurence J. Peters

          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