SQL - 其中 "in" 和 "not in" 从同一列到 2 个新列
SQL - Where "in" and "not in" from same column into 2 new columns
我一直在处理一份报告,但我不能使用@variable。
所以我试图用 "simple" SQL.
来解决我的问题
我的问题是我有一个 table (DEPTH
) 包含仓库中的所有位置。但是这个 table 并不能说明该位置是否被占用。要找出答案,我需要在 table (PRODUCTLOCATION
)
中使用 "where in" 或 "where not in"
我想将其作为一个输出而不是 运行 2 个查询。
使用 UNION
我得到了我想要的,但格式错误。
所以 LoationCount 结果应该是 1 列中存在的位置和另一列中不存在的位置
我尝试了很多东西。
如您所见,我希望按 EquipmentType 等分组
这是我现在的脚本。
select EquipmentType, EquipmentName, LocationName, LocationCount, Used_Or_UnUsed, LocationID, LocationType
from
(select et.equiptext EquipmentType,
e.EQUIPTEXT EquipmentName,
s.SIZETEXT LocationName, count(*) LocationCount, 'Empty Locations' as Used_Or_UnUsed, s.sizeid LocationID ,
case when d.LOCATIONTYPE=1 then 'Pick Location' when d.LOCATIONTYPE=2 then 'Buffer Location' else 'Unknown' end as LocationType
from depth d with (nolock)
join SIZEPARAM s with (nolock) on d.SIZEID=s.SIZEID
join POSITIONS p with (nolock) on d.POSITIONID=p.POSITIONID
join shelf sh with (nolock) on p.SHELFID=sh.SHELFID
join EQUIPMENT e with (nolock) on sh.EQUIPID=e.EQUIPID
join EQUIPMENTTYPE et with (nolock) on e.EQUIPTYPEID=et.EQUIPTYPEID
where d.DEPTHID not in (select depthid from productlocation)
and d.SIZEID in (select SIZEID from SIZEPARAM where SizeCategoryID in (10,20,30,40,50,60,70,210))
group by et.EQUIPTEXT,e.EQUIPTEXT, d.SIZEid, s.SIZEID, s.SIZETEXT, d.LOCATIONTYPE
) t
group by t.EquipmentType, t.EquipmentName, t.LocationName, t.LocationCount, t.Used_Or_UnUsed, t.LocationID, t.LocationType
union all
select EquipmentType, EquipmentName, LocationName, LocationCount, Used_Or_UnUsed, LocationID, LocationType
from
(select et.equiptext EquipmentType,
e.EQUIPTEXT EquipmentName,
s.SIZETEXT LocationName, count(*) LocationCount, 'Used Locations' as Used_Or_UnUsed, s.sizeid LocationID ,
case when d.LOCATIONTYPE=1 then 'Pick Location' when d.LOCATIONTYPE=2 then 'Buffer Location' else 'Unknown' end as LocationType
from depth d with (nolock)
join SIZEPARAM s with (nolock) on d.SIZEID=s.SIZEID
join POSITIONS p with (nolock) on d.POSITIONID=p.POSITIONID
join shelf sh with (nolock) on p.SHELFID=sh.SHELFID
join EQUIPMENT e with (nolock) on sh.EQUIPID=e.EQUIPID
join EQUIPMENTTYPE et with (nolock) on e.EQUIPTYPEID=et.EQUIPTYPEID
where d.DEPTHID in (select depthid from productlocation)
and d.SIZEID in (select SIZEID from SIZEPARAM where SizeCategoryID in (10,20,30,40,50,60,70,210))
group by et.EQUIPTEXT,e.EQUIPTEXT, d.SIZEid, s.SIZEID, s.SIZETEXT, d.LOCATIONTYPE
) t
group by t.EquipmentType, t.EquipmentName, t.LocationName, t.LocationCount, t.Used_Or_UnUsed, t.LocationID, t.LocationType
order by t.EquipmentName asc
请原谅我这个问题的蹩脚格式。
征得您的同意,我不会重复整个查询。
并把你需要的精华放在这里。
SELECT d.depthid AS depthid
,CASE WHEN pr.depthid IS NULL THEN 1 END AS not_in_flag
,CASE WHEN pr.depthid IS NOT NULL THEN 1 END AS in_flag
FROM depth d
LEFT OUTER JOIN productlocation pr
ON d.depthid = pr.depthid
只需将 d.depthid [not] in (select depthid from productlocation)
从您的 WHERE 子句(它使您只能获得一组或另一组)移动到带有 CASE WHEN 的 select 子句:
select
et.equiptext as equipmenttype,
e.equiptext as equipmentname,
s.sizetext as locationname,
count(*) as locationcount,
case when d.depthid in (select depthid from productlocation) then 'Used Locations'
else 'Empty Locations'
end as used_or_unused,
s.sizeid as locationid,
case when d.locationtype = 1 then 'pick location'
when d.locationtype = 2 then 'buffer location'
else 'unknown'
end as locationtype
from depth d with (nolock)
join sizeparam s with (nolock) on d.sizeid = s.sizeid
join positions p with (nolock) on d.positionid = p.positionid
join shelf sh with (nolock) on p.shelfid = sh.shelfid
join equipment e with (nolock) on sh.equipid = e.equipid
join equipmenttype et with (nolock) on e.equiptypeid = et.equiptypeid
where d.sizeid in (select sizeid from sizeparam where sizecategoryid in (10,20,30,40,50,60,70,210))
group by et.equiptext, e.equiptext, d.sizeid, s.sizeid, s.sizetext, d.locationtype
order by e.equiptext asc
我一直在处理一份报告,但我不能使用@variable。 所以我试图用 "simple" SQL.
来解决我的问题我的问题是我有一个 table (DEPTH
) 包含仓库中的所有位置。但是这个 table 并不能说明该位置是否被占用。要找出答案,我需要在 table (PRODUCTLOCATION
)
我想将其作为一个输出而不是 运行 2 个查询。
使用 UNION
我得到了我想要的,但格式错误。
所以 LoationCount 结果应该是 1 列中存在的位置和另一列中不存在的位置
我尝试了很多东西。 如您所见,我希望按 EquipmentType 等分组
这是我现在的脚本。
select EquipmentType, EquipmentName, LocationName, LocationCount, Used_Or_UnUsed, LocationID, LocationType
from
(select et.equiptext EquipmentType,
e.EQUIPTEXT EquipmentName,
s.SIZETEXT LocationName, count(*) LocationCount, 'Empty Locations' as Used_Or_UnUsed, s.sizeid LocationID ,
case when d.LOCATIONTYPE=1 then 'Pick Location' when d.LOCATIONTYPE=2 then 'Buffer Location' else 'Unknown' end as LocationType
from depth d with (nolock)
join SIZEPARAM s with (nolock) on d.SIZEID=s.SIZEID
join POSITIONS p with (nolock) on d.POSITIONID=p.POSITIONID
join shelf sh with (nolock) on p.SHELFID=sh.SHELFID
join EQUIPMENT e with (nolock) on sh.EQUIPID=e.EQUIPID
join EQUIPMENTTYPE et with (nolock) on e.EQUIPTYPEID=et.EQUIPTYPEID
where d.DEPTHID not in (select depthid from productlocation)
and d.SIZEID in (select SIZEID from SIZEPARAM where SizeCategoryID in (10,20,30,40,50,60,70,210))
group by et.EQUIPTEXT,e.EQUIPTEXT, d.SIZEid, s.SIZEID, s.SIZETEXT, d.LOCATIONTYPE
) t
group by t.EquipmentType, t.EquipmentName, t.LocationName, t.LocationCount, t.Used_Or_UnUsed, t.LocationID, t.LocationType
union all
select EquipmentType, EquipmentName, LocationName, LocationCount, Used_Or_UnUsed, LocationID, LocationType
from
(select et.equiptext EquipmentType,
e.EQUIPTEXT EquipmentName,
s.SIZETEXT LocationName, count(*) LocationCount, 'Used Locations' as Used_Or_UnUsed, s.sizeid LocationID ,
case when d.LOCATIONTYPE=1 then 'Pick Location' when d.LOCATIONTYPE=2 then 'Buffer Location' else 'Unknown' end as LocationType
from depth d with (nolock)
join SIZEPARAM s with (nolock) on d.SIZEID=s.SIZEID
join POSITIONS p with (nolock) on d.POSITIONID=p.POSITIONID
join shelf sh with (nolock) on p.SHELFID=sh.SHELFID
join EQUIPMENT e with (nolock) on sh.EQUIPID=e.EQUIPID
join EQUIPMENTTYPE et with (nolock) on e.EQUIPTYPEID=et.EQUIPTYPEID
where d.DEPTHID in (select depthid from productlocation)
and d.SIZEID in (select SIZEID from SIZEPARAM where SizeCategoryID in (10,20,30,40,50,60,70,210))
group by et.EQUIPTEXT,e.EQUIPTEXT, d.SIZEid, s.SIZEID, s.SIZETEXT, d.LOCATIONTYPE
) t
group by t.EquipmentType, t.EquipmentName, t.LocationName, t.LocationCount, t.Used_Or_UnUsed, t.LocationID, t.LocationType
order by t.EquipmentName asc
请原谅我这个问题的蹩脚格式。
征得您的同意,我不会重复整个查询。 并把你需要的精华放在这里。
SELECT d.depthid AS depthid
,CASE WHEN pr.depthid IS NULL THEN 1 END AS not_in_flag
,CASE WHEN pr.depthid IS NOT NULL THEN 1 END AS in_flag
FROM depth d
LEFT OUTER JOIN productlocation pr
ON d.depthid = pr.depthid
只需将 d.depthid [not] in (select depthid from productlocation)
从您的 WHERE 子句(它使您只能获得一组或另一组)移动到带有 CASE WHEN 的 select 子句:
select
et.equiptext as equipmenttype,
e.equiptext as equipmentname,
s.sizetext as locationname,
count(*) as locationcount,
case when d.depthid in (select depthid from productlocation) then 'Used Locations'
else 'Empty Locations'
end as used_or_unused,
s.sizeid as locationid,
case when d.locationtype = 1 then 'pick location'
when d.locationtype = 2 then 'buffer location'
else 'unknown'
end as locationtype
from depth d with (nolock)
join sizeparam s with (nolock) on d.sizeid = s.sizeid
join positions p with (nolock) on d.positionid = p.positionid
join shelf sh with (nolock) on p.shelfid = sh.shelfid
join equipment e with (nolock) on sh.equipid = e.equipid
join equipmenttype et with (nolock) on e.equiptypeid = et.equiptypeid
where d.sizeid in (select sizeid from sizeparam where sizecategoryid in (10,20,30,40,50,60,70,210))
group by et.equiptext, e.equiptext, d.sizeid, s.sizeid, s.sizetext, d.locationtype
order by e.equiptext asc