如何在 SQL 服务器中根据我的条件执行 JOIN 查询?
How to perform a JOIN Query in my criteria in SQL Server?
我正在努力避免重复。有时显示重复记录,我不明白我做错了什么。
我对 SQL 进行了格式化,使其更具可读性。此外,顶部的 case 语句应该只是 coalesce() 或 isnull() 而不是 case 语句。
我认为,问题是 JEDnineDays 实际上并不包含返回的所有 ItemId,因此您的其他连接不会相互连接。您需要在连接语句中添加更多条件。类似于:
on (JEDnineDays.itemid = MECnineDays.itemid OR JEDthirtyDays.itemid = MECnineDays.itemid OR ... etc)
或者,可能更好的方法是执行 FROM Item
table,然后加入其余的 table。这将确保所有 table 都可以重新连接到现有项目。一旦您知道您将在 FROM 子句中拥有所有 ItemId,您可以根据需要将所有完整外部联接更改为左联接。
已修复SQL:
select
itemid
,convert(DECIMAL(10,2),JEDnineDays.NineDaysSold) JEDNineDaysSold
,convert(DECIMAL(10,2),MECnineDays.NineDaysSold) MECNineDaysSold
,convert(DECIMAL(10,2),JEDthirtyDays.ThirtyDaysSold) JEDThirtyDaysSold
,convert(DECIMAL(10,2),MECthirtyDays.ThirtyDaysSold) MECThirtyDaysSold
into #days
from Items i
full outer join (
select itemid, sum(qtysold) as NineDaysSold
from [JC_ItemDSP10days]
where StoreID IN ('1201','1302','1400','1500')
group by ItemID
) as JEDnineDays on(JEDnineDays.itemid = i.itemid)
full outer join (
select itemid, sum(qtysold) as NineDaysSold
from [JC_ItemDSP10days]
where StoreID IN ('2001','2400','2200')
group by ItemID
) as MECnineDays on(i.itemid = MECnineDays.itemid)
full outer join (
select itemid, sum(qtysold) as ThirtyDaysSold
from [JC_ItemDSP30Days]
where StoreID IN ('1201','1302','1400','1500')
group by ItemID
) as JEDthirtyDays on(i.itemid = JEDthirtyDays.itemid)
full outer join (
select
itemid, sum(qtysold) as ThirtyDaysSold
from [JC_ItemDSP30Days]
where StoreID IN ('2001','2400','2200')
group by ItemID
) as MECthirtyDays on(i.itemid = MECthirtyDays.itemid)
您的第一个子查询可能不会 return 所有 ItemID
值。 full join
会将来自其他子查询的值放在不同的行中。
使用完整列表开始您的查询,然后将其余部分加入其中:
from (
select distinct ItemID
from JC_ItemDSP10days
) all_items
full outer join
(
) sub1
on sub1.ItemID = all_items.ItemID
我同意它们看起来像重复项,但初始 ItemId CASE 语句表明 similar/same 值来自两个不同的 tables/views:
具体为:
- MECnineDays.ItemID
- JEDthirtyDays.ItemID
另外,不是说它错了,而是 Full Outer Join 的用法看起来很可疑。
您可能想查看以下其中一项或两项:
- 联合或全部联合
- 分组依据,使用 Min()/Max()/Sum()
一旦您选择了 #Days,这会产生预期的结果吗?
select itemid,
Sum(IsNull(JEDNineDaysSold,0)) JEDNineDaysSold,
Sum(IsNull(MECNineDaysSold,0)) MECNineDaysSold,
Sum(IsNull(JEDThirtyDaysSold,0)) JEDThirtyDaysSold,
Sum(IsNull(MECThirtyDaysSold,0)) MECThirtyDaysSold
from #days
group by itemid
order by itemid
我正在努力避免重复。有时显示重复记录,我不明白我做错了什么。
我对 SQL 进行了格式化,使其更具可读性。此外,顶部的 case 语句应该只是 coalesce() 或 isnull() 而不是 case 语句。
我认为,问题是 JEDnineDays 实际上并不包含返回的所有 ItemId,因此您的其他连接不会相互连接。您需要在连接语句中添加更多条件。类似于:
on (JEDnineDays.itemid = MECnineDays.itemid OR JEDthirtyDays.itemid = MECnineDays.itemid OR ... etc)
或者,可能更好的方法是执行 FROM Item
table,然后加入其余的 table。这将确保所有 table 都可以重新连接到现有项目。一旦您知道您将在 FROM 子句中拥有所有 ItemId,您可以根据需要将所有完整外部联接更改为左联接。
已修复SQL:
select
itemid
,convert(DECIMAL(10,2),JEDnineDays.NineDaysSold) JEDNineDaysSold
,convert(DECIMAL(10,2),MECnineDays.NineDaysSold) MECNineDaysSold
,convert(DECIMAL(10,2),JEDthirtyDays.ThirtyDaysSold) JEDThirtyDaysSold
,convert(DECIMAL(10,2),MECthirtyDays.ThirtyDaysSold) MECThirtyDaysSold
into #days
from Items i
full outer join (
select itemid, sum(qtysold) as NineDaysSold
from [JC_ItemDSP10days]
where StoreID IN ('1201','1302','1400','1500')
group by ItemID
) as JEDnineDays on(JEDnineDays.itemid = i.itemid)
full outer join (
select itemid, sum(qtysold) as NineDaysSold
from [JC_ItemDSP10days]
where StoreID IN ('2001','2400','2200')
group by ItemID
) as MECnineDays on(i.itemid = MECnineDays.itemid)
full outer join (
select itemid, sum(qtysold) as ThirtyDaysSold
from [JC_ItemDSP30Days]
where StoreID IN ('1201','1302','1400','1500')
group by ItemID
) as JEDthirtyDays on(i.itemid = JEDthirtyDays.itemid)
full outer join (
select
itemid, sum(qtysold) as ThirtyDaysSold
from [JC_ItemDSP30Days]
where StoreID IN ('2001','2400','2200')
group by ItemID
) as MECthirtyDays on(i.itemid = MECthirtyDays.itemid)
您的第一个子查询可能不会 return 所有 ItemID
值。 full join
会将来自其他子查询的值放在不同的行中。
使用完整列表开始您的查询,然后将其余部分加入其中:
from (
select distinct ItemID
from JC_ItemDSP10days
) all_items
full outer join
(
) sub1
on sub1.ItemID = all_items.ItemID
我同意它们看起来像重复项,但初始 ItemId CASE 语句表明 similar/same 值来自两个不同的 tables/views:
具体为:
- MECnineDays.ItemID
- JEDthirtyDays.ItemID
另外,不是说它错了,而是 Full Outer Join 的用法看起来很可疑。
您可能想查看以下其中一项或两项:
- 联合或全部联合
- 分组依据,使用 Min()/Max()/Sum()
一旦您选择了 #Days,这会产生预期的结果吗?
select itemid,
Sum(IsNull(JEDNineDaysSold,0)) JEDNineDaysSold,
Sum(IsNull(MECNineDaysSold,0)) MECNineDaysSold,
Sum(IsNull(JEDThirtyDaysSold,0)) JEDThirtyDaysSold,
Sum(IsNull(MECThirtyDaysSold,0)) MECThirtyDaysSold
from #days
group by itemid
order by itemid