Solved

SQL Query - Where Clause Question

Posted on 2015-01-13
4
73 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 69

Accepted Solution

by:
Scott Pletcher 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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

Question has a verified solution.

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

Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

707 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