Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a query that's joined to other tables. I am pivoting the data based on result_value and units. These two items are related to each other.

E.g. Cu measurement = ppm , Cu value = 27

However there are fields in the column that are null. How do I still print its measurement irrespective if its associated column is null. The fields represented in the Unit columns are all the same

        Sample ID	Lab ID	            N_Val	NUnit	 P_Val      PUnit
1	CU ANNGRO	L19042090	    1.60		 1598	    %	
2	CU ANNGRO	L19042090						
3	CU BREAK THRU	L19042091	    1.53	%	 1598	    %											


What I have tried:

-- Simplify the pivot selection query by separating the query using a with clause

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 ('N000068')
  AND s.ID_NUMERIC between TO_NUMBER(12918) and TO_NUMBER(12920)
  )
SELECT pvt12.*
FROM(SELECT * FROM pivot_data PIVOT ( MAX(result_value) result_value , MAX(units) units FOR identity IN(
                                                                                                  'NIR_N' "Nitro",
                                                                                                  '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  
Posted
Updated 31-May-19 3:52am
v2

1 solution

I am a SQL Server person and I know what values I would replace the nulls with and I would be using the IsNull function.... But this is not Sql Server and I have no clue where your nulls are going to pop in and what values you want to replace them with.

Perhaps this will help
Ask TOM "Pivot and null values"[^]
 
Share this answer
 
Comments
Member 14225726 31-May-19 9:54am    
The values for units are stored in the database in my case?
MadMyche 31-May-19 11:08am    
The end result is that you need to figure out where the NULLs are and what to replace them with

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900