SQL - How to get columns from row values in the same column (SQL Server 2016)
up vote
0
down vote
favorite
I need to derive columns from the row values of one column.
Here's the row data.
CustomerID Activity Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06
And here's the output that I am trying to get to:
CustomerID ActiveDate InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06
Please help! Thanks!
sql sql-server
add a comment |
up vote
0
down vote
favorite
I need to derive columns from the row values of one column.
Here's the row data.
CustomerID Activity Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06
And here's the output that I am trying to get to:
CustomerID ActiveDate InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06
Please help! Thanks!
sql sql-server
please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47
Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I need to derive columns from the row values of one column.
Here's the row data.
CustomerID Activity Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06
And here's the output that I am trying to get to:
CustomerID ActiveDate InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06
Please help! Thanks!
sql sql-server
I need to derive columns from the row values of one column.
Here's the row data.
CustomerID Activity Date
10001 Active 2018-06-21
10001 Inactive 2018-06-25
10001 Active 2018-08-22
10001 Inactive 2018-10-06
And here's the output that I am trying to get to:
CustomerID ActiveDate InactiveDate
10001 2018-06-21 2018-06-25
10001 2018-08-22 2018-10-06
Please help! Thanks!
sql sql-server
sql sql-server
edited Nov 10 at 13:01
Gordon Linoff
748k34285391
748k34285391
asked Nov 10 at 0:35
user10631408
11
11
please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47
Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08
add a comment |
please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47
Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08
please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47
please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47
Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08
Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
You can try to make row number in subquery group by CustomerID,Activity
, then do condition aggregate function.
SELECT CustomerID,
MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
FROM T
)t1
group by CustomerID,rn
sqlfiddle
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Yep the problem is sample data have sameCustomerid
, therefore I need to create a row number for it :)
– D-Shih
Nov 10 at 1:15
add a comment |
up vote
0
down vote
You logic is a little unclear. If you want the next "inactive" date:
select CustomerID, date as active_date, inactive_date
from (select t.*,
min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
from t
) t
where activity = 'Active';
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
You can try to make row number in subquery group by CustomerID,Activity
, then do condition aggregate function.
SELECT CustomerID,
MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
FROM T
)t1
group by CustomerID,rn
sqlfiddle
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Yep the problem is sample data have sameCustomerid
, therefore I need to create a row number for it :)
– D-Shih
Nov 10 at 1:15
add a comment |
up vote
1
down vote
You can try to make row number in subquery group by CustomerID,Activity
, then do condition aggregate function.
SELECT CustomerID,
MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
FROM T
)t1
group by CustomerID,rn
sqlfiddle
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Yep the problem is sample data have sameCustomerid
, therefore I need to create a row number for it :)
– D-Shih
Nov 10 at 1:15
add a comment |
up vote
1
down vote
up vote
1
down vote
You can try to make row number in subquery group by CustomerID,Activity
, then do condition aggregate function.
SELECT CustomerID,
MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
FROM T
)t1
group by CustomerID,rn
sqlfiddle
You can try to make row number in subquery group by CustomerID,Activity
, then do condition aggregate function.
SELECT CustomerID,
MAX(CASE WHEN Activity = 'Active' THEN Date END) ActiveDate,
MAX(CASE WHEN Activity = 'Inactive' THEN Date END) InactiveDate
FROM (
SELECT *,ROW_NUMBER() OVER(PARTITION BY CustomerID,Activity ORDER BY Date ) rn
FROM T
)t1
group by CustomerID,rn
sqlfiddle
answered Nov 10 at 0:50
D-Shih
24.4k61431
24.4k61431
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Yep the problem is sample data have sameCustomerid
, therefore I need to create a row number for it :)
– D-Shih
Nov 10 at 1:15
add a comment |
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Yep the problem is sample data have sameCustomerid
, therefore I need to create a row number for it :)
– D-Shih
Nov 10 at 1:15
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
I dont see any need for row number here.
– Martin Smith
Nov 10 at 1:03
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
if didn't make row number the pivot will get only one row? or Could you post your another solution? I am looking forward :)
– D-Shih
Nov 10 at 1:11
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Did you mean this? dbfiddle.uk/…
– D-Shih
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Ah sorry just realised I missed that it was the same Customerid throughout. Thought they were 2 different customers
– Martin Smith
Nov 10 at 1:14
Yep the problem is sample data have same
Customerid
, therefore I need to create a row number for it :)– D-Shih
Nov 10 at 1:15
Yep the problem is sample data have same
Customerid
, therefore I need to create a row number for it :)– D-Shih
Nov 10 at 1:15
add a comment |
up vote
0
down vote
You logic is a little unclear. If you want the next "inactive" date:
select CustomerID, date as active_date, inactive_date
from (select t.*,
min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
from t
) t
where activity = 'Active';
add a comment |
up vote
0
down vote
You logic is a little unclear. If you want the next "inactive" date:
select CustomerID, date as active_date, inactive_date
from (select t.*,
min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
from t
) t
where activity = 'Active';
add a comment |
up vote
0
down vote
up vote
0
down vote
You logic is a little unclear. If you want the next "inactive" date:
select CustomerID, date as active_date, inactive_date
from (select t.*,
min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
from t
) t
where activity = 'Active';
You logic is a little unclear. If you want the next "inactive" date:
select CustomerID, date as active_date, inactive_date
from (select t.*,
min(case when activity = 'Inactive' then date end) over (partition by CustomerID order by date desc) as inactive_date
from t
) t
where activity = 'Active';
answered Nov 10 at 13:03
Gordon Linoff
748k34285391
748k34285391
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53234979%2fsql-how-to-get-columns-from-row-values-in-the-same-column-sql-server-2016%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
please provide a minimal, complete, and verifiable example
– landru27
Nov 10 at 0:47
Are you certain that for every "active" there is an associated "inactive" that is the row that immediately follows (based on date)? Don't assume, go look. And don't just look at the first set of rows that you see in the results window of a SSMS query (or the results of the "select rows" menu).
– SMor
Nov 10 at 2:08