Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
每个ID多行的表的SQL查询输出每个ID一行_Sql_Sql Server - Fatal编程技术网

每个ID多行的表的SQL查询输出每个ID一行

每个ID多行的表的SQL查询输出每个ID一行,sql,sql-server,Sql,Sql Server,这对我来说有点扫兴。我简化了这些表,并添加了一个示例场景来辅助上下文。我需要在SQLServer中编写一个查询,该查询将使用第一个表中的数据通过中心的引用表在第三个表中输出结果。我在编写SQL查询方面不是特别聪明(但肯定会越来越好),所以您能为我提供的任何帮助都将是非常棒的!各表如下: 以下是可能包含以下内容的数据表: 单个标识的一个和三个条目 ┌────────┬──────────────────┐ │Identity│Partial_Identifier│ ├────────┼──────

这对我来说有点扫兴。我简化了这些表,并添加了一个示例场景来辅助上下文。我需要在SQLServer中编写一个查询,该查询将使用第一个表中的数据通过中心的引用表在第三个表中输出结果。我在编写SQL查询方面不是特别聪明(但肯定会越来越好),所以您能为我提供的任何帮助都将是非常棒的!各表如下:

以下是可能包含以下内容的数据表: 单个标识的一个和三个条目

┌────────┬──────────────────┐
│Identity│Partial_Identifier│
├────────┼──────────────────┤
│100     │a                 │
├────────┼──────────────────┤
│100     │b                 │
├────────┼──────────────────┤
│100     │c                 │
├────────┼──────────────────┤
│101     │b                 │
├────────┼──────────────────┤
│102     │b                 │
├────────┼──────────────────┤
│102     │c                 │
└────────┴──────────────────┘
下面是与部分标识符组合匹配的参考表 我需要一个(唯一的)
IDCode
,用于显示。设计是 不是我认为是理想的东西,但那是预先存在的,所以 我得将就一下

┌──────┬────────────────────┬────────────────────┬────────────────────┐
│IDCode│Partial_Identifier_1│Partial_Identifier_2│Partial_Identifier_3│
├──────┼────────────────────┼────────────────────┼────────────────────┤
│1     │a                   │                    │                    │
├──────┼────────────────────┼────────────────────┼────────────────────┤
│2     │a                   │b                   │                    │
├──────┼────────────────────┼────────────────────┼────────────────────┤
│3     │a                   │b                   │c                   │
├──────┼────────────────────┼────────────────────┼────────────────────┤
│4     │b                   │                    │                    │
├──────┼────────────────────┼────────────────────┼────────────────────┤
│5     │b                   │c                   │                    │
├──────┼────────────────────┼────────────────────┼────────────────────┤
│6     │b                   │c                   │d                   │
└──────┴────────────────────┴────────────────────┴────────────────────┘
对于第一个表中的数据,我希望得到以下结果:

┌────────┬──────┐
│Identity│IDCode│
├────────┼──────┤
│100     │3     │
├────────┼──────┤
│101     │4     │
├────────┼──────┤
│102     │5     │
└────────┴──────┘

如果您能就如何处理这一点提供任何帮助,我们将不胜感激。

可能不是最有效的方法,但这将起作用:

declare @a table (id int, p_id nchar(1))
insert @a
select 100,'a'
union select 100,'b'
union select 100,'c'
union select 101,'b'
union select 102,'b'
union select 102,'c'

declare @b table (idcode int, p_id1 nchar(1), p_id2 nchar(1), p_id3 nchar(1))
insert @b
select 1, 'a', null, null
union select 2, 'a', 'b', null
union select 3, 'a', 'b', 'c'
union select 4, 'b', null, null
union select 5, 'b', 'c', null
union select 6, 'b', 'c', 'd'

select id, idcode
from 
(
    select id
    , max(case when r=1 then p_id end) a
    , max(case when r=2 then p_id end) b
    , max(case when r=3 then p_id end) c
    from (
        select id, p_id, row_number() over (partition by id order by p_id) r
        from @a
    ) x
    group by id
) y
inner join @b b
on coalesce(b.p_id1,'') = coalesce(y.a,'')
and coalesce(b.p_id2,'') = coalesce(y.b,'')
and coalesce(b.p_id3,'') = coalesce(y.c,'')
order by id

对于
Partial\u Identifier
,值是什么样的?“最低的”是否总是出现在
部分标识符\u 1
中,还是有些不按顺序出现?另外,请始终指定您正在使用的SQL Server版本。