Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ms sql + top 1 for each customer

Posted on 2016-10-17
3
Medium Priority
?
94 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 53

Expert Comment

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

Accepted Solution

by:
chaau earned 2000 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 52

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

722 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