Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/27.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
Sql 尽管有多个ID,但在一个上显示不同的字段_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 尽管有多个ID,但在一个上显示不同的字段

Sql 尽管有多个ID,但在一个上显示不同的字段,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,这就是表格的外观: CountryId CountryName MemberType 143 AUT 1 171 AUT 2 202 AUT 3 206 BE 1 CountryName IsType1 IsType2 AUT Y N

这就是表格的外观:

CountryId   CountryName    MemberType
143           AUT                1
171           AUT                2
202           AUT                3
206           BE                 1
CountryName   IsType1   IsType2
   AUT          Y          N
   AUT          N          Y
   AUT          N          N
   BE           Y          N
CountryName   IsType1   IsType2
AUT             Y          Y
BE              Y          N
这是我运行查询时得到的结果:

CountryId   CountryName    MemberType
143           AUT                1
171           AUT                2
202           AUT                3
206           BE                 1
CountryName   IsType1   IsType2
   AUT          Y          N
   AUT          N          Y
   AUT          N          N
   BE           Y          N
CountryName   IsType1   IsType2
AUT             Y          Y
BE              Y          N
我的问题是,我希望每个countryname只显示一行。也就是说,如果是1型和2型。但是每个CountryName都有多个CountryId,所以我不知道该怎么办

这就是我想要的:

CountryId   CountryName    MemberType
143           AUT                1
171           AUT                2
202           AUT                3
206           BE                 1
CountryName   IsType1   IsType2
   AUT          Y          N
   AUT          N          Y
   AUT          N          N
   BE           Y          N
CountryName   IsType1   IsType2
AUT             Y          Y
BE              Y          N
这就是我的代码的样子:

SELECT 
 CountryName=c.CountryCode_ISO3166
,IsType1=CASE WHEN cm.CountryMemberTypeId = 1 then 'Y' else 'N' end
,IsType2=CASE WHEN cm.CountryMemberTypeId = 2 then 'Y' else 'N' end


FROM static.Country c
inner JOIN static.CountryMemberOfType cmem ON C.CountryId = cmem.CountryId
inner join static.CountryMemberType cm on cm.CountryMemberTypeId=cmem.CountryMemberTypeId
order by CountryCode_ISO3166

使用条件聚合:

select CountryName,
       case when sum(case when MemberType = 1 then 1 else 0 end) > 0 
            then 'Y' else 'N' end as IsType1,
       case when sum(case when MemberType = 2 then 1 else 0 end) > 0 
            then 'Y' else 'N' end as IsType2
from Tablename
group by CountryName   

使用条件聚合:

select CountryName,
       case when sum(case when MemberType = 1 then 1 else 0 end) > 0 
            then 'Y' else 'N' end as IsType1,
       case when sum(case when MemberType = 2 then 1 else 0 end) > 0 
            then 'Y' else 'N' end as IsType2
from Tablename
group by CountryName   

“当我运行我的查询时”哪个查询?另外,为什么这个查询返回
AT
对于
AUT
,这是需要的吗?为什么运行查询时AUT突然变成AT?您能告诉我们您的查询吗?可能重复(我建议查看我在该问题中的答案:-)您希望有多少个iTypex列?MemberType只有两个或尽可能多的值?只有三列,一列用于countryname,一列用于检查countryname是否有值1,一列用于检查countryname是否有值2。“当我运行查询时”哪个查询?另外,为什么这个查询返回
AT
对于
AUT
,这是需要的吗?为什么运行查询时AUT突然变成AT?您能告诉我们您的查询吗?可能重复(我建议查看我在该问题中的答案:-)您希望有多少个iTypex列?MemberType只有两个或尽可能多的值?只有三列,一列用于countryname,一列用于检查countryname是否有值1,一列用于检查countryname是否有值2。“当我运行查询时”哪个查询?另外,为什么这个查询返回
AT
对于
AUT
,这是需要的吗?为什么运行查询时AUT突然变成AT?您能告诉我们您的查询吗?可能重复(我建议查看我在该问题中的答案:-)您希望有多少个iTypex列?MemberType只有两个或尽可能多的值?只有三列,一列用于countryname,一列用于检查countryname是否有值1,另一列用于检查countryname是否有值2。工作正常。非常感谢!:)工作得很好。非常感谢!:)工作得很好。非常感谢!:)