Solved

ms sql + top 1 for each customer

Posted on 2016-10-17
3
36 Views
Last Modified: 2016-10-18
i have a table with these columns

Customer ID
ScheduleDate
Activity
Scheduled

I need a query that pulls the first row for each customerID that has 2 columns, the one column would be the first entry for the customer with the criteria of scheduled = 1 and then the next column would be the first entry in the list where scheduled = 0

the data is a list with multiple entries for each customer on any given day, can be as many as 10 entries per customer, and each row can have an entry of scheduled =1 or 0

Thanks
0
Comment
Question by:dkilby
3 Comments
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 41847613
do you have sample data with expected output?
0
 
LVL 24

Accepted Solution

by:
chaau earned 500 total points
ID: 41847649
There are number of ways you can achieve it. With correlated queries:
SELECT a.CustomerID,
(SELECT MIN(ScheduleDate) FROM tbl WHERE CustomerID = a.CustomerID and scheduled = 1) as sch_1,
(SELECT MIN(ScheduleDate) FROM tbl WHERE CustomerID = a.CustomerID and scheduled = 0) as sch_0
FROM tbl a
GROUP BY a.CustomerID

Open in new window

More cool way with the ROW_NUMBER() window function:
with e as (
SELECT CustomerID,
scheduled,
ScheduleDate,
ROW_NUMBER() OVER(PARTITION BY CustomerID, scheduled ORDER BY ScheduleDate) rn
FROM tbl)
SELECT e1.CustomerID, e1.ScheduleDate as sch_1, e2.ScheduleDate as sch_0
FROM e e1 LEFT JOIN e e2 ON e1.CustomerID=e2.CustomerID AND e1.scheduled=1 and e2.scheduled=0 and e1.rn=1 and e2.rn=1

Open in new window

With just a plain GROUP BY:
SELECT CustomerID,
MIN(CASE WHEN scheduled = 1 THEN ScheduleDate END) as sch_1,
MIN(CASE WHEN scheduled = 0 THEN ScheduleDate END) as sch_0
FROM tbl
GROUP BY CustomerID

Open in new window


All should produce the same result
0
 
LVL 45

Expert Comment

by:Vitor Montalvão
ID: 41847820
I understood your question a little different from Chaau so if I'm right then this should work for you:
SELECT [Customer ID], scheduled, MIN(ScheduleDate) MinScheduleDate
FROM tblName
GROUP BY [Customer ID], scheduled

Open in new window

0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

758 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

21 Experts available now in Live!

Get 1:1 Help Now