[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

SQL Query - Where Clause Question

Posted on 2015-01-13
4
Medium Priority
?
75 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 70

Accepted Solution

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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

649 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