我有一张表,该表包含“财政周末”和“财政年度”以及所有周末数据以及FY17和FY18年。下面是它在sql表中的显示方式。
Fiscal Week Ending Fiscal Year
20161112 FY17
20161126 FY17
20161210 FY17
20170204 FY17
20170218 FY17
20170304 FY17
20170318 FY17
20170401 FY17
20170520 FY17
20170603 FY17
20170722 FY17
20170805 FY17
20170819 FY17
20170902 FY17
20171021 FY17
20171104 FY17
20171111 FY18
20171230 FY18
20180120 FY18
20180203 FY18
20180303 FY18
20180421 FY18
20180505 FY18
20180623 FY18
20180707 FY18
20180721 FY18
20180804 FY18
20161224 FY17
20161231 FY17
20170107 FY17
20170114 FY17
20170121 FY17
20170325 FY17
20170909 FY17
20170916 FY17
20170923 FY17
20171007 FY17
20180407 FY18
20180602 FY18
20180609 FY18
20180616 FY18
20161119 FY17
20161203 FY17
20161217 FY17
20170128 FY17
20170211 FY17
20170225 FY17
20170311 FY17
20170429 FY17
20170513 FY17
20170527 FY17
20170610 FY17
20170729 FY17
20170812 FY17
20170930 FY17
20171014 FY17
20171028 FY17
20171104 FY18
20171223 FY18
20180113 FY18
20180127 FY18
20180331 FY18
20180414 FY18
20180428 FY18
20180512 FY18
20180630 FY18
20180714 FY18
20161105 FY17
20170408 FY17
20170415 FY17
20170422 FY17
20170506 FY17
20170617 FY17
20170624 FY17
20170701 FY17
20170708 FY17
20170715 FY17
20170826 FY17
20171118 FY18
20171125 FY18
20171202 FY18
20171209 FY18
20171216 FY18
20180106 FY18
20180210 FY18
20180217 FY18
20180224 FY18
20180310 FY18
20180317 FY18
20180324 FY18
20180519 FY18
20180526 FY18
20180728 FY18
我正在寻找的是显示如下数据。
FY17 FY18
20161112 20171111
20161126 20171230
20161210 20180120
20170204 20180203
20170218 20180303
20170304 20180421
20170318 20180505
20170401 20180623
20170520 20180707
20170603 20180721
20170722 20180804
20170805 20180407
20170819 20180602
20170902 20180609
20171021 20180616
20171104 20171104
20161224 20171223
20161231 20180113
20170107 20180127
20170114 20180331
20170121 20180414
20170325 20180428
20170909 20180512
20170916 20180630
20170923 20180714
20171007 20171118
20161119 20171125
20161203 20171202
20161217 20171209
20170128 20171216
20170211 20180106
20170225 20180210
20170311 20180217
20170429 20180224
20170513 20180310
20170527 20180317
20170610 20180324
20170729 20180519
20170812 20180526
20170930 20180728
20171014
20171028
20161105
20170408
20170415
20170422
20170506
20170617
20170624
20170701
20170708
20170715
20170826
任何人都可以通过SQL语句帮助我如何做到这一点。
答案 0 :(得分:1)
如果您的数据库类型和版本支持窗口功能,
那么您可以使用ROW_NUMBER来获取一个号码。
一个数字,基于每个FiscalYear的FiscalWeekEnd。
之后,您可以通过条件聚合来进行透视。
SELECT
MAX(CASE WHEN FiscalYear = 'FY17' THEN FiscalWeekEnding END) AS FY17,
MAX(CASE WHEN FiscalYear = 'FY18' THEN FiscalWeekEnding END) AS FY18
FROM
(
SELECT
ROW_NUMBER() OVER (PARTITION BY "Fiscal Year" ORDER BY "Fiscal Week Ending") AS RN,
"Fiscal Year" AS FiscalYear,
"Fiscal Week Ending" AS FiscalWeekEnding
FROM yourtable
WHERE "Fiscal Year" IN ('FY17','FY18')
) q
GROUP BY RN
ORDER BY RN