1 abc 123 xyz工作 2 bcd 252 jhyvb备注5 3 efg 541 sdh DHidR3 Sry I Cant写表格式 我尝试了什么: i试了试外连接到Dataheader,我只显示数据头注释I have two tables like DataHeader and DataDetaili want to display the Remarks from DataDetail table which are having high DDidand if remarks not available in DataDetail then I want to display Remarks from DataHeader Tablehow to write query in SQLDataHeaderDHidAB C Remarks1 abc 123 xyzDHidR12 bcd 252 jhyvbDHidR23 efg 541 sdhDHidR3DataDetailDHid DDid Remarks11 working12 working13 worked21 Remarks122 Remarks223 Remarks324 Remarks425 Remarks5i want to display the data likeDHid ABCRemarks1 abc 123 xyz worked2 bcd 252 jhyvb Remarks53 efg 541 sdh DHidR3Sry I Cant write tables formatWhat I have tried:i have tried left outer join to Dataheader and i displaying dataheader remarks only推荐答案这样的东西:Something like this:SELECT DH.DHid, DH.A, DH.B, DH.C, IsNull( ( SELECT TOP 1 DD.Remarks FROM DataDetail As DD WHERE DD.DHid = DH.DHid And DD.Remarks Is Not Null ORDER BY DD.DDid DESC ), DH.Remarks ) As RemarksFROM DataHeader As DH; 或:Or:WITH cteDetail As( SELECT ROW_NUMBER() OVER (PARTITION BY DHid ORDER BY DDid DESC) As RN, DHid, Remarks FROM DataDetail WHERE Remarks Is Not Null)SELECT DH.DHid, DH.A, DH.B, DH.C, IsNull(DD.Remarks, DH.Remarks) As RemarksFROM DataHeader As DH LEFT JOIN cteDetail As DD ON DD.DHid = DH.DHid And DD.RN = 1; 这篇关于如何在SQL中编写查询以显示列的最大数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-19 02:04