Solved

get the maximum value on a condition

Posted on 2016-10-19
3
52 Views
Last Modified: 2016-10-20
Dear All

I want to write a sql statement giving me the most recent date when a customer paid for different products by product

Result should be like this

Customer ='James", last_date_paid_for_product1='20160101", last_date_paid_for_product2='20160201", last_date_paid_for_product2
3='20160201",

Data is in this format
Customer            Date_of_pay         Product1          Product2           Product3
A                           20161010                200                                                300
A                           20161011                200                                                
A                           20161012                200                                                
A                           20161013                                           400                     300
A                           20161014                                                                      300

Result of the sql should be
Customer ='A", last_date_paid_for_product1='20161012", last_date_paid_for_product2='20161013", last_date_paid_for_product2
3='20161014",


Thank you

Anthony
0
Comment
Question by:Anthony Matovu
3 Comments
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 41851471
I would use this simple query:
SELECT
   Customer            
  ,MAX(CASE WHEN product1 IS NOT NULL THEN Date_of_pay END) as last_date_paid_for_product1
  ,MAX(CASE WHEN product2 IS NOT NULL THEN Date_of_pay END) as last_date_paid_for_product2
  ,MAX(CASE WHEN product3 IS NOT NULL THEN Date_of_pay END) as last_date_paid_for_product3
FROM products
GROUP BY Customer

Open in new window

0
 
LVL 19
ID: 41851482
did you make a mistake? did you mean to write this:
Customer ='A', last_date_paid_for_product1='20161012', last_date_paid_for_product2='20161013', last_date_paid_for_product3='20161014'

What is the data type for Date_of_pay ?
0
 
LVL 49

Expert Comment

by:Gustav Brock
ID: 41851594
You should rephrase this completely. You state two different resultsets, and have tagged the question wildly:

MS SQL Server MS Access .NET Programming

so no one knows what your are doing.

/gustav
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

In a multiple monitor setup, if you don't want to use AutoCenter to position your popup forms, you have a problem: where will they appear?  Sometimes you may have an additional problem: where the devil did they go?  If you last had a popup form open…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

932 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

20 Experts available now in Live!

Get 1:1 Help Now