Solved

SQL Query - Where Clause Question

Posted on 2015-01-13
4
72 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Server for XML PATH giving wrong results. 6 59
Create a Calendar table 29 43
Backing up Large SQL Server VM Best practice [using Veeam Backup] 8 71
HIghlights of SSIS? 3 42
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

734 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