Sql 将具有共享键字段的多行查询为每个键的单个结果行
我有一个名为Form的表,其中包含以下内容:Sql 将具有共享键字段的多行查询为每个键的单个结果行,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个名为Form的表,其中包含以下内容: FormType KeyField Field AlphaValue NumericValue -------- --------- ------- -------------------- ------------- A AAA001 A001 EXT 0.000000 A AAA001 A002
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
所需的结果需要是键字段,后跟每个可能的字段(在本例中为A001、A002、A003、A004),类似于:
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
上一个查询结果为:
TypeA A001 A002 A003 A004
------- ------------ ------------ ----------- --------------------
AAA001 EXT mail 190.000000 example@example.com
当我需要将没有匹配项的必填字段替换为空字符串或0.000000时,它只跳过所有没有结果的键
关于如何达到预期的效果,你有什么想法吗?试试下面的方法:
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函数提供默认值 如果您正在进行聚合(这是
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;
这假设每个键只有一个值(如问题所暗示的)。您需要学习如何使用连接而不是逗号分隔的查询。这种编码方式是对每个查询进行内部连接。当您使用EAV模式创建数据结构时,这是一个真正的问题。如果您可以将您的示例发布为易于使用的内容,我可以帮助您构建一个查询,从中获取数据。这篇文章是一个很好的开始。很好的黑客与最大:)但你需要添加组的人clause@PavelMorshenyuk . . . 非常感谢。