Solved

ms sql + top 1 for each customer

Posted on 2016-10-17
3
44 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 46

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

867 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

12 Experts available now in Live!

Get 1:1 Help Now