Solved

ms sql + top 1 for each customer

Posted on 2016-10-17
3
84 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
[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
3 Comments
 
LVL 52

Expert Comment

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

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 51

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Viewers will learn how the fundamental information of how to create a table.
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…

636 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