Sunday 15 February 2015

SQL Server : group by name and specify the values ​​in columns -


I've had requests for SQL Server to get a table with all the article features:

 < Code> tartikel.cArtNr id, as tMerkmal.cName feature, tMerkmalWertSprache.cWert FeatureValue as available as tartikel Join tartikel.kArtikel = tArtikelMerkmal.kArtikel on tArtikelMerkmal Insert tMerkmal on tArtikelMerkmal.kMerkmal = tMerkmal tArtikelMerkmal.kMerkmalWert join .kMerkmal inner tMerkmalWertSprache = tMerkmalWertSprache.kMerkmalWert where (tMerkmalWertSprache.kSprache = '1')   

I precision such results :

  id | FeatureName | Feature Value -------------------------------- 1 | Feature 1 | Value A1 | Feature 2 | Value A1 | Feature 2 | Value B1 Feature 2 | Value C1 Feature 3 | The value was a   

but I wanted to create a group by value in FeatureName and in separate columns.

Like it:

  ID | FeatureName | Feature value 1. Feature value 2 | Feature value 3 ----------------------------------------------- - ------------------- 1 | Feature 1 | Price A | | 1 | Feature 2 | Price A | Man B Value C1 Feature 3 | Price A | |           
after the text "itemprop =" text " >

you get results you can use the function of SQL server:

  select id, facility, FeatureValue_l, FeatureValue_2, the FeatureValue_3 (select tartikel .cArtNr ID, tMerkmal. as cName facility, as tMerkmalWertSprache.cWert FeatureValue, as 'FeatureValue _' + (tartikel.cArtNr, varchar by tMerkmal.cName order) by tMerkmal.cName as much (split ROW_NUMBER () (10 )) Join the cast seq tartikel from tartikel.kArtikel = tArtikelMerkmal.kArtikel on tArtikelMerkmal Join interiors tArtikelMerkmal.kMerkmal = tMerkmal.kMerkmal interiors on tMerkmal at tMerkmalWertSprache at tArtikelMerkmal.kMerkmalWert = tMerkmalWertSprache.kMerkmalWert ou tMerkmalWertSprache.kSprache = '1') for Axis (maximum (FeatureValue) Feature (Feature_1_1, Feature Value_2, Feature) VALUE_3) to CIC) PIV;   

If you had an unknown number of FeatureValues ​​for each ID, you can use dynamic SQL:

 < Code> NVARCHAR (Max), as @cols declaration @ query, select NVARCHAR (Max) @cols = STUFF (Selection Separated by ',' + 'QUOTENAME (' FeatureValue _ '+ tartikel.cArtNr, tMerkmal.cName), as entered in varchar (more than ROW_NUMBER (split (10)) tartikel join from within tArtikelMerkmal on tartikel.kArtikel = tArtikelMerkmal.kArtikel Where tArtikelMerkmal.kMerkmalWert = tMerkmalWertSprache.kMerkmalWert on tMerkmalWertSprache Join insider Join tArtikelMerkmal.kMerkmal = on tMerkmal tMerkmal.kMerkmal inner tMerkmalWertSprache.kSprache = '1' XML pATH ( ''), type ) .Value ('.', 'NVARCHAR (Max)'), 1,1, '') Set @query = 'Select ID, Feature,' + to @cols + '(Select tartikel. As carch.com (10)) as cArtNr AS ID, tMerkmal.cName AS Feature, TMMarkkalWertSprache.cWert Feature Value, '' Feature Value _ '' + Cast (row_number ()) (by tartikel.cartnr, tMerkmal.cName by tMerkmal the cName divided by order) to q Join tArtikelMerkmal within tartikel tartikel.kArtikel = tArtikelMerkmal.kArtikel where tArtikelMerkmal.kMerkmalWert = tMerkmalWertSprache.kMerkmalWert on tMerkmal Join insider tArtikelMerkmal.kMerkmal = tMerkmal.kMerkmal tMerkmalWertSprache Join insider tMerkmalWertSprache.kSprache = '' 1 '') Execute maximum (feature value) for seq in X axis (('+ @Cos +') (@ Korea)    

No comments:

Post a Comment