SQL Pivot-结果集的第一行包含空字段

时间:2019-05-30 13:36:56

标签: sql sql-server jaspersoft-studio

我有一个数据透视查询,其结果如下:

Sample  Un      Cu  Un      Si          Un
123 mg/kg       6   mg/kg       
321 mg/kg       6   mg/kg       1.26    %

这些结果是使用sql查询并在Jasper报告上绘制的结果获得的:

WITH pivot_data AS(
    SELECT va.identity,
    vc.units,
    s.field_name "Sample ID",
    s.id_text "Lab ID",
    TO_CHAR(str.result_value, S_FORMATMASK_PACKAGE.s_FormatMask(vc.analysis, s.id_numeric))result_value
  from samp_test_result str
  inner join sample s on str.id_numeric = s.id_numeric and str.id_text = s.id_text
  inner join client c on c.id = s.client_id
  inner join versioned_analysis va on va.identity = str.analysis
  inner join versioned_component vc on vc.analysis = va.identity and vc.analysis_version = va.analysis_version and vc.name = str.component_name
  WHERE s.fas_sample_type = 'LEAF'
  AND s.status            = 'A'
  AND s.flg_released      = 'T'
  AND vc.flg_report       = 'T'
  AND c.id = UPPER ('I000009')
  AND s.ID_NUMERIC between TO_NUMBER(2126) and TO_NUMBER(12917)
  order by vc.units desc
  )
SELECT pvt12.*
FROM(SELECT * FROM pivot_data PIVOT ( MAX(result_value) result_value , MAX(units) units FOR identity IN(
                                                                                                  'NIR_N' "N",
                                                                                                  'XRF_P' "P",
                                                                                                  'XRF_CA' "Ca",
                                                                                                  'XRF_MG' "Mg",
                                                                                                  'XRF_MN' "Mn", 
                                                                                                  'XRF_S' "S",
                                                                                                  'XRF_ZN' "Zn", 
                                                                                                  'XRF_CU' "Cu", 
                                                                                                  'XRF_FE' "Fe",
                                                                                                  'XRF_K' "K",
                                                                                                  'XRF_SI' "Si")))  pvt12 
union                                                                                              
SELECT pvt1.*
FROM(SELECT * FROM pivot_data PIVOT ( MAX(result_value) result_value , MAX(units) units FOR identity IN(
                                                                                                  'NITROGEN' "N",
                                                                                                  'LEAF_P'  "P",
                                                                                                  'LEAF_CA' "Ca",
                                                                                                  'LEAF_MG' "Mg",
                                                                                                  'LEAF_MN' "Mn",
                                                                                                  'LEAF_S'  "S",
                                                                                                  'LEAF_ZN' "Zn",
                                                                                                  'LEAF_CU' "Cu",
                                                                                                  'LEAF_FE' "Fe",
                                                                                                  'XRF_K' "K", 
                                                                                                  'XRF_SI' "Si")))  pvt1   

Jasper上的问题是,只要结果集的第一行为空,则在我的Jasper报告中该列的标题为空。有人可以帮忙吗:)

1 个答案:

答案 0 :(得分:0)

如果在选择列表中指定列的显式列表,而不是选择*。 您可以使用notull或合并将可能为null的列的null替换为0或空字符串。