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. How do I do this in SQL

How do I do this in SQL

Scheduled Pinned Locked Moved Database
databasesalesquestion
6 Posts 4 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.
  • Y Offline
    Y Offline
    Yitzchok Dev
    wrote on last edited by
    #1

    I have these tables Customer Table: CustomerID int; PK FirstName nvarchar; LastNaem nvarchar; etc.; Products Table: ProductID int; PK Name varcahr; etc.; OrderItems Table: OrderItemID int; PK ProductID int; FK CustomerID int; FK etc.; Now I would like to get all users that have ordered: Product1 AND Product2, etc. Only those people that orders all the selected products -- modified at 21:15 Monday 10th September, 2007

    C A 2 Replies Last reply
    0
    • Y Yitzchok Dev

      I have these tables Customer Table: CustomerID int; PK FirstName nvarchar; LastNaem nvarchar; etc.; Products Table: ProductID int; PK Name varcahr; etc.; OrderItems Table: OrderItemID int; PK ProductID int; FK CustomerID int; FK etc.; Now I would like to get all users that have ordered: Product1 AND Product2, etc. Only those people that orders all the selected products -- modified at 21:15 Monday 10th September, 2007

      C Offline
      C Offline
      Chetan Patel
      wrote on last edited by
      #2

      Pass @Products --> with comma separated values like 'product1,product2' and no of items @ProductCount --> 2 Select * from Customers Where CustomerID in (Select CustomerID From (Select CustomerID from OrderItems Inner Join Products on Products .ProductID = OrderItems.productid Where [Name] IN (@products) Group by CustomerID,[Name]) as Temp Group by CustomerID having Count(CustomerID) = @productCount)

      Best Regards, Chetan Patel

      A 1 Reply Last reply
      0
      • C Chetan Patel

        Pass @Products --> with comma separated values like 'product1,product2' and no of items @ProductCount --> 2 Select * from Customers Where CustomerID in (Select CustomerID From (Select CustomerID from OrderItems Inner Join Products on Products .ProductID = OrderItems.productid Where [Name] IN (@products) Group by CustomerID,[Name]) as Temp Group by CustomerID having Count(CustomerID) = @productCount)

        Best Regards, Chetan Patel

        A Offline
        A Offline
        Arjan Einbu
        wrote on last edited by
        #3

        This can't possibly work, can it? I mean, the WHERE [Name] IN (@Products) part specifically.

        J 1 Reply Last reply
        0
        • Y Yitzchok Dev

          I have these tables Customer Table: CustomerID int; PK FirstName nvarchar; LastNaem nvarchar; etc.; Products Table: ProductID int; PK Name varcahr; etc.; OrderItems Table: OrderItemID int; PK ProductID int; FK CustomerID int; FK etc.; Now I would like to get all users that have ordered: Product1 AND Product2, etc. Only those people that orders all the selected products -- modified at 21:15 Monday 10th September, 2007

          A Offline
          A Offline
          Arjan Einbu
          wrote on last edited by
          #4

          You could create the inner query like this:

          SELECT oi.CustomerID
          FROM OrderItems oi
          JOIN Products p
          WHERE p.ProductID IN ('Product1', 'Product2')
          GROUP BY oi.CustomerID
          HAVING COUNT(*) = 2

          You'll have to dynamically create the WHERE p.ProductID IN (...) part, and the value for the HAVING COUNT(*) = 2 should use a parameter. Joined with the customer table, the results could look something like this:

          DECLARE @NumberOfProducts int
          SET @NumberOfProducts = 2

          SELECT
          c.*
          FROM Customer c
          JOIN
          (
          SELECT oi.CustomerID
          FROM OrderItems oi
          JOIN Products p
          WHERE p.ProductID IN ('Product1', 'Product2')
          GROUP BY oi.CustomerID
          HAVING COUNT(*) = @NumberOfProducts
          ) AS t
          ON t.CustomerID = c.CustomerID

          Y 1 Reply Last reply
          0
          • A Arjan Einbu

            This can't possibly work, can it? I mean, the WHERE [Name] IN (@Products) part specifically.

            J Offline
            J Offline
            John ph
            wrote on last edited by
            #5

            If this has to work then the query should get execute using "EXEC sp_executesql" command

            Regards
            J O H N :rose:
            "Even eagles need a push." David McNally


            1 Reply Last reply
            0
            • A Arjan Einbu

              You could create the inner query like this:

              SELECT oi.CustomerID
              FROM OrderItems oi
              JOIN Products p
              WHERE p.ProductID IN ('Product1', 'Product2')
              GROUP BY oi.CustomerID
              HAVING COUNT(*) = 2

              You'll have to dynamically create the WHERE p.ProductID IN (...) part, and the value for the HAVING COUNT(*) = 2 should use a parameter. Joined with the customer table, the results could look something like this:

              DECLARE @NumberOfProducts int
              SET @NumberOfProducts = 2

              SELECT
              c.*
              FROM Customer c
              JOIN
              (
              SELECT oi.CustomerID
              FROM OrderItems oi
              JOIN Products p
              WHERE p.ProductID IN ('Product1', 'Product2')
              GROUP BY oi.CustomerID
              HAVING COUNT(*) = @NumberOfProducts
              ) AS t
              ON t.CustomerID = c.CustomerID

              Y Offline
              Y Offline
              Yitzchok Dev
              wrote on last edited by
              #6

              Thank you

              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