MySQL:从转换为列的行中删除null

时间:2016-11-24 09:57:02

标签: mysql sql

我已经进行了查询,其中,我将开发人员关联项目状态的结果提取到列中。但是,我正在努力删除空值。

以下是我的疑问:

SELECT  GROUP_CONCAT(DISTINCT 
    CONCAT(
        'CASE when project=''', project, 
        '''and resource_name=''', resource_name,
        ''' then Tracker end AS `', 'Tracker' , '`')
    ) INTO @sql2        
FROM
    finance_dashboard.resource_utilization_data;

SET @sql = CONCAT('SELECT distinct resource_name, ', @sql2, ' FROM         
    finance_dashboard.resource_utilization_data');


PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

输出如下:

'babu.balakrishnan', 'Feature', NULL,      NULL
'prasenjit.ghosh',   NULL,      'Feature', NULL
'shamim.akhtar',     NULL,      NULL,      'Feature'
'babu.balakrishnan', 'Support', NULL,      NULL
'shamim.akhtar',     NULL,      NULL,      'Support'

我希望输出的内容是这样的:

'babu.balakrishnan', 'Feature', 
                   , 'Support'

'prasenjit.ghosh',  'Feature'

'shamim.akhtar',    'Feature'
                    'Support'

是否有任何可能性,仅使用查询。

UPDATE1 带聚合的group_concat将结果显示为:

 'babu.balakrishnan', 'Feature,Support', NULL, NULL
 'prasenjit.ghosh', NULL, 'Feature', NULL
 'shamim.akhtar', NULL, NULL, 'Feature,Support'

UPDATE2 表格结构如下:

projectTrackerresource_nameTotal_Hoursdateproject_counts

以下查询将数据插入表中:

    insert into finance_dashboard.resource_utilization_data
    select p.project,
    p.Tracker,
    p.resource_name,
    p.estimated_hours as Developer_Estimated_Hours,
    p.date,
    p.project_counts
    from project_stats.resource_utilization_summary p 
    where p.date>=@start_date 
    and p.date<=@end_date
    order by p.date;

某些值可以硬编码以获得所需格式的结果。

0 个答案:

没有答案