使用共享密钥字段将多行查询到每个密钥的单个结果行

时间:2016-04-06 21:12:42

标签: sql sql-server tsql

我有一个名为 Form 的表格,其中包含以下内容:

fishingcourses

所需的结果必须是 KeyField ,后跟每个可能的字段(在本例中为A001,A002,A003,A004),如:


    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    example@example.com   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    mail@example.com      0.000000

我一直在努力,但并没有真正反映出预期的结果:


    TypeA   A001         A002         A003        A004
    ------- ------------ ------------ ----------- --------------------
    AAA001  EXT          mail         190.000000  example@example.com
    ABC123  DIST         something    215.000000
    BBB255               delivery     94.000000
    CCC923  TECH                      0.000000
    DDD123                            0.000000    mail@example.com

上一个查询结果为:


    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

当我需要将没有匹配的必填字段替换为空字符串或0.000000时,它只是跳过所有必需字段都没有结果的键。

关于如何达到预期效果的任何想法?

2 个答案:

答案 0 :(得分:2)

如果你正在进行聚合(这是distinct本质上做的),那么你也可以使用条件聚合:

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;

这假设每个键只有一个值(如问题所暗示的那样)。

答案 1 :(得分:1)

尝试这样的事情:

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'

基本上我们查询唯一键,然后离开加入所有其他字段。 ISNULL函数提供默认值。