SQL Query

  • SQL Query

    Posted by drosen@csfay.com on May 8, 2017 at 11:28 am
    • Drew Rosen

      Member

      May 8, 2017 at 11:28 AM

      I have the following table:

      PrikeyĀ Ā Ā Ā Ā Ā Ā Ā  orderprikeyĀ Ā Ā  statusdate
      1Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  5Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  01/02/17
      2Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  5Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  02/05/17
      3Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  100Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  03/01/17
      4Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  100Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  04/05/17
      5Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  6Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  01/02/17

      I would like toĀ select the Ā with 1 record per orderprikey with the most current date ie

      2Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 5Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  02/05/17
      4Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  100Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā 04/05/17
      5Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  6Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā  01/02/17

      I tried the following

      Select distinct prikey,orderprikey,MAX(statusdate) as statusdt
      from Table
      goup by prikey,orderprikey,statusdate

      thanks for your help

      ——————————
      Drew Rosen
      Carolina Services of Fayetteville, Inc
      Fayetteville NC
      ——————————

    • Matthew Arp

      Member

      May 8, 2017 at 12:22 PM

      Assuming that prikey is an Identity column (strictly increases) then the below will work.

      SELECT t1.* FROM Table t1 INNER JOIN (SELECT MAX(prikey) prikey GROUP BY orderprikey) t2 ON t2.prikey=t1.prikey

      ——————————
      Matthew Arp
      Business Systems Developer
      Hunton Group
      Houston TX
      ——————————
      ——————————————-

    • Valerie Sikora

      Member

      May 9, 2017 at 8:25 AM

      I think your script will work if you remove the statusdate from the group by statement.

      Thanks
      Val

      ——————————
      Valerie Sikora
      InterDyn BMI
      Roseville MN
      ——————————
      ——————————————-

    • Jeff Pfershy

      Member

      May 9, 2017 at 11:29 AM

      You can also use a CTE (Common Table Expression) to apply a row number to the orderprikey column and sort based on the statusdate in descending order. This creates a phantom row number for each statusdate record within the same orderprikey, and every time the orderprikey changes, the numbering starts again at 1.

      WITH My_CTE AS
      (
      Ā SELECT
      Ā Ā Ā  orderprikey
      Ā Ā Ā Ā Ā  ,statusdate
      Ā Ā Ā Ā Ā  ,ROW_NUMBER() OVER(PARTITION BY orderprikey ORDER BY statusdate DESC) AS ‘RowNumber’Ā Ā 
      Ā Ā Ā  FROM table1
      )
      SELECT
      Ā Ā  orderprikey
      Ā  ,statusdate
      FROM My_CTE
      WHERE RowNumber = 1
      ORDER BY 1

      If I wanted to apply the same logic to AR invoices in GP and say, just get the most recent invoice for each GP customer, I could do something like this:

      WITH Sales_CTE AS (
      Ā SELECT
      Ā Ā Ā  CUSTNMBR as ‘CustomerID’
      Ā Ā Ā Ā Ā  ,DOCNUMBR as ‘InvoiceNumber’
      Ā Ā Ā Ā Ā  ,DOCDATE as ‘InvoiceDate’
      Ā Ā Ā Ā Ā  ,ORTRXAMT AS ‘InvoiceAmt’
      Ā Ā Ā Ā Ā  ,ROW_NUMBER() OVER(PARTITION BY CUSTNMBR ORDER BY DOCDATE DESC,ORTRXAMT DESC) AS ‘RowNumber’Ā Ā 
      —Ā Ā Ā Ā Ā  ,ROW_NUMBER() OVER(PARTITION BY CUSTNMBR, DOCDATE ORDER BY DOCDATE DESC,ORTRXAMT DESC) AS ‘RowNumber’Ā Ā 
      Ā Ā Ā  FROM RM20101
      Ā Ā Ā Ā Ā  WHERE RMDTYPAL = 1
      Ā Ā Ā Ā Ā Ā Ā  AND VOIDSTTS = 0
      Ā Ā Ā Ā Ā Ā Ā  AND ORTRXAMT > 0)
      SELECT
      Ā Ā Ā  CustomerID
      Ā Ā  ,InvoiceNumber
      Ā Ā  ,InvoiceDate
      Ā Ā  ,InvoiceAmt
      FROM Sales_CTE
      WHERE RowNumber = 1
      ORDER BY 1

      Hope that helps

      ——————————
      Jeff Pfershy
      Solution Consultant
      248-489-0707
      The TM Group, Inc.
      Farmington Hills MI
      ——————————
      ——————————————-

    drosen@csfay.com replied 9 years, 1 month ago 1 Member · 0 Replies
  • 0 Replies

Sorry, there were no replies found.

The discussion ‘SQL Query’ is closed to new replies.

Start of Discussion
0 of 0 replies June 2018
Now

Welcome to our new site!

Here you will find a wealth of information created for peopleĀ  that are on a mission to redefine business models with cloud techinologies, AI, automation, low code / no code applications, data, security & more to compete in the Acceleration Economy!