如何将SQL中3张表计算的2个字段的值插入一张表显示6个字段?

第一张表

select place,count(*)'>4μm_NG' from [Report_Test].[dbo].[ods_QualityModule_cleanliness_recycle] 

where place ='中转油箱出口'  and substring (convert (varchar(4),test_time),0,5) ='2022' and dust1>10000 group by place

第二张表

select place,count(*)'>6μm_NG' from [Report_Test].[dbo].[ods_QualityModule_cleanliness_recycle] 

where place ='中转油箱出口'  and substring (convert (varchar(4),test_time),0,5) ='2022' and dust2>640 group by place

第三张表

select place,count(*)'>14μm_NG' from [Report_Test].[dbo].[ods_QualityModule_cleanliness_recycle] 

where place ='中转油箱出口'  and substring (convert (varchar(4),test_time),0,5) ='2022' and dust3>160 group by place 

阿聪聪 发布于 2023-1-7 14:31 (编辑于 2023-1-7 14:31)
1min目标场景问卷 立即参与
回答问题
悬赏:0 F币 + 添加悬赏
提示:增加悬赏、完善问题、追问等操作,可使您的问题被置顶,并向所有关注者发送通知
共2回答
最佳回答
1
MKERALv6初级互助
发布于2023-1-7 15:26(编辑于 2023-1-7 15:43)

是这种效果?

效果一:
place>4μm_NG>6μm_NG>14μm_NG
中转油箱出口152030

如果是直接使用left join 将三段SQL拼起来就行

效果二:
place>4μm_NGplace>6μm_NGplace>14μm_NG
中转油箱出口15中转油箱出口20中转油箱出口30

第二种的话:

select a.*,b.*,c.*from(

select '1' as a ,place,count(*)'>4μm_NG' from [Report_Test].[dbo].[ods_QualityModule_cleanliness_recycle] 

where place ='中转油箱出口'  and substring (convert (varchar(4),test_time),0,5) ='2022' and dust1>10000 group by place

) as a

left join (

select '1' as a ,place,count(*)'>6μm_NG' from [Report_Test].[dbo].[ods_QualityModule_cleanliness_recycle] 

where place ='中转油箱出口'  and substring (convert (varchar(4),test_time),0,5) ='2022' and dust2>640 group by place

) as b on a.a=b.a

left join(

select '1' as a ,place,count(*)'>14μm_NG' from [Report_Test].[dbo].[ods_QualityModule_cleanliness_recycle] 

where place ='中转油箱出口'  and substring (convert (varchar(4),test_time),0,5) ='2022' and dust3>160 group by place 

) as on a.a=c.a

最佳回答
0
keyw1066Lv3见习互助
发布于2023-1-7 15:52

有个思路是先union,然后使用case when

  • 3关注人数
  • 244浏览人数
  • 最后回答于:2023-1-7 15:52
    请选择关闭问题的原因
    确定 取消
    返回顶部