?
Solved

I need to get quantity from record selected with MAX()

Posted on 2007-08-09
3
Medium Priority
?
197 Views
Last Modified: 2010-03-19
I have a large table with four fields (ProductSK, StoreSK, ReceiptDate, Units).  I need to find the record for each ProductSK and StoreSKwith the mose recent ReceiptDate (if there is a tie, I need to choose the one with the greatest number of units).  The results will be stored in a table with a primary key of ProductSK, StoreSK.

My current query is:
TRUNCATE Table dbo.Last_Receipt
INSERT INTO dbo.Last_Receipt (ProductSK, StoreSK, ReceiptDate, DaysSince, Units)
SELECT
      tf.ProductSK,
      tf.StoreSK,
      tf.ReceiptDate as ReceiptDate,
      DATEDIFF(dd, tf.ReceiptDate, GetDate()) as DaysSince,
      tf.Units as Units

FROM
      (SELECT ProductSK, StoreSK, MAX(ReceiptDate) as RecptDate
       FROM InventoryAllocationCube.dbo.TFRPF90_Sum
       GROUP BY ProductSK, StoreSK) as aa INNER JOIN
       InventoryAllocationCube.dbo.TFRPF90_Sum as tf ON
       aa.ProductSK = tf.ProductSK AND aa.StoreSK = tf.StoreSK

This is not generating unique rows and the insert fails due to duplicate keyed records.  I am quessing that ties are the problem.  How can I make this work?
0
Comment
Question by:tomllewis
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
mherchl earned 1800 total points
ID: 19663953
SELECT
      tf.ProductSK,
      tf.StoreSK,
      tf.ReceiptDate as ReceiptDate,
      DATEDIFF(dd, tf.ReceiptDate, GetDate()) as DaysSince,
      max(tf.Units) as Units

FROM
      (SELECT ProductSK, StoreSK, MAX(ReceiptDate) as RecptDate
       FROM InventoryAllocationCube.dbo.TFRPF90_Sum
       GROUP BY ProductSK, StoreSK) as aa INNER JOIN
       InventoryAllocationCube.dbo.TFRPF90_Sum as tf ON
       aa.ProductSK = tf.ProductSK AND aa.StoreSK = tf.StoreSK AND aa.RecptDate = tf.ReceiptDate
GROUP BY       tf.ProductSK,      tf.StoreSK,      tf.ReceiptDate   DATEDIFF(dd, tf.ReceiptDate, GetDate())
0
 
LVL 42

Expert Comment

by:dqmq
ID: 19664033
Try this:

TRUNCATE Table dbo.Last_Receipt
INSERT INTO dbo.Last_Receipt (ProductSK, StoreSK, ReceiptDate, DaysSince, Units)
SELECT
      tf.ProductSK,
      tf.StoreSK,
      tf.ReceiptDate as ReceiptDate,
      DATEDIFF(dd, tf.ReceiptDate, GetDate()) as DaysSince,
      max(tf.Units) as Units
FROM
 InventoryAllocationCube.dbo.TFRPF90_Sum
     (SELECT ProductSK, StoreSK, MAX(ReceiptDate) as RecptDate
       FROM InventoryAllocationCube.dbo.TFRPF90_Sum
       GROUP BY ProductSK, StoreSK) as aa INNER JOIN
       InventoryAllocationCube.dbo.TFRPF90_Sum as tf ON
       aa.ProductSK = tf.ProductSK AND aa.StoreSK = tf.StoreSK
       and tf.ReceiptDate = aa.RecptDate
group by
      tf.ProductSK,
      tf.StoreSK,
      tf.ReceiptDate


     
0
 
LVL 42

Assisted Solution

by:dqmq
dqmq earned 200 total points
ID: 19664070
Too little, too late... Remove the DATEDIFF and mherchl answer is good to go.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have a large data set and a SSIS package. How can I load this file in multi threading?
MSSQL DB-maintenance also needs implementation of multiple activities. However, unprecedented errors can hamper the database management. In that case, deploying Stellar SQL Database Toolkit ensures fast and accurate database and backup repair as wel…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question