Solved

MAX of two columns in one SQL

Posted on 2014-03-09
4
331 Views
Last Modified: 2014-03-09
Hello,
I have the following Customer table:

Table structure
The desired output is:
CustomerId = 1
OrderDate = 02-FEB-2014
CustomerEmail = angel@yahoo.com

I need to get the latest order date and related email from this table.

I tried to do the following:
Select CustomerId, MAX(OrderDate) OVER (PARTITION BY CustomerID), MAX(CustomerEmail) OVER (PARTITION BY CustomerId)
FROM Customer table;

Open in new window


Though this sql is returning the MAX(orderDate) correctly, the MAX(CustomerEmail) returned is not the one associated to the Max(orderdate) but the one with highest alphabetical order (i.e, in this case Zy2013@yahoo.com).

How do I get both the MAX(OrderDate) and its associated CustomerEmail in the same query using MAX as well as OVER PARTITION by approach?


Thanks!
0
Comment
Question by:sath350163
[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 10

Expert Comment

by:aboo_s
ID: 39915785
Select CustomerId, MAX(OrderDate),CustomerEmail  OVER (PARTITION BY OrderDate), FROM Customer table;
0
 
LVL 7

Accepted Solution

by:
Anoo S Pillai earned 250 total points
ID: 39916033
If my understanding is correct, your logical requirement is to identify latest order by each customer and then get the related value.  So partition by should be based on CustomerID and OrderBy should be based on orderDate
 
ROW_number is the proper function for this requirement, something similar to the following would be useful.

select * 
from 
   (  select * , 
       row_number () over ( partition by customerid order by orderdate desc )  as rownum 
       from customer
  ) DTab
where rownum = 1 

Open in new window

0
 
LVL 75

Expert Comment

by:Anthony Perkins
ID: 39916037
There are a couple of ways of doing that this is one:
SELECT  c.CustomerId, c.OrderDate, c.CustomerEmail
FROM    [Customer table] c
        INNER JOIN (SELECT  CustomerId,
                            MAX(OrderDate) LastOrderDate
                    FROM    [Customer table]
                    GROUP BY CustomerId
                   ) d ON c.CustomerId = d.CustomerId
                          AND c.OrderDate = d.LastOrderDate

Open in new window

0
 
LVL 75

Assisted Solution

by:Anthony Perkins
Anthony Perkins earned 250 total points
ID: 39916049
Here is another approach using a CTE:
;WITH    CTE
          AS (SELECT    CustomerId,
                        OrderDate,
                        CustomerEmail,
                        ROW_NUMBER() OVER (PARTITION BY CustomerId ORDER BY OrderDate DESC) Row
             )
SELECT  CustomerId,
        OrderDate,
        CustomerEmail
FROM    CTE
WHERE   Row = 1

Open in new window

0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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.
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 different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

688 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