TRANSFORM Sum(Len([imie])) AS dl SELECT zawodnicy.waga FROM zawodnicy GROUP BY zawodnicy.waga PIVOT zawodnicy.kraj; select len(imie) from zawodnicy select * from ( select kraj, len(imie) as dl, wzrost from zawodnicy ) as tab pivot ( sum (wzrost) for kraj in ([POL],[ger], [nor]) ) as piv select * from ( select kraj, len(imie) as dl, wzrost from zawodnicy ) as tab pivot ( sum (wzrost) for kraj in ([pol]) ) as piv go DECLARE @colList varchar(500) SELECT @colList = COALESCE(@colList + ',', '') + kraj FROM (SELECT DISTINCT kraj FROM zawodnicy) AS kraje DECLARE @sqlToRun varchar(1000) SET @sqlToRun = ' SELECT * FROM ( select kraj, len(imie) as dl, wzrost from zawodnicy ) AS tab PIVOT ( sum (wzrost) FOR kraj IN ('+ @colList +') ) AS piv' EXEC (@sqlToRun) select COALESCE(kraj + ',','') from zawodnicy go DECLARE @colList varchar(500) SELECT @colList = COALESCE(@colList + ',', '') + kraj FROM (SELECT DISTINCT kraj FROM zawodnicy) AS kraje select @colList go DECLARE @colList varchar(500) SELECT @colList = @colList + ',' + kraj FROM (SELECT DISTINCT kraj FROM zawodnicy) AS kraje select @colList select nazwisko_t, data_ur_t, COALESCE(convert(varchar(max),data_ur_t),'y') t from trenerzy