在订单之间添加一个空行

时间:2018-04-24 11:07:08

标签: sql

以下查询:

SELECT t1.account, t1.group_name as "Group", t1.balance as Debit_Balance, t1.short_mkt_value as Short, t1.int_balance as "Long", t1.report_date as Date, t1.balance-t2.balance Daily_Change
FROM table t1
LEFT JOIN table t2
ON t2.report_date=DATE_SUB(t1.report_date, INTERVAL 1 DAY)
AND t1.account=t2.account
where t1.group_name = "Group"
ORDER BY t1.report_date, t1.account

生成以下数据:

account Group   Debit_Balance   Short     Long         Date     Daily_Change
716-05  Carter   12861700      7892750   20754400    4/11/2018    -6119444
716-06  Carter   -8717010      10302500  1585470     4/11/2018    -1190944
716-07  Carter    7900390      20151900  28052300    4/11/2018     1303464
716-05  Carter    8689760       7064350  15754100    4/12/2018    -4171923
716-06  Carter  -10195500      12064200   1868720    4/12/2018    -1478470
716-07  Carter    6709190      20897900  27607100    4/12/2018    -1191199.5

如何更改查询以便在每组帐户/分组后都有一个空行?

首选结果:

account Group   Debit_Balance   Short     Long         Date     Daily_Change
716-05  Carter   12861700      7892750   20754400    4/11/2018    -6119444
716-06  Carter   -8717010      10302500  1585470     4/11/2018    -1190944
716-07  Carter    7900390      20151900  28052300    4/11/2018     1303464

716-05  Carter    8689760       7064350  15754100    4/12/2018    -4171923
716-06  Carter  -10195500      12064200   1868720    4/12/2018    -1478470
716-07  Carter    6709190      20897900  27607100    4/12/2018    -1191199.5

1 个答案:

答案 0 :(得分:1)

SELECT 0 as group_order, t1.account, t1.group_name as "Group", t1.balance as Debit_Balance, t1.short_mkt_value as Short, t1.int_balance as "Long", t1.report_date as Date, t1.balance-t2.balance Daily_Change
FROM table t1
LEFT JOIN table t2
ON t2.report_date=DATE_SUB(t1.report_date, INTERVAL 1 DAY)
AND t1.account=t2.account
where t1.group_name = "Group"

UNION ALL
SELECT DISTINCT 1 as group_order, '' as account, t1.group_name as "Group", 0 as Debit_Balance, 0 as Short, 0 as "Long", t1.report_date as  Date, 0 as Daily_Change
FROM table t1

ORDER BY group_order, Date, account

因此,您为组和日期引入了不同的组值。通常group_order为0(第一个),对于人工组值,它为1(最后)。