Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2012 Forums
 Transact-SQL (2012)
 Year Problem Query

Author  Topic 

Yonkouturko
Yak Posting Veteran

59 Posts

Posted - 2013-01-05 : 07:40:44
SELECT TOP (5) Table_PurchaseOrder_Information.PO_Number, Table_PurchaseOrder_Information.Supplier, Table_PurchaseOrder_List.Unit_Price,
Table_PurchaseOrder_Information.CreatedDate
FROM Table_PurchaseOrder_Information INNER JOIN
Table_PurchaseOrder_List ON Table_PurchaseOrder_Information.ReferenceNumber = Table_PurchaseOrder_List.ReferenceNumber
ORDER BY Table_PurchaseOrder_Information.CreatedDate DESC

thats my query... top 5 2012 records will show... but i have created a new record for 2013 but it wont show..?
any help on these?

jimf
Master Smack Fu Yak Hacker

2875 Posts

Posted - 2013-01-05 : 16:17:32
There would have to be an entry in each table for the 2013 record to show.

Jim

Everyday I learn something that somebody else already knew
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-06 : 10:30:53
are you looking at top 5 values for each year?
then use

SELECT *
FROM
(
SELECT ROW_NUMBER() OVER (PARTITION BY YEAR(Table_PurchaseOrder_Information.CreatedDate) ORDER BY Table_PurchaseOrder_Information.CreatedDate DESC) AS Seq,
Table_PurchaseOrder_Information.PO_Number, Table_PurchaseOrder_Information.Supplier, Table_PurchaseOrder_List.Unit_Price,
Table_PurchaseOrder_Information.CreatedDate
FROM Table_PurchaseOrder_Information INNER JOIN
Table_PurchaseOrder_List ON Table_PurchaseOrder_Information.ReferenceNumber = Table_PurchaseOrder_List.ReferenceNumber
)t
WHERE Seq<=5
ORDER BY CreatedDate DESC


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

Go to Top of Page

Yonkouturko
Yak Posting Veteran

59 Posts

Posted - 2013-01-06 : 20:48:33
@Visakh16
im looking for top 5 of the whole record regardless of the year...

i hope your getting me clear on this

thanks...
Go to Top of Page

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2013-01-07 : 04:32:43
do you have the corresponding record created in Table_PurchaseOrder_List for 2013 also ? Or only in the Table_PurchaseOrder_Information ?


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2013-01-07 : 10:11:00
then only other issue might be that you dont have matching entry in Table_PurchaseOrder_List table for 2013 records. Try changing INNER to LEFT JOIN

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

Go to Top of Page
   

- Advertisement -