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. query for unique customers

query for unique customers

Scheduled Pinned Locked Moved Database
databasesales
5 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
    imsathy
    wrote on last edited by
    #1

    i need a query to display unique customers on each of thier last purchase date alone segregating other dates in the same columns for the same customer thanx in advance sathy

    C 1 Reply Last reply
    0
    • I imsathy

      i need a query to display unique customers on each of thier last purchase date alone segregating other dates in the same columns for the same customer thanx in advance sathy

      C Offline
      C Offline
      Colin Angus Mackay
      wrote on last edited by
      #2

      If you want help on building a query then you need to tell us what the tables are, what columns are available, what the primary key(s) are on each table and their foreign key counterparts on other tables. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

      I 1 Reply Last reply
      0
      • C Colin Angus Mackay

        If you want help on building a query then you need to tell us what the tables are, what columns are available, what the primary key(s) are on each table and their foreign key counterparts on other tables. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

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

        the table contains customerId(foreign key), dateOfPurchase, totalValue, shopId(primary key) these are the four columns to be dealth i want to display unique customerId, dateOfPurchase(in specific the last purchase date of a customer), totalValue for the given shopId select distinct customerId, max(dateOfPurchase)as date, totalValue from business_orders where shopId = '9F5DF' group by customerId this query works when totalValue is not added in the query sathy

        C 1 Reply Last reply
        0
        • I imsathy

          the table contains customerId(foreign key), dateOfPurchase, totalValue, shopId(primary key) these are the four columns to be dealth i want to display unique customerId, dateOfPurchase(in specific the last purchase date of a customer), totalValue for the given shopId select distinct customerId, max(dateOfPurchase)as date, totalValue from business_orders where shopId = '9F5DF' group by customerId this query works when totalValue is not added in the query sathy

          C Offline
          C Offline
          Colin Angus Mackay
          wrote on last edited by
          #4

          imsathy wrote:

          this query works when totalValue is not added in the query

          That is correct, it must be in the GROUP BY clause or part of an aggregate. If you want the totalValue from the corresponding date you need to create a subquery.

          SELECT customerId, [date], totalValue
          FROM business_orders AS bo
          INNER JOIN (
          SELECT customerId, max(dateOfPurchase) AS [date]
          FROM business_orders
          WHERE shopId = '9F5DF'
          GROUP BY customerId) AS ld ON
          bo.customerId = ld.customerID AND
          bo.[dateOfPurchase] = ld.[date]

          I also removed the DISTINCT keyword as the GROUP BY effectively does that already and so it was redundant. CAVEAT: If you have two or more orders on the same dateOfPurchace then you will get duplicates for an individual customer. There is nothing you can do about that as there is not enough date to determine which was really the last purchase. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

          I 1 Reply Last reply
          0
          • C Colin Angus Mackay

            imsathy wrote:

            this query works when totalValue is not added in the query

            That is correct, it must be in the GROUP BY clause or part of an aggregate. If you want the totalValue from the corresponding date you need to create a subquery.

            SELECT customerId, [date], totalValue
            FROM business_orders AS bo
            INNER JOIN (
            SELECT customerId, max(dateOfPurchase) AS [date]
            FROM business_orders
            WHERE shopId = '9F5DF'
            GROUP BY customerId) AS ld ON
            bo.customerId = ld.customerID AND
            bo.[dateOfPurchase] = ld.[date]

            I also removed the DISTINCT keyword as the GROUP BY effectively does that already and so it was redundant. CAVEAT: If you have two or more orders on the same dateOfPurchace then you will get duplicates for an individual customer. There is nothing you can do about that as there is not enough date to determine which was really the last purchase. ColinMackay.net "Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucius "If a man empties his purse into his head, no man can take it away from him, for an investment in knowledge pays the best interest." -- Joseph E. O'Donnell

            I Offline
            I Offline
            imsathy
            wrote on last edited by
            #5

            :)thanx a lot Colin sathy

            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