Interrogez plusieurs lignes avec un champ de key partagé dans une ligne de résultat unique pour chaque key

J'ai une table nommée Form avec le contenu suivant:

FormType KeyField Field AlphaValue NumericValue -------- --------- ------- -------------------- ------------- A AAA001 A001 EXT 0.000000 A AAA001 A002 mail 0.000000 A AAA001 A003 190.000000 A AAA001 A004 [email protected] 0.000000 A ABC123 A001 DIST 0.000000 A ABC123 A002 something 0.000000 A ABC123 A003 215.000000 A BBB255 A002 delivery 0.000000 A BBB255 A003 94.000000 A CCC923 A002 TECH 0.000000 A DDD123 A004 [email protected] 0.000000 

Le résultat souhaité doit être le KeyField suivi de tous les champs possibles (dans ce cas A001, A002, A003, A004), quelque chose comme:

 TypeA A001 A002 A003 A004 ------- ------------ ------------ ----------- -------------------- AAA001 EXT mail 190.000000 [email protected] ABC123 DIST something 215.000000 BBB255 delivery 94.000000 CCC923 TECH 0.000000 DDD123 0.000000 [email protected] 

J'ai travaillé sur ceci, mais ne reflète pas vraiment le résultat désiré:

 select a.KeyField as 'TypeA', b.AlphaValue as 'A001', c.AlphaValue as 'A002', d.NumericValue as 'A003', e.AlphaValue as 'A004' from (select distinct KeyField from Form where FormType = 'A') as a, (select AlphaValue, KeyField from Form where FormType = 'A' and Field = 'A001') as b, (select AlphaValue, KeyField from Form where FormType = 'A' and Field = 'A002') as c, (select NumericValue, KeyField from Form where FormType = 'A' and Field = 'A003') as d, (select AlphaValue, KeyField from Form where FormType = 'A' and Field = 'A004') as e where b.KeyField = a.KeyField and c.KeyField = a.KeyField and d.KeyField = a.KeyField and e.KeyField = a.KeyField 

La requête précédente a pour résultat:

 TypeA A001 A002 A003 A004 ------- ------------ ------------ ----------- -------------------- AAA001 EXT mail 190.000000 [email protected] 

Il saute simplement toutes les keys qui n'ont pas de résultat pour chacun des champs requirejs, quand j'ai besoin que les champs requirejs qui n'ont pas de correspondance soient remplacés par une string vide ou 0.000000.

Des idées sur la façon d'atteindre le résultat souhaité?

Essayez quelque chose comme ceci:

 SELECT master.KeyField AS 'TypeA', ISNULL(A001.AlphaValue, '') AS 'A001', ISNULL(A002.AlphaValue, '') AS 'A002', ISNULL(A003.NumericValue, 0) AS 'A003', ISNULL(A004.AlphaValue, '') AS 'A004' FROM ( SELECT FormType, KeyField FROM Form WHERE FormType = 'A' GROUP BY FormType, KeyField ) AS master LEFT JOIN Form AS A001 ON master.FormType = A001.FormType AND master.KeyField = A001.KeyField AND A001.Field = 'A001' LEFT JOIN Form AS A002 ON master.FormType = A002.FormType AND master.KeyField = A002.KeyField AND A002.Field = 'A002' LEFT JOIN Form AS A003 ON master.FormType = A003.FormType AND master.KeyField = A003.KeyField AND A003.Field = 'A003' LEFT JOIN Form AS A004 ON master.FormType = A004.FormType AND master.KeyField = A004.KeyField AND A004.Field = 'A004' 

Fondamentalement, nous interrogeons les keys uniques, puis nous quittons en rejoignant tous les autres champs. La fonction ISNULL fournit des valeurs par défaut.

Si vous faites une agrégation de toute façon (ce qui est essentiellement le cas), vous pouvez aussi bien utiliser l'agrégation conditionnelle:

 select KeyField, max(case when keyfield = 'A001' then alphafield else '' end) as A001, max(case when keyfield = 'A002' then alphafield else '' end) as A002, max(case when keyfield = 'A003' then NumericValue else 0 end) as A003, max(case when keyfield = 'A004' then alphafield else ''end) as A004 from Form where FormType = 'A' group by KeyField; 

Cela suppose qu'il n'y a qu'une seule valeur pour chacune des keys (comme implicite dans la question).