我有这张桌子:
count_0-10_days, amount_0-10_days, count_10-20_days, amount_10-20_days, count_20-30_days, amount_20-30_days
如何获得以下报告?
UNION
我目前使用.controller("photoCtrl", function($scope, $http) {
$scope.images = [];
$scope.getImages = function() {
$http.get('https://api.myjson.com/bins/30vuu')
.success(function(data) {
$scope.images = data;
})
}
});
并执行多个子查询,但我想知道是否有更好,更有效的方法吗?
答案 0 :(得分:2)
您是否尝试在选择中使用Sum(Case)?如果您的日期是非标准日期格式,请务必使用正确的日期模式。没有数据样本,这是我能建议的最好的。下面是如何为0-10情况执行此操作,重复您想要的其他日期范围。
Select
SUM( CASE WHEN [date_time] between TO_DATE([date_time], 'date pattern') AND TO_DATE([date_time], 'date pattern')-10 THEN 1 ELSE 0 END ) AS 'count_0-10_days',
SUM( CASE WHEN [date_time] between TO_DATE([date_time], 'date pattern') AND TO_DATE([date_time], 'date pattern')-10 THEN amount ELSE 0 END ) AS 'amount_0-10_days'
From [Table_name]
答案 1 :(得分:0)
select start_date,end_date,(rownum-1)*10+1||'-'||rownum*10 days, amount
from
(
select dr.start_date,dr.end_date,sum(t.amount) amount
from (
select ((trunc(to_date(<start_date>,'dd-mon-yyyy')))+(rownum-1)*10) start_date,
((trunc(to_date(<start_date>,'dd-mon-yyyy')))+(rownum)*10)-1 end_date
from dual connect by rownum < 366
) dr, <table_name> t
where
to_date(trunc(t.date_time), 'dd-mon-yyyy') between dr.start_date and dr.end_date
group by dr.start_date,dr.end_date
);