MAX的多重选择

时间:2019-03-25 07:54:53

标签: sql-server

我有一个T-SQL。并且有多个选择语句,我想获得最大的CompleteDate。但是如何添加GROUP BY语句?

 <!DOCTYPE html>
    <html ng-app="PIR_Detection">
    <head>
        <meta charset="utf-8" />
        <title>PIR Door Monitoring</title>
        <script>document.write('<base href="' + document.location + '" />');</script>
        <script data-require="angular.js@1.4.x" src="https://code.angularjs.org/1.4.12/angular.js" data-semver="1.4.9"></script>
    </head>
    <body ng-controller="myCtrl">
        <div class="col-sm-4 col-lg-offset-4" ng-controller="myCtrl">
            <select class="form-control" ng-model="sel_val" ng-change="getPIRData(sel_val.deveui)" ng-options="data.deveui for data in Customers">Select PIR Device</select>
        </div>
        <br />
        <span style="font-size:14px" id="pirstatus"></span>
        <h1> {{pirstatus}}</h1>
        <span ng-bind="pirstatus"></span> 
        <script>
            var app = angular.module('PIR_Detection', []);
            app.controller('myCtrl', function ($scope, $http, $window) {
                $scope.sel_val = 0;
                $scope.DefaultLabel = "Loading.....";
                var post = $http({
                    method: "get",
                    url: "../data.json",
                    dataType: 'json',
                    data: {},
                    headers: { "Content-Type": "application/json" }
                });
                post.success(function (data, status) {
                    $scope.Customers = data;
                });
                post.error(function (data, status) {
                });
                $scope.getPIRData = function (id) {
                    var url = "/PIRDetails/GetPIRStatus/" + id;
                    $http.get(url)
                        .then(function (response) {
                            $scope.myWelcome = response.data;
                            $scope.pirstatus = base64toHEX($scope.myWelcome.dataFrame);
                            document.getElementById("pirstatus").innerHTML = $scope.pirstatus;
                        });
                };
            });


        </script>

    </body>
    </html>

1 个答案:

答案 0 :(得分:0)

有可能使用子查询来获得MAX,然后GROUP BY其列:

SELECT
  max(q.CompleteDate) MaxCompleteDate
, q.sort
--,  your grouped columns here
FROM
(
SELECT 
* 
FROM 
(
    SELECT
    ROW_NUMBER() OVER (PARTITION BY [Description],[SolveIssueCategory],
        [SolveIssueName],[SolveExplanation] ORDER BY [CompleteDate],[CompleteTime],
        [Status]) as sort, 
    * FROM
        (SELECT 
            ISNULL ([DispatchListMain].[Description] , '') as 'Description' ,
            ISNULL (SI.SolveIssueName , '') as 'SolveIssueName' ,
            ISNULL ([SolveIssue].[SolveIssueName] , '') as 'SolveIssueName' ,
            ISNULL ([Detail].[SolveExplanation] , '') as 'SolveExplanation' ,
            MAX (ISNULL (CONVERT (char(10) , [DetailTotal].[CompleteDate] , 111) , '')) 
                as 'CompleteDate' ,
            MAX (ISNULL (CONVERT (varchar(5) , CONVERT (datetime , 
                [DetailTotal].[CompleteDate]) , 108) , '')) as 'CompleteTime' ,
            ISNULL (CASE [Detail].[Status]
            WHEN 'S00' THEN 'temp'
            END , '') as 'Status'
    FROM 
        [DispatchListMain] JOIN [Detail] ON [Detail].[DispatchListMainId] = 
            [DispatchListMain].[id] 
        JOIN [Personnel]
        ON [Personnel].[Id] = [DispatchListMain].[PersonnelId]
        LEFT OUTER JOIN [SolveIssue] ON [SolveIssue].[Id] = [Detail].[SolveReason]
        JOIN [DetailTotal] ON [DetailTotal].[DispatchListMainId] = [DispatchListMain].[Id]
        LEFT OUTER JOIN 
        (SELECT [Id] , [SolveIssueCode] 
         , [SolveIssueName] , [SolveIssueCategory] 
         , [ParentId] 
         FROM [SolveIssue] WHERE SolveIssueCategory = 
            'SolveIssueCategory') SI
        ON SI.Id = [DetailTotal].[IssueCategory] 
    WHERE
        --GROUP BY [DispatchListMain].[id] 
        ERPNo = 'GN18030003' AND
        [Detail].[Status] <> 'S61' AND
        (CONVERT (varchar(10),([Detail].[CreateDate])) >= '2019/03/18' 
             AND CONVERT (varchar(10),([Detail].[CreateDate])) <= + '2019/03/25')) temp ) 
             temp2 WHERE temp2.sort = 1 ORDER BY 'CreateDate' DESC
)q
GROUP BY q.CompleteData, q.sort --, other your columns