我有SQL Server工作。我有超过20000行使用SQL server.I有列Filed Name Amount。 in Amount Filed Inserted negative and Positive Number。现在我想要Sort Amount字段负数和正数
示例:
Entity ExpenseTypeCode ExpenseType Amount
11 043 Hotel 5
12 044 travel 23
13 045 drink 55
14 046 Dinner 23
15 047 airline 556
16 048 Hotel -5
我如何喜欢超过30000个LINES。在我的表中我有费用类型但负值和正值 我想要排序我的表像负面和正面顺序相同的值
Entity ExpenseTypeCode ExpenseType Amount
11 043 Hotel 5
16 048 Hotel -5 --> Want sort like this
12 044 travel 23
13 045 drink 55
14 046 Dinner 23
15 047 airline 556
我怎样才能对表格进行排序?
答案 0 :(得分:5)
在排序中使用 ABS功能:
ABS():它会将您的负值转换为正值
SELECT
*
FROM TableName
Order BY ABS(Amount),Amount*-1
如果您想要负值和正值相同且订单先考虑为正,那么:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Graph</title>
</head>
<body>
<script src="../node_modules/angular/angular.min.js"></script>
<script src="../node_modules/chart.js/Chart.min.js"></script>
<script src="../node_modules/angular-chart.js/dist/angular-chart.min.js"></script>
<script>
var app = angular.module('app', ['chart.js']);
app.controller('BarCtrl', ['$scope', function ($scope) {
$scope.options = { legend: { display: true } };
$scope.labels = ['2006', '2007', '2008', '2009', '2010', '2011', '2012'];
$scope.series = ['Series A', 'Series B'];
$scope.data = [
[65, 59, 80, 81, 56, 55, 40],
[28, 48, 40, 19, 86, 27, 90]
];
}]);
</script>
<div ng-app="app" ng-controller="BarCtrl">
<canvas id="bar" chart-type="type"
chart-data="data" chart-labels="labels"> chart-series="series"
</canvas>
</div>
</body>
</html>
示例:强>
初始
输出
答案 1 :(得分:1)
select * from @t t
order by expensetype,
case when amount > 0 then 1 else 2 end
结果
Entity ExpenseTypeCode ExpenseType Amount
----------- --------------- ----------- -----------
15 47 airline 556
14 46 Dinner 23
13 45 drink 55
11 43 Hotel 5
16 48 Hotel -5
12 44 travel 23
如果您正在寻找匹配对,那么这样的东西可能就是您想要的
declare @t table(Entity int, ExpenseTypeCode int, ExpenseType varchar(10), Amount int)
insert into @t values
( 11, 043, 'Hotel' , 6),
( 8, 043, 'Hotel' , 5),
( 9, 043, 'Hotel' , 5),
( 10, 043, 'Hotel' , 5),
( 12, 044, 'travel' , 23),
( 13, 045, 'drink' , 55),
( 14, 046, 'Dinner' , 23),
( 15, 047, 'airline' , 556),
( 16, 048, 'Hotel' , -5),
( 17, 048, 'Hotel' , -5),
( 18, 043, 'Hotel' , -6),
( 19, 043, 'Hotel' , -6)
select t.*,row_number() over(partition by t.ExpenseType, t.amount order by t.entity) rn,t.amount as absamount
from @t t
where t.amount > 0
union all
select t.*,row_number() over(partition by t.ExpenseTypeCode, t.amount order by t.entity) rn, abs(t.amount)
from @t t
where t.amount < 0
order by t.expensetype,absamount,rn,t.amount desc
结果
Entity ExpenseTypeCode ExpenseType Amount rn absamount
----------- --------------- ----------- ----------- -------------------- -----------
15 47 airline 556 1 556
14 46 Dinner 23 1 23
13 45 drink 55 1 55
8 43 Hotel 5 1 5
16 48 Hotel -5 1 5
9 43 Hotel 5 2 5
17 48 Hotel -5 2 5
10 43 Hotel 5 3 5
11 43 Hotel 6 1 6
18 43 Hotel -6 1 6
19 43 Hotel -6 2 6
12 44 travel 23 1 23
或可能是完整的加入
select s.*,t.* from
(
select t.*,row_number() over(partition by t.ExpenseType, t.amount order by t.entity) rn
from @t t
where t.amount > 0
) s
full join
(
select t.*,row_number() over(partition by t.ExpenseTypeCode, t.amount order by t.entity) rn
from @t t
where t.amount < 0
) t on t.expensetype = s.expensetype and t.rn = s.rn and abs(t.amount) = s.amount
order by s.expensetype
Entity ExpenseTypeCode ExpenseType Amount rn Entity ExpenseTypeCode ExpenseType Amount rn
----------- --------------- ----------- ----------- -------------------- ----------- --------------- ----------- ----------- --------------------
NULL NULL NULL NULL NULL 19 43 Hotel -6 2
15 47 airline 556 1 NULL NULL NULL NULL NULL
14 46 Dinner 23 1 NULL NULL NULL NULL NULL
13 45 drink 55 1 NULL NULL NULL NULL NULL
11 43 Hotel 6 1 18 43 Hotel -6 1
10 43 Hotel 5 3 NULL NULL NULL NULL NULL
8 43 Hotel 5 1 16 48 Hotel -5 1
9 43 Hotel 5 2 17 48 Hotel -5 2
12 44 travel 23 1 NULL NULL NULL NULL NULL