Solved

get the maximum value on a condition

Posted on 2016-10-19
3
43 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
Comment Utility
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 18
Comment Utility
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
Comment Utility
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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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…
Viewers will learn how the fundamental information of how to create a table.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

743 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

14 Experts available now in Live!

Get 1:1 Help Now