smallbear008 2017-08-30 08:08 采纳率: 0%
浏览 745

SQL Server 问题咨询 - 联合表查询

表1如下所示:
图片说明

表2如下所示:
图片说明
表1和表2中的数据行数不同,在综合表1和表2查询时,将表1和表2中的小计行和余额调节表行对齐,上面的数据也保持对齐。(id分别为量表的自增id)
想要的结果如下:
图片说明

如下语句无法达到想要的效果,请各位大神帮忙
select (case when a.name1 is null then '' else a.name1 end), Convert(nvarchar(20), a.amt1, 0), (case when a.name2 is null then '' else a.name2 end), Convert(nvarchar(20), a.amt2, 0),
(case when b.name1 is null then '' else b.name1 end), Convert(nvarchar(20), b.amt1, 0), (case when b.name2 is null then '' else b.name2 end), Convert(nvarchar(20), b.amt2, 0)
from 表1 a full join #表2 b on a.id = b.id

  • 写回答

2条回答

  • smallbear008 2017-08-30 08:10
    关注
    如下语句无法达到想要的效果,请各位大神帮忙
    select (case when a.name1 is null then '' else a.name1 end), Convert(nvarchar(20), a.amt1, 0), (case when a.name2 is null then '' else a.name2 end), Convert(nvarchar(20), a.amt2, 0),
                       (case when b.name1 is null then '' else b.name1 end), Convert(nvarchar(20), b.amt1, 0), (case when b.name2 is null then '' else b.name2 end), Convert(nvarchar(20), b.amt2, 0)
                from 表1 a full join #表2 b on  a.id = b.id
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配