关于FULL JOIN连接查询的问题 ̄ ̄!

DumDum DumDum 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
现在有两个表需要用到FULL JOIN连接查询,一个表是年销售计划,就是一年12个月那个月那个部门销售某个商品多少,另一个表销售明细表,就是在某一时间,那个部门卖闻某个商品多少。下面是我的查询


select isnull(a.syear,b.syear) as syear,
isnull(a.DepartmentID,b.DepartmentID) as DepartmentID,
isnull(a.CommodityID,b.CommodityID) as CommodityID
from
(select sYear ,DepartmentID,CommodityID,
sum(case when sMOnth =1 then isnull(quantity,0) else 0 end) as P01,
sum(case when sMOnth =2 then isnull(quantity,0) else 0 end) as P02,
sum(case when sMOnth =3 then isnull(quantity,0) else 0 end) as P03,
sum(case when sMOnth =4 then isnull(quantity,0) else 0 end) as P04,
sum(case when sMOnth =5 then isnull(quantity,0) else 0 end) as P05,
sum(case when sMOnth =6 then isnull(quantity,0) else 0 end) as P06,
sum(case when sMOnth =7 then isnull(quantity,0) else 0 end) as P07,
sum(case when sMOnth =8 then isnull(quantity,0) else 0 end) as P08,
sum(case when sMOnth =9 then isnull(quantity,0) else 0 end) as P09,
sum(case when sMOnth =10 then isnull(quantity,0) else 0 end) as P10,
sum(case when sMOnth =11 then isnull(quantity,0) else 0 end) as P11,
sum(case when sMOnth =12 then isnull(quantity,0) else 0 end) as P12
from tSalePlan
group by sYear,DepartmentID,CommodityID
) as a
full join
(select year(sDate) as sYear ,DepartmentID,CommodityID,
sum(case when month(sDate) =1 then isnull(quantity,0) else 0 end) as S01,
sum(case when month(sDate) =2 then isnull(quantity,0) else 0 end) as S02,
sum(case when month(sDate) =3 then isnull(quantity,0) else 0 end) as S03,
sum(case when month(sDate) =4 then isnull(quantity,0) else 0 end) as S04,
sum(case when month(sDate) =5 then isnull(quantity,0) else 0 end) as S05,
sum(case when month(sDate) =6 then isnull(quantity,0) else 0 end) as S06,
sum(case when month(sDate) =7 then isnull(quantity,0) else 0 end) as S07,
sum(case when month(sDate) =8 then isnull(quantity,0) else 0 end) as S08,
sum(case when month(sDate) =9 then isnull(quantity,0) else 0 end) as S09,
sum(case when month(sDate) =10 then isnull(quantity,0) else 0 end) as S10,
sum(case when month(sDate) =11 then isnull(quantity,0) else 0 end) as S11,
sum(case when month(sDate) =12 then isnull(quantity,0) else 0 end) as S12
from tSaleOrder
group by year(sDate),DepartmentID,CommodityID) as b
on a.sYear =b.sYear and a.DepartmentID=b.DepartmentID and a.CommodityID =b.CommodityID
order by a.sYear,a.DepartmentID,a.CommodityID


里面的单个查询没有问题,现在FULL JOin连接查询后,SYear,DepartmentID,CommodityID这三个字段能匹配的数据没有合并,被分拆成多条数据了 ̄ ̄!不知道为什么 ̄ ̄!那个大哥帮看看 ̄ ̄!
我另有一个DepartmentID,CommodityID这两个字段匹配的相似的查询没有问题,就这个不知道为什么不行 ̄ ̄!
...全文

相关推荐

SQL联合查询inner join、outer join和cross join的区别详解

对于开发使用到数据库的应用,免不了就要使用联合查询,SQL中常用的联合查询有inner join、outer join和cross join;这三者的区别很多人都应该不是很清楚,包括我自己,下面我们一起来看看,如果你使用join连表,缺陷的情况下是inner join,另外,开发中使用的left join和right join属于outer join,而outer join还包括full join.下面我通过图案让大家认识它们的区别。 现有两张表,Table A 是左边的表。Table B 是右边的表。其各有四条记录,其中有两条记录name是相同的: 1.INNER JOIN 产生的结果是A

weixin_38734037

SQL 连接 JOIN 例解左连接,右连接,全连接,内连接,交叉连接,自连接

SQL 连接 JOIN 例解左连接,右连接,全连接,内连接,交叉连接,自连接

苍狼_2001

inner join、 left join 、right join、 outer join之间的区别

inner join、 left join 、right join、 outer join之间的区别

虾说说

SQL的几种连接查询方式(内连接、外连接、全连接、联合查询)

准备表 drop table if exists test_a; CREATE TABLE `test_a` ( `id` varchar(10) NOT NULL, `username` varchar(10) NOT NULL, `password` varchar(10) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; drop table if exists test_a_description; CREATE TABLE `test_a_description` ( `id`

weixin_38628243

MySQL查询优化:连接查询排序limit(join、order by、limit语句)介绍

两张表连接查询并limit,SQL效率很高,但是加上order by以后,语句的执行时间变的巨长,效率巨低,接下来为大家介绍下连接查询排序limit

weixin_38611796
评论 12
收藏 已收藏
分享
返回顶部
DumDum
疑难问题 22,298 成员 121,727 内容