如何根据条件对数据进行分组?

时间:2018-10-15 04:15:43

标签: sql sql-server tsql pivot

我正在尝试使用sql打开当前表

customer.id sale_date 
15           1/12/2017
15           2/12/2017
15           7/12/2017
12           6/09/2017
12           12/09/2017
16           8/14/2017
13           6/01/2017
13           7/01/2017

变成这样的东西。

sale_date1是第一个订购日期。

sale_date2sale_date1之后一个月的任何订单日期。

sale_date3sale_date1后五个月的任何订单日期。

customer.id sale_date1  sale_date2  sale_date3(at least 5 months after sale_date1)
15           1/12/2017   2/12/2017   7/12/2017 
12           6/07/2017   NULL        12/09/2017
16           8/14/2017   NULL        NULL
13           6/01/2017   7/01/2017   NULL

4 个答案:

答案 0 :(得分:2)

这里的一个选项是使用相关子查询来填充三列中的每一列:

WITH cte AS (
    SELECT [customer.id], MIN(sale_date) AS min_sale_date 
    FROM yourTable
    GROUP BY [customer.id]
)

SELECT
    [customer.id],
    min_sale_date AS sale_date1,
    (SELECT MIN(t2.sale_date) FROM yourTable t2
     WHERE t1.[customer.id] = t2.[customer.id] AND
           t2.sale_date >= DATEADD(month, 1, t1.min_sale_date) AND
           t2.sale_date <  DATEADD(month, 5, t1.min_sale_date)) AS sale_date2,
    (SELECT MIN(t2.sale_date) FROM yourTable t2
     WHERE t1.[customer.id] = t2.[customer.id] AND
           t2.sale_date >= DATEADD(month, 5, t1.min_sale_date)) AS sale_date3
FROM cte t1
ORDER BY [customer.id];

enter image description here

Demo

答案 1 :(得分:0)

尝试使用row_number()和条件聚合

select customerid,max(case when seq=1 then sale_date end) as date1, 
max(case when seq=2 then sale_date end) as date2,
max(case when seq=3 then sale_date end) as date3
from
(
select *, row_number() over(partition by customerid order by sale_date) as seq
from tablename
)X
group by customerid

答案 2 :(得分:0)

我想这就是你想要的

SELECT A.customer.id, SALES1.sale_date , SALES2.sale_date ,SALES3.sale_date , SALES4.sale_date,SALES5.sale_date  from 
(SELECT distinct customer.id , 
From yourTable)A 
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=1)SALES1 
A.customer.id =  SALES1.customer.id
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=2)SALES2 
A.customer.id =  SALES1.customer.id
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=3)SALES3 
A.customer.id =  SALES1.customer.id
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=2)SALES2 
A.customer.id =  SALES1.customer.id
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=4)SALES4 
A.customer.id =  SALES1.customer.id
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=2)SALES2 
A.customer.id =  SALES1.customer.id
LEFT JOIN
(SELECT * from 
(SELECT customer.id, sale_date 
  ROW_NUMBER() OVER(ORDER BY sale_date ASC) 
    AS R1,
  name, recovery_model_desc
FROM yourTable)S1 where R1=5)SALES5 
A.customer.id =  SALES1.customer.id

答案 3 :(得分:0)

尝试一下:

with mindate as (
select id, min(sale_date) MinDate, 
DATEADD(month, 1, min(sale_date)) MinDatePlus1Month, 
DATEADD(month, 5, min(sale_date)) MinDatePlus2Month 
from yourtable
group by id
)
select f1.id, f1.MinDate sale_date1, f2.sale_date sale_date2, f3.sale_date sale_date3 
from mindate f1
left outer join yourtable f2 on f1.id=f2.id and f1.MinDatePlus1Month=f2.sale_date
left outer join yourtable f3 on f1.id=f3.id and f1.MinDatePlus2Month=f3.sale_date