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. Distinct records on old school foxpro database file

Distinct records on old school foxpro database file

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

    I've been messing with this for hours today. I'm trying to do this in 1 call to the database. Normally I would just get a list of unique items first, and then go back and get the numbers. Perhaps I'm trying to do something that can't be done, or it's just beyond my knowledge level, I'll take the latter of thought. I need to get the qty sum and records of a sales history file Item | qty | custNum | date 09-170 | 5 | 0014 | 01/20/1990 09-170 | 24 | 0014 | 02/21/1991 17-209 | 24 | 0014 | 03/15/1997 17-209 | 24 | 0014 | 08/15/2007 Results should be Item | qty | desc | avg price 09-170 | 29 | trowel | 3.00 17-209 | 48 | kneepad | 24.00 So I wrote this to get the count of unique items which is 2. I'm not sure if my code returns the right number or not, because the database file contains thousands of records. I get 14, not reflective of the example above.

    SELECT COUNT(FITEMNO) AS cCount
    FROM
    (
    SELECT
    FITEMNO
    FROM ARTRS01.dbf
    WHERE FCUSTNO=@FCUSTNO
    GROUP BY FITEMNO
    )
    UNION ALL
    SELECT COUNT(FITEMNO) AS hCount
    FROM
    (
    SELECT
    FITEMNO
    FROM ARTRS01H.dbf
    WHERE FCUSTNO=@FCUSTNO
    GROUP BY FITEMNO
    )

    I wrote this to get the records, in which I get 36. I'm just testing on the history file, so there is no union to join the 2 database files yet. I keep getting 36 records with duplicates instead of 14 unique records.

    SELECT FITEMNO, FSHIPQTY AS hItems"
    FROM
    (
    SELECT h.FITEMNO, h.FSHIPQTY FROM ARTRS01H.dbf h WHERE h.FCUSTNO=@FCUSTNO GROUP BY FITEMNO, FSHIPQTY
    )

    G J N J 4 Replies Last reply
    0
    • J jkirkerx

      I've been messing with this for hours today. I'm trying to do this in 1 call to the database. Normally I would just get a list of unique items first, and then go back and get the numbers. Perhaps I'm trying to do something that can't be done, or it's just beyond my knowledge level, I'll take the latter of thought. I need to get the qty sum and records of a sales history file Item | qty | custNum | date 09-170 | 5 | 0014 | 01/20/1990 09-170 | 24 | 0014 | 02/21/1991 17-209 | 24 | 0014 | 03/15/1997 17-209 | 24 | 0014 | 08/15/2007 Results should be Item | qty | desc | avg price 09-170 | 29 | trowel | 3.00 17-209 | 48 | kneepad | 24.00 So I wrote this to get the count of unique items which is 2. I'm not sure if my code returns the right number or not, because the database file contains thousands of records. I get 14, not reflective of the example above.

      SELECT COUNT(FITEMNO) AS cCount
      FROM
      (
      SELECT
      FITEMNO
      FROM ARTRS01.dbf
      WHERE FCUSTNO=@FCUSTNO
      GROUP BY FITEMNO
      )
      UNION ALL
      SELECT COUNT(FITEMNO) AS hCount
      FROM
      (
      SELECT
      FITEMNO
      FROM ARTRS01H.dbf
      WHERE FCUSTNO=@FCUSTNO
      GROUP BY FITEMNO
      )

      I wrote this to get the records, in which I get 36. I'm just testing on the history file, so there is no union to join the 2 database files yet. I keep getting 36 records with duplicates instead of 14 unique records.

      SELECT FITEMNO, FSHIPQTY AS hItems"
      FROM
      (
      SELECT h.FITEMNO, h.FSHIPQTY FROM ARTRS01H.dbf h WHERE h.FCUSTNO=@FCUSTNO GROUP BY FITEMNO, FSHIPQTY
      )

      G Offline
      G Offline
      GuyThiebaut
      wrote on last edited by
      #2

      If you want the distinct count of FITEMO use:

      SELECT COUNT(distinct FITEMNO)
      FROM ARTRS01.dbf
      WHERE FCUSTNO=@FCUSTNO

      “That which can be asserted without evidence, can be dismissed without evidence.”

      ― Christopher Hitchens

      J 1 Reply Last reply
      0
      • J jkirkerx

        I've been messing with this for hours today. I'm trying to do this in 1 call to the database. Normally I would just get a list of unique items first, and then go back and get the numbers. Perhaps I'm trying to do something that can't be done, or it's just beyond my knowledge level, I'll take the latter of thought. I need to get the qty sum and records of a sales history file Item | qty | custNum | date 09-170 | 5 | 0014 | 01/20/1990 09-170 | 24 | 0014 | 02/21/1991 17-209 | 24 | 0014 | 03/15/1997 17-209 | 24 | 0014 | 08/15/2007 Results should be Item | qty | desc | avg price 09-170 | 29 | trowel | 3.00 17-209 | 48 | kneepad | 24.00 So I wrote this to get the count of unique items which is 2. I'm not sure if my code returns the right number or not, because the database file contains thousands of records. I get 14, not reflective of the example above.

        SELECT COUNT(FITEMNO) AS cCount
        FROM
        (
        SELECT
        FITEMNO
        FROM ARTRS01.dbf
        WHERE FCUSTNO=@FCUSTNO
        GROUP BY FITEMNO
        )
        UNION ALL
        SELECT COUNT(FITEMNO) AS hCount
        FROM
        (
        SELECT
        FITEMNO
        FROM ARTRS01H.dbf
        WHERE FCUSTNO=@FCUSTNO
        GROUP BY FITEMNO
        )

        I wrote this to get the records, in which I get 36. I'm just testing on the history file, so there is no union to join the 2 database files yet. I keep getting 36 records with duplicates instead of 14 unique records.

        SELECT FITEMNO, FSHIPQTY AS hItems"
        FROM
        (
        SELECT h.FITEMNO, h.FSHIPQTY FROM ARTRS01H.dbf h WHERE h.FCUSTNO=@FCUSTNO GROUP BY FITEMNO, FSHIPQTY
        )

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

        I must be missing something here. Why can't you just:

        SELECT item,sum(qty) as qty
        FROM MyTable
        GROUP BY item

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

        J 1 Reply Last reply
        0
        • G GuyThiebaut

          If you want the distinct count of FITEMO use:

          SELECT COUNT(distinct FITEMNO)
          FROM ARTRS01.dbf
          WHERE FCUSTNO=@FCUSTNO

          “That which can be asserted without evidence, can be dismissed without evidence.”

          ― Christopher Hitchens

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

          I tried that at first but got an error (missing operator in query expression 'COUNT(distinct FITEMNO) And did research and ended up with the example in my post. So I thought it was too advanced for the old foxpro or it was a OLEDB thing

          1 Reply Last reply
          0
          • J Jorgen Andersson

            I must be missing something here. Why can't you just:

            SELECT item,sum(qty) as qty
            FROM MyTable
            GROUP BY item

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

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

            I ended up doing something similar. I wrote one function to get the distinct items, and went back and got the sums with the distinct item list. I don't know what I was thinking, was trying to do it all in one shot.

            " SELECT " & _
            " SUM(FSHIPQTY) " & _
            ", SUM(FAMOUNT) " & _
            " FROM ARTRS01H.dbf " & _
            " WHERE FCUSTNO=@FCUSTNO " & _
            " AND FITEMNO=@FITEMNO "

            1 Reply Last reply
            0
            • J jkirkerx

              I've been messing with this for hours today. I'm trying to do this in 1 call to the database. Normally I would just get a list of unique items first, and then go back and get the numbers. Perhaps I'm trying to do something that can't be done, or it's just beyond my knowledge level, I'll take the latter of thought. I need to get the qty sum and records of a sales history file Item | qty | custNum | date 09-170 | 5 | 0014 | 01/20/1990 09-170 | 24 | 0014 | 02/21/1991 17-209 | 24 | 0014 | 03/15/1997 17-209 | 24 | 0014 | 08/15/2007 Results should be Item | qty | desc | avg price 09-170 | 29 | trowel | 3.00 17-209 | 48 | kneepad | 24.00 So I wrote this to get the count of unique items which is 2. I'm not sure if my code returns the right number or not, because the database file contains thousands of records. I get 14, not reflective of the example above.

              SELECT COUNT(FITEMNO) AS cCount
              FROM
              (
              SELECT
              FITEMNO
              FROM ARTRS01.dbf
              WHERE FCUSTNO=@FCUSTNO
              GROUP BY FITEMNO
              )
              UNION ALL
              SELECT COUNT(FITEMNO) AS hCount
              FROM
              (
              SELECT
              FITEMNO
              FROM ARTRS01H.dbf
              WHERE FCUSTNO=@FCUSTNO
              GROUP BY FITEMNO
              )

              I wrote this to get the records, in which I get 36. I'm just testing on the history file, so there is no union to join the 2 database files yet. I keep getting 36 records with duplicates instead of 14 unique records.

              SELECT FITEMNO, FSHIPQTY AS hItems"
              FROM
              (
              SELECT h.FITEMNO, h.FSHIPQTY FROM ARTRS01H.dbf h WHERE h.FCUSTNO=@FCUSTNO GROUP BY FITEMNO, FSHIPQTY
              )

              N Offline
              N Offline
              NitinDhapte
              wrote on last edited by
              #6

              SELECT h.FITEMNO, SUM(h.FSHIPQTY) AS TOTAL_QTY FROM ARTRS01H.dbf h WHERE h.FCUSTNO=@FCUSTNO GROUP BY FITEMNO

              1 Reply Last reply
              0
              • J jkirkerx

                I've been messing with this for hours today. I'm trying to do this in 1 call to the database. Normally I would just get a list of unique items first, and then go back and get the numbers. Perhaps I'm trying to do something that can't be done, or it's just beyond my knowledge level, I'll take the latter of thought. I need to get the qty sum and records of a sales history file Item | qty | custNum | date 09-170 | 5 | 0014 | 01/20/1990 09-170 | 24 | 0014 | 02/21/1991 17-209 | 24 | 0014 | 03/15/1997 17-209 | 24 | 0014 | 08/15/2007 Results should be Item | qty | desc | avg price 09-170 | 29 | trowel | 3.00 17-209 | 48 | kneepad | 24.00 So I wrote this to get the count of unique items which is 2. I'm not sure if my code returns the right number or not, because the database file contains thousands of records. I get 14, not reflective of the example above.

                SELECT COUNT(FITEMNO) AS cCount
                FROM
                (
                SELECT
                FITEMNO
                FROM ARTRS01.dbf
                WHERE FCUSTNO=@FCUSTNO
                GROUP BY FITEMNO
                )
                UNION ALL
                SELECT COUNT(FITEMNO) AS hCount
                FROM
                (
                SELECT
                FITEMNO
                FROM ARTRS01H.dbf
                WHERE FCUSTNO=@FCUSTNO
                GROUP BY FITEMNO
                )

                I wrote this to get the records, in which I get 36. I'm just testing on the history file, so there is no union to join the 2 database files yet. I keep getting 36 records with duplicates instead of 14 unique records.

                SELECT FITEMNO, FSHIPQTY AS hItems"
                FROM
                (
                SELECT h.FITEMNO, h.FSHIPQTY FROM ARTRS01H.dbf h WHERE h.FCUSTNO=@FCUSTNO GROUP BY FITEMNO, FSHIPQTY
                )

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

                I finally got it in 1 shot. Runs super fast now. Customer complained about the 5 minute run time, so I took another stab at it. Don't know why it I got it this time, perhaps the nap time and the beers!

                SELECT
                DISTINCT v.FITEMNO
                , SUM(v.FSHIPQTY)
                , SUM(v.FSHIPQTY * v.FPRICE)
                , (SELECT FDESCRIPT FROM ICITM01.dbf WHERE FITEMNO=v.FITEMNO) AS FREALDESC
                FROM ARTRS01H.dbf v
                WHERE FCUSTNO=@FCUSTNO
                GROUP BY v.FITEMNO

                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