Solved

SQL Query - Where Clause Question

Posted on 2015-01-13
4
65 Views
Last Modified: 2015-01-15
I have a table that I am querying some tables from and wanting to pull the last date.  However, sometimes this date is the same.   Example, a PO that was last received on the same day for same Item.

How could I create my query that if the "Received_Date" is the same, then look at the "Want_Date" instead?

Example
select item_id, Unit_price, Want_date, Received_date
from  PO_Line

Not sure how to tweak the above query that if "Received_Date" is the same, then look at "Want_date" and only pull the most recent.  Any ideas?
0
Comment
Question by:holemania
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
ScottPletcher earned 500 total points
ID: 40547919
SELECT [Item ID], [Unit Price], [Want_Date], [Received_Date], ...
FROM (
    SELECT *, ROW_NUMBER() OVER ( PARTITION BY /*PO_Number,*/ [Item ID] ORDER BY Received_Date DESC, Want_Date DESC ) AS row_num
    FROM PO_Line
) AS derived
WHERE
    row_num = 1
0
 
LVL 5

Expert Comment

by:Leon Kammer
ID: 40547921
Hi,

Could just be that it is late, but could you clarify the question please, I am having rouble understanding what you want the query to accomplish.

if "Received_Date" is the same, then look at "Want_Date".

(If the Received_Date colum is identical to the Want_Date, or if there are multiple records returned for the item ID for that date?)

Cheers

Leon
0
 

Author Comment

by:holemania
ID: 40549544
Leon,

I'll look at what Scott suggested, but I am pulling some data off a table.  Sometimes, the "received date" is the same, and I want to pull the most recent "received date".  Since I can have 2 or more records received on same date, then I am told to use the "want date" as the next filter.  

The example above, I have 2 PO that was received on the same date.  Now I need to then look at the "want date".
0
 

Author Closing Comment

by:holemania
ID: 40551310
Thank you.  That worked.
0

Featured Post

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

757 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now