MSSQL数据库联表查询求助

daviy1980 2021-03-27 04:47:09
现在有2个数据表:
表test_tuan_ti:字段分别为abc,如图:


表test_yan_piao:字段为jc,如图:



两个表的c字段对应,现在想联表查询得出对应c字段abj合计数,如excel表格截图那样:


求mssql查询命令。谢谢!!
...全文
111 点赞 收藏 6
写回复
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
daviy1980 03-28
引用 3 楼 RINK_1 的回复:
[quote=引用 2 楼 RINK_1 的回复:]SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B,SUM(j) AS TOTAL_J
FROM test_tuan_ti A
JOIN test_yan_piao B ON A.C=B.C
GROUP C


应该是GROUP BY C[/quote]


谢谢您的回答!但我按您的语句执行,却还是会重复统计:
回复
daviy1980 03-28
谢谢!


引用 5 楼 RINK_1 的回复:
[quote=引用 4 楼 daviy1980 的回复:][quote=引用 3 楼 RINK_1 的回复:][quote=引用 2 楼 RINK_1 的回复:]SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B,SUM(j) AS TOTAL_J
FROM test_tuan_ti A
JOIN test_yan_piao B ON A.C=B.C
GROUP C


应该是GROUP BY C[/quote]


谢谢您的回答!但我按您的语句执行,却还是会重复统计:
[/quote]


SELECT A.C,TOTAL_A,TOTAL_B,TOTAL_J
FROM (SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B FROM test_tuan_ti GROUP BY C) AS A
JOIN (SELECT C,SUM(j) AS TOTAL_J FROM test_yan_piao GROUP BY C) B ON A.C=B.C


[/quote]
回复
RINK_1 03-28
引用 4 楼 daviy1980 的回复:
[quote=引用 3 楼 RINK_1 的回复:][quote=引用 2 楼 RINK_1 的回复:]SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B,SUM(j) AS TOTAL_J FROM test_tuan_ti A JOIN test_yan_piao B ON A.C=B.C GROUP C
应该是GROUP BY C[/quote] 谢谢您的回答!但我按您的语句执行,却还是会重复统计: [/quote]

SELECT A.C,TOTAL_A,TOTAL_B,TOTAL_J 
FROM (SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B FROM test_tuan_ti GROUP BY C) AS A
JOIN (SELECT C,SUM(j) AS TOTAL_J  FROM test_yan_piao GROUP BY C) B ON A.C=B.C

回复
RINK_1 03-27
引用 2 楼 RINK_1 的回复:
SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B,SUM(j) AS TOTAL_J FROM test_tuan_ti A JOIN test_yan_piao B ON A.C=B.C GROUP C
应该是GROUP BY C
回复
RINK_1 03-27
SELECT C,SUM(a) AS TOTAL_A,SUM(b) AS TOTAL_B,SUM(j) AS TOTAL_J FROM test_tuan_ti A JOIN test_yan_piao B ON A.C=B.C GROUP C
回复
daviy1980 03-27
请各位老师们帮帮忙。
回复
相关推荐
发帖
疑难问题
创建于2007-09-28

2.1w+

社区成员

MS-SQL Server 疑难问题
申请成为版主
帖子事件
创建了帖子
2021-03-27 04:47
社区公告
暂无公告