如何在MySQL Group By查询中将日期列值转换为列名

时间:2019-01-11 00:02:10

标签: mysql group-by pivot

表结构:

$scope.splitMerchants =[
        {id: 1,         label: 'Edit'},
        {id: 2,         label: 'Open'},
        {id: 3,         label: 'Close'},
        {id: 4,         label: 'Close1'},
        {id: 5,         label: 'Close2'},
        {id: 6,         label: 'Close3'},
        {id: 7,         label: 'Close4'}
    ];
$scope.currentPage = 1;
$scope.page = $scope.splitMerchants[0];

$scope.paginationPreviousButton = function(){
            $scope.currentPage--;
            if ($scope.currentPage <= 0) {
                $scope.currentPage = 1;
            }
            $scope.page = $scope.splitMerchants[$scope.currentPage-1];
};

$scope.paginationNextButton = function() {
           $scope.currentPage++;
           if($scope.currentPage > $scope.splitMerchants.length) { 
                $scope.currentPage = $scope.splitMerchants.length;
           }
           $scope.page = $scope.splitMerchants[$scope.currentPage-1];
};

$scope.paginationDropdownChange = function(page) {
            $scope.currentPage = page.id;
};

以下是我对上表的查询:

ID    NAME    SUM_HOURS   WEEK

1     name1     32        2018-01-01
1     name1     30        2018-02-01
1     name1     35        2018-03-01

所需的表结构:

SELECT id                                    id,
e.name                                       name,
SUM(f.hours)                                 sum_hours,
fc.from_date                                 week,
FROM factdaily f, finance_calendar_full  fc, employee e
WHERE f.date                    = fc.date
AND f.emp_id                    = e.id 
AND f.date                   >= valid_from
AND f.date                   <= valid_to       
GROUP BY week
HAVING sum_hours > 0

由于我无法弄清任何帮助,因此深表感谢。

0 个答案:

没有答案