SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Finding latest records
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

zorban
Starting Member

USA
2 Posts

Posted - 08/11/2012 :  07:45:31  Show Profile  Reply with Quote
Hello,

I am trying to find the latest price and qty of an item sorted by the customer.

My table is SAlESHIS
The fields are: ORDATE,CUSTOMER,ITEM,ITEMDESC1,ITEMDESC2,QTY,PRICE

I tried:

SELECT DISTINCT ITEM, CUSTOMER, ITEMDESC1,ITEMDESC2, PRICE, QTY, MAX(DISTINCT ORDATE)
FROM SALESHIS AS T
WHERE CUSTOMER =
(SELECT MAX(CUSTOMER)
FROM SALESHIS WHERE ITEM = T.ITEM)
GROUP BY ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY
ORDER BY ITEM

by I am still getting duplicate items for the same customer...

Could someone point me to the right direction?

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/11/2012 :  08:20:57  Show Profile  Reply with Quote
Based on your description, what you need is to have ORDDATE in the WHERE clause rather than CUSTOMER, like this:
SELECT DISTINCT ITEM,
       CUSTOMER,
       ITEMDESC1,
       ITEMDESC2,
       PRICE,
       QTY,
       ORDATE
FROM   SALESHIS AS T
WHERE  ORDDATE = 
		(
           SELECT MAX(T2.ORDDATE)
           FROM   SALESHIS T2
           WHERE  T2.ITEM = T.ITEM AND T2.CUSTOMER = T.CUSTOMER
       )
ORDER BY
       ITEM
Alternatively, you could use the row_number function like shown below if you are on SQL 2005 or later. In the query above, if you had two orders for the same customer for the same item on the latest date, both would show up. The one below avoids that (by picking the one with the highest price)
SELECT
	ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
FROM
(
	SELECT 
		ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
		,ROW_NUMBER() OVER 
			(PARTITION BY CUSTOMER, ITEM ORDER BY ORDDATE DESC, PRICE DESC) AS RN
	FROM
		SALESHIS
) s
WHERE
	RN = 1
ORDER BY
       ITEM
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47154 Posts

Posted - 08/11/2012 :  10:42:29  Show Profile  Reply with Quote

SELECT 
		ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
FROM	SALESHIS s
OUTER APPLY (SELECT COUNT(1) AS Cnt
             FROM SALESHIS
             WHERE CUSTOMER = s.CUSTOMER
             AND ITEM = s.ITEM
             AND ORDDATE > s.ORDDATE
             OR (ORDDATE = s.ORDDATE AND PRICE > s.PRICE)
             )s1
WHERE COALESCE(Cnt,0) =0


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

zorban
Starting Member

USA
2 Posts

Posted - 08/12/2012 :  07:13:37  Show Profile  Reply with Quote
Thanks for the help!

@ sunitabeck

In the second statement, where can I can I put the values of ITEM that I need to exclude:


I tried this, and received an error:

SELECT
ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
FROM
(
SELECT
ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
,ROW_NUMBER() OVER
(PARTITION BY CUSTOMER, ITEM ORDER BY ORDDATE DESC, PRICE DESC) AS RN
FROM
SALESHIS
) s
WHERE
RN = 1 AND QTY > '0' AND ITEM <> 'ITEM1' AND ITEM <> 'ITEM2'
ORDER BY
ITEM


Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
47154 Posts

Posted - 08/12/2012 :  12:05:46  Show Profile  Reply with Quote
if you want to use my suggestion just add it to current WHERE clause using AND

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page

sunitabeck
Flowing Fount of Yak Knowledge

5152 Posts

Posted - 08/12/2012 :  21:32:53  Show Profile  Reply with Quote
quote:
Originally posted by zorban

Thanks for the help!

@ sunitabeck

In the second statement, where can I can I put the values of ITEM that I need to exclude:


I tried this, and received an error:

SELECT
ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
FROM
(
SELECT
ITEM, CUSTOMER, ITEMDESC1, ITEMDESC2, PRICE, QTY, ORDATE
,ROW_NUMBER() OVER
(PARTITION BY CUSTOMER, ITEM ORDER BY ORDDATE DESC, PRICE DESC) AS RN
FROM
SALESHIS
) s
WHERE
RN = 1 AND QTY > '0' AND ITEM <> 'ITEM1' AND ITEM <> 'ITEM2'
ORDER BY
ITEM




What is the error message? Are you on SQL 2000? If you are, the row_number function will not work.

You can put the conditions to exclude items in the first query as an AND condition after the existing where clause.

WHERE  ORDDATE = 
		(
           SELECT MAX(T2.ORDDATE)
           FROM   SALESHIS T2
           WHERE  T2.ITEM = T.ITEM AND T2.CUSTOMER = T.CUSTOMER
       )
       AND YourExclusionConditions here
In the second code, put the exclusion condition as a WHERE clause in the inner query:
FROM
     SALESHIS
WHERE
    YourExclusionConditions here
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000