具有相同帐户ID的记录,其中多个值作为一个表,每个值位于单独的字段中

时间:2018-01-02 17:46:34

标签: sql

我有一张看起来像这样的桌子 表:AcctPrograms

    Acct     ProgramOptions   Date_End
    100      A                2016-09-01 00:00:00.0000000   
    100      B                2017-01-01 00:00:00.0000000
    101      B                2017-01-01 00:00:00.0000000
    101      C                2018-01-01 00:00:00.0000000
    101      D                2017-01-01 00:00:00.0000000
    102      C                2018-01-01 00:00:00.0000000
    103      F                2016-06-01 00:00:00.0000000
    103      G                2015-12-14 00:00:00.0000000
    ...

我想创建一个如下所示的视图vw_AcctProgramOptions:

    Acct   ProgramOption1  ProgramOption2  ... ProgramOption10
    100    A               B
    101    B               C              
    102    C
    103    F               G
    ...

问题是,AcctPrograms表中的一个acct可以有10个以上的选项,但我只想从AcctPrograms表中通过MaxDate(Date_End)收集前10个订单。

非常感谢任何帮助。我已经在这方面工作了几天,似乎无法让它发挥作用。

非常感谢。

编辑: 使用MSSQL 2008

2 个答案:

答案 0 :(得分:1)

使用ROW_NUMBER订购按ProgramOptions分组的Acct,并使用CASE WHEN语句以列的形式提取订单中的前10个。

SELECT A.ACCT,
MAX(CASE WHEN A.RNUM = 1 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION1,
MAX(CASE WHEN A.RNUM = 2 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION2,
MAX(CASE WHEN A.RNUM = 3 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION3,
MAX(CASE WHEN A.RNUM = 4 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION4,
MAX(CASE WHEN A.RNUM = 5 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION5,
MAX(CASE WHEN A.RNUM = 6 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION6,
MAX(CASE WHEN A.RNUM = 7 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION7,
MAX(CASE WHEN A.RNUM = 8 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION8,
MAX(CASE WHEN A.RNUM = 9 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION9,
MAX(CASE WHEN A.RNUM = 10 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION10
FROM
(SELECT
ACCT, PROGRAMOPTIONS, DATE_END, ROW_NUMBER() OVER (PARTITION BY ACCT ORDER BY DATE_END) AS RNUM
FROM
ACCTPROGRAMS A
)
GROUP BY A.ACCT;

答案 1 :(得分:0)

- Vashi的回答是A在查询中略低一点

- 尝试查询

create table #table
(
    Acct int,
    ProgramOptions varchar(1),
    Date_End datetime
)

insert into #table
(
    Acct,
    ProgramOptions,
    Date_End
)
values
(    100,      'A',                '2016-09-01 00:00:00'   ),
(    100,      'B',                '2017-01-01 00:00:00'   ),
(    101,      'B',                '2017-01-01 00:00:00'   ),
(    101,      'C',                '2018-01-01 00:00:00'   ),
(    101,      'D',                '2017-01-01 00:00:00'   ),
(    102,      'C',                '2018-01-01 00:00:00'   ),
(    103,      'F',                '2016-06-01 00:00:00'   ),
(    103,      'G',                '2015-12-14 00:00:00'   )


SELECT A.ACCT,
MAX(CASE WHEN A.RNUM = 1 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION1,
MAX(CASE WHEN A.RNUM = 2 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION2,
MAX(CASE WHEN A.RNUM = 3 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION3,
MAX(CASE WHEN A.RNUM = 4 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION4,
MAX(CASE WHEN A.RNUM = 5 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION5,
MAX(CASE WHEN A.RNUM = 6 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION6,
MAX(CASE WHEN A.RNUM = 7 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION7,
MAX(CASE WHEN A.RNUM = 8 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION8,
MAX(CASE WHEN A.RNUM = 9 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION9,
MAX(CASE WHEN A.RNUM = 10 THEN A.PROGRAMOPTIONS ELSE NULL END) AS PROGRAMOPTION10
FROM
(SELECT
ACCT, PROGRAMOPTIONS, DATE_END, ROW_NUMBER() OVER (PARTITION BY ACCT ORDER BY DATE_END) AS RNUM
FROM
#table
) A
GROUP BY A.ACCT;


drop table #table