需要在分区中选择最新的5条记录

时间:2016-02-23 03:42:13

标签: sql teradata

我陷入了困境。这可能很简单,但我没有通过。

我有一个审计表Audit_Info,它捕获所有表的审计信息。 表可以在同一个工作日期多次运行。我的要求是获取最近5个月的每个月的最新业务日期记录。可能会发生在一个特定的月份,表格没有运行。

表就像

table_name business_date src_rec tgt_rec del_rec load_timestamp
abc          25/10/2015   10      10      0       23/01/2016 03:06:56
abc          25/10/2015   10      10      0       23/01/2016 05:23:34
abc          07/09/2015   10      10      0       23/10/2015 05:37:30
abc          05/08/2015   10      10      0       23/09/2015 05:23:34
abc          15/06/2015   10      10      0       23/07/2015 05:23:34
abc          25/04/2015   10      10      0       23/05/2015 05:23:34

类似的还有其他表格。我需要5张桌子。

感谢您的帮助。

此致 阿米特Please see the highlighted

3 个答案:

答案 0 :(得分:3)

根据您的预期结果,这应该接近:

app.config([ '$stateProvider', '$urlRouterProvider','$authProvider', '$controllerProvider',  '$httpProvider', '$compileProvider', '$filterProvider', '$provide', '$ocLazyLoadProvider', 'JS_REQUIRES',
function ($stateProvider, $urlRouterProvider, $authProvider, $controllerProvider, $compileProvider, $httpProvider, $filterProvider, $provide, $ocLazyLoadProvider, jsRequires) {

app.controller = $controllerProvider.register;
app.directive = $compileProvider.directive;
app.filter = $filterProvider.register;
app.factory = $provide.factory;
app.service = $provide.service;
app.constant = $provide.constant;
app.value = $provide.value;

$ocLazyLoadProvider.config({
    debug: false,
    events: true,
    modules: jsRequires.modules
});

function redirectWhenLoggedOut($q, $injector) {
    return {

        responseError: function(rejection) {

            // Need to use $injector.get to bring in $state or else we get
            // a circular dependency error
            var $state = $injector.get('$state');

            // Instead of checking for a status code of 400 which might be used
            // for other reasons in Laravel, we check for the specific rejection
            // reasons to tell us if we need to redirect to the login state
            var rejectionReasons = ['token_not_provided', 'token_expired', 'token_absent', 'token_invalid'];

            // Loop through each rejection reason and redirect to the login
            // state if one is encountered
            angular.forEach(rejectionReasons, function(value, key) {

                if(rejection.data.error === value) {

                    // If we get a rejection corresponding to one of the reasons
                    // in our array, we know we need to authenticate the user so 
                    // we can remove the current user from local storage
                    localStorage.removeItem('user');

                    // Send the user to the auth state so they can login
                    $state.go('login.signin');
                }
            });
            return $q.reject(rejection);
        }
    }
}

// Setup for the $httpInterceptor
$provide.factory('redirectWhenLoggedOut', redirectWhenLoggedOut);

// Push the new factory onto the $http interceptor array
$httpProvider.interceptors.push('redirectWhenLoggedOut');

$authProvider.loginUrl = 'http://localhost/himalayan-engineering-college/public/api/authenticate';
// APPLICATION ROUTES
// -----------------------------------
// For any unmatched url, redirect to /app/dashboard
$urlRouterProvider.otherwise("/login/signin");
//
// Set up the states
$stateProvider.state('app', {
        url: "/app",
        templateUrl: "assets/views/app.html",
        resolve: loadSequence('modernizr', 'moment', 'angularMoment', 'uiSwitch', 'perfect-scrollbar-plugin', 'perfect_scrollbar', 'toaster', 'ngAside', 'vAccordion', 'sweet-alert', 'chartjs', 'tc.chartjs', 'oitozero.ngSweetAlert','satellizer','AuthController'),
        abstract: true
    }).state('app.dashboard', {
        url: "/dashboard",
        templateUrl: "assets/views/dashboard.html",
        resolve: loadSequence('satellizer','jquery-sparkline', 'dashboardCtrl','chartsCtrl'),
        title: 'Dashboard',
        ncyBreadcrumb: {
            label: 'Dashboard'
        }
    })....//more states
}]);

答案 1 :(得分:1)

嗯,如果我理解正确,你可以使用row_number()进行日期算术:

select ai.*
from (select ai.*,
             row_number() over (partition by table_name, extract(year from business_date), extract(month from business_date)
                                order by business_date desc
                               ) as seqnum
      from audit_info ai
      where timestamp >= current timestamp - interval '5' month
     ) ai
where seqnum = 1;

答案 2 :(得分:1)

如果我理解正确,您希望最近5个月拥有数据的最佳日期。如果是这样,请按年和月分组,并使用max功能选择每月最长的日期:

select top 5 
    max(business_date), extract(year from business_date) , extract(month from business_date)
from mytable
group by extract(year from business_date), extract(month from business_date)
order by extract(year from business_date) desc, extract(month from business_date) desc