SET NAMES cp1250;
 CREATE OR REPLACE VIEW `v_SRODKI_LOKOM` AS 
SELECT 
	sp.lp, 
	sp.Value,
	CONCAT_WS(' - ',NULLIF(sparent.DisplayValue,''),NULLIF(TRIM(REPLACE(sp.DisplayValue,IFNULL(sparent.DisplayValue,''),'')),'')) AS Nazwa,
	sp.DisplayValue,
	sp.param2 AS LokTyp,
	CONCAT_WS('.', sparent.param1, sp.param1) AS LP2,
	sp.Slownik_GUID AS slownik_GUID 
FROM `slowniki_pierwotne` sp 
LEFT JOIN `slowniki_pierwotne` sparent ON(sparent.Value = sp.MasterKey AND sparent.Slownik_GUID = 'SRODKI_LOKOM')
WHERE 
	sp.Slownik_GUID = 'SRODKI_LOKOM' 
	AND !EXISTS(
		SELECT 
			spChild.Value 
		FROM `slowniki_pierwotne` spChild 
		WHERE 
			sp.Value = spChild.MasterKey 
			AND spChild.Slownik_GUID = 'SRODKI_LOKOM' 
		LIMIT 1
	) 
	AND sp.poziom_upr >= 0 
ORDER BY sp.lp
;
