请查看以下SQL查询及其结果。
WITH Temp ([Description], [Amount], [OverdueBy])
AS
(select Description, SUM(Amount) as Amount, (DATEDIFF(day,DueDate,GETDATE())) as OverdueBy from brvAPAllInvoices
Where PaidDate is null and APCo = 1
Group By Description, DueDate
)
SELECT CASE WHEN OverdueBy >= 46 THEN 'Overdue by 46+ days+'
WHEN OverdueBy >= 31 and OverdueBy < 45 THEN 'Overdue by 31-45 days'
WHEN OverdueBy >= 15 and OverdueBy < 30 THEN 'Overdue by 15-30 days'
WHEN OverdueBy >= 8 and OverdueBy < 14 THEN 'Overdue by 8-14 days'
WHEN OverdueBy >= 1 and OverdueBy < 7 THEN 'Overdue by 1-7 days'
WHEN OverdueBy <= 0 and OverdueBy > -7 THEN 'Due in 0-7 days'
WHEN OverdueBy <= -8 and OverdueBy > -14 THEN 'Due in 8-14 days'
WHEN OverdueBy <= -15 and OverdueBy > -30 THEN 'Due in 15-30 days'
WHEN OverdueBy <= -31 and OverdueBy > -45 THEN 'Due in 45+ days'
ELSE 'Less than that' END AS OverdueRange,
Count(*) as OverdueCount,
Sum(Temp.Amount) as [Total Amount]
From Temp
group by CASE WHEN OverdueBy >= 46 THEN 'Overdue by 46+ days+'
WHEN OverdueBy >= 31 and OverdueBy < 45 THEN 'Overdue by 31-45 days'
WHEN OverdueBy >= 15 and OverdueBy < 30 THEN 'Overdue by 15-30 days'
WHEN OverdueBy >= 8 and OverdueBy < 14 THEN 'Overdue by 8-14 days'
WHEN OverdueBy >= 1 and OverdueBy < 7 THEN 'Overdue by 1-7 days'
WHEN OverdueBy <= 0 and OverdueBy > -7 THEN 'Due in 0-7 days'
WHEN OverdueBy <= -8 and OverdueBy > -14 THEN 'Due in 8-14 days'
WHEN OverdueBy <= -15 and OverdueBy > -30 THEN 'Due in 15-30 days'
WHEN OverdueBy <= -31 and OverdueBy > -45 THEN 'Due in 45+ days'
ELSE 'Less than that' END
结果:
+-----------------------+--------------+--------------+
| OverdueRange | OverdueCount | Total Amount |
+-----------------------+--------------+--------------+
| Due in 0-7 days | 14 | 101631.81 |
| Due in 15-30 days | 337 | 491171.32 |
| Due in 8-14 days | 7 | 1829.62 |
| Less than that | 26 | 23325.68 |
| Overdue by 1-7 days | 112 | 389606.12 |
| Overdue by 15-30 days | 18 | 49001.10 |
| Overdue by 31-45 days | 13 | -92473.18 |
| Overdue by 46+ days+ | 50 | 380746.95 |
| Overdue by 8-14 days | 11 | 18399.05 |
+-----------------------+--------------+--------------+
我想手动指定订单或OverDueRange字段,以便按照我想要的顺序不按字母顺序排列。
为实现这一目标,我在搜索的底部添加了以下代码:
ORDER BY (CASE OverdueRange
WHEN 'Overdue by 46+ days+' THEN 1
WHEN 'Overdue by 15-30 days' THEN 2
WHEN 'Overdue by 8-14 days' THEN 3
WHEN 'Overdue by 1-7 days' THEN 4
WHEN 'Due in 0-7 days' THEN 5
WHEN 'Due in 8-14 days' THEN 6
WHEN 'Due in 15-30 days' THEN 7
WHEN 'Due in 45+ days' THEN 8
ELSE 100 END) ASC, OverdueRange DESC;
当我执行代码时,收到以下错误:
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
Msg 207, Level 16, State 1, Line 36
Invalid column name 'OverdueRange'.
我做错了什么?
答案 0 :(得分:3)
您不需要case
语句。只是做:
ORDER BY MAX(temp.OverdueBy) DESC;
这将选择每个范围的最大值。根据范围的定义,它应该按照您想要的顺序(您也可以使用MIN(temp.OverdueBy))
。